Skip to content

Commit

Permalink
feat: swap zod (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
denchiklut authored Sep 29, 2024
1 parent 0a4a93d commit 497e177
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 67 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"uuid": "^10.0.0",
"winston": "^3.14.2",
"workbox-window": "^7.1.0",
"yup": "^1.4.0"
"zod": "^3.23.8"
},
"devDependencies": {
"@babel/cli": "7.25.6",
Expand Down
31 changes: 20 additions & 11 deletions src/common/env/env.util.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
import type { ObjectSchema, InferType, AnyObject } from 'yup'
import { parse } from './parse.util'
import type { TypeOf, ZodObject, ZodRawShape } from 'zod'

interface Props<T extends AnyObject> {
schema: ObjectSchema<T>
envs: Partial<Record<keyof InferType<ObjectSchema<T>>, unknown>>
interface Props<T extends ZodRawShape> {
schema: ZodObject<T>
envs: Partial<Record<keyof TypeOf<ZodObject<T>>, unknown>>
clientPrefix?: string
}
export function createEnv<S extends AnyObject>({
export function createEnv<S extends ZodRawShape>({
schema,
envs,
clientPrefix = 'CLIENT_'
}: Props<S>) {
const client = schema.pick(Object.keys(schema.shape).filter(k => k.startsWith(clientPrefix)))
const { data, error } = parse((IS_SERVER || IS_SPA ? schema : client) as ObjectSchema<S>, envs)
const client = schema.pick(
Object.keys(schema.shape)
.filter(k => k.startsWith(clientPrefix))
.reduce((acc, key) => {
const res = acc as Collection<string, boolean>
res[key] = true

if (error) {
console.error('❌ Invalid environment variables:', error.errors)
return res
}, {})
)

const { success, data, error } = (IS_SERVER || IS_SPA ? schema : client).safeParse(envs)

if (!success) {
console.error('❌ Invalid environment variables:', error.flatten().fieldErrors)
throw new Error('Invalid environment variables')
}

Expand All @@ -35,5 +44,5 @@ export function createEnv<S extends AnyObject>({

return Reflect.get(target, prop, receiver)
}
}) as InferType<ObjectSchema<S>>
}) as TypeOf<ZodObject<S>>
}
22 changes: 11 additions & 11 deletions src/common/env/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { string, mixed, object, type InferType } from 'yup'
import { z, type TypeOf } from 'zod'
import { getOrDefault } from './get.util'
import { createEnv } from './env.util'

if (IS_SERVER) require('dotenv/config')

// you can find implementation for a `zod` library
// in the `feat/pipable-stream` git branch
const envSchema = object({
CLIENT_HOST: string().default('http://localhost:3000'),
CLIENT_PUBLIC_PATH: string().default('0.0.0'),
APP_VERSION: string().default('0.0.0'),
NODE_ENV: mixed<'production' | 'development' | 'test'>()
.oneOf(['production', 'development', 'test'])
.default('development')
/**
* you can find implementation for a `yup` library
* in the `feat/pipable-stream` git branch
*/
const envSchema = z.object({
CLIENT_HOST: z.string().url().default('http://localhost:3000'),
CLIENT_PUBLIC_PATH: z.string().default('/'),
APP_VERSION: z.string().default('0.0.0'),
NODE_ENV: z.enum(['production', 'development', 'test']).default('development')
})

export type Env = InferType<typeof envSchema>
export type Env = TypeOf<typeof envSchema>

export const getENV = getOrDefault(
createEnv({
Expand Down
13 changes: 0 additions & 13 deletions src/common/env/parse.util.ts

This file was deleted.

32 changes: 1 addition & 31 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7854,11 +7854,6 @@ prop-types@^15.8.1:
object-assign "^4.1.1"
react-is "^16.13.1"

property-expr@^2.0.5:
version "2.0.6"
resolved "https://registry.yarnpkg.com/property-expr/-/property-expr-2.0.6.tgz#f77bc00d5928a6c748414ad12882e83f24aec1e8"
integrity sha512-SVtmxhRE/CGkn3eZY1T6pC8Nln6Fr/lu1mKSgRud0eC73whjGfoAogbn78LkD8aFL0zz3bAFerKSnOl7NlErBA==

proxy-addr@~2.0.7:
version "2.0.7"
resolved "https://registry.yarnpkg.com/proxy-addr/-/proxy-addr-2.0.7.tgz#f19fe69ceab311eeb94b42e70e8c2070f9ba1025"
Expand Down Expand Up @@ -9239,11 +9234,6 @@ thunky@^1.0.2:
resolved "https://registry.yarnpkg.com/thunky/-/thunky-1.1.0.tgz#5abaf714a9405db0504732bbccd2cedd9ef9537d"
integrity sha512-eHY7nBftgThBqOyHGVN+l8gF0BucP09fMo0oO/Lb0w1OF80dJv+lDVpXG60WMQvkcxAkNybKsrEIE3ZtKGmPrA==

tiny-case@^1.0.3:
version "1.0.3"
resolved "https://registry.yarnpkg.com/tiny-case/-/tiny-case-1.0.3.tgz#d980d66bc72b5d5a9ca86fb7c9ffdb9c898ddd03"
integrity sha512-Eet/eeMhkO6TX8mnUteS9zgPbUMQa4I6Kkp5ORiBD5476/m+PIRiumP5tmh5ioJpH7k51Kehawy2UDfsnxxY8Q==

[email protected]:
version "1.0.5"
resolved "https://registry.yarnpkg.com/tmpl/-/tmpl-1.0.5.tgz#8683e0b902bb9c20c4f726e3c0b69f36518c07cc"
Expand All @@ -9266,11 +9256,6 @@ [email protected]:
resolved "https://registry.yarnpkg.com/toidentifier/-/toidentifier-1.0.1.tgz#3be34321a88a820ed1bd80dfaa33e479fbb8dd35"
integrity sha512-o5sSPKEkg/DIQNmH43V0/uerLrpzVedkUh8tGNvaeXpfpuwjKenlSox/2O/BTlZUtEe+JG7s5YhEz608PlAHRA==

toposort@^2.0.2:
version "2.0.2"
resolved "https://registry.yarnpkg.com/toposort/-/toposort-2.0.2.tgz#ae21768175d1559d48bef35420b2f4962f09c330"
integrity sha512-0a5EOkAUp8D4moMi2W8ZF8jcga7BgZd91O/yabJCFY8az+XSzeGyTKs0Aoo897iV1Nj6guFq8orWDS96z91oGg==

touch@^3.1.0:
version "3.1.1"
resolved "https://registry.yarnpkg.com/touch/-/touch-3.1.1.tgz#097a23d7b161476435e5c1344a95c0f75b4a5694"
Expand Down Expand Up @@ -9399,11 +9384,6 @@ type-fest@^0.21.3:
resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-0.21.3.tgz#d260a24b0198436e133fa26a524a6d65fa3b2e37"
integrity sha512-t0rzBq87m3fVcduHDUFhKmyyX+9eo6WQjZvf51Ea/M0Q7+T374Jp1aUiyUl0GKxp8M/OETVHSDvmkyPgvX+X2w==

type-fest@^2.19.0:
version "2.19.0"
resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-2.19.0.tgz#88068015bb33036a598b952e55e9311a60fd3a9b"
integrity sha512-RAH822pAdBgcNMAfWnCBU3CFZcfZ/i1eZjwFU/dsLKumyuuP3niueg2UAukXYF0E2AAoc82ZSSf9J0WQBinzHA==

type-is@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/type-is/-/type-is-2.0.0.tgz#7d249c2e2af716665cc149575dadb8b3858653af"
Expand Down Expand Up @@ -10231,16 +10211,6 @@ yocto-queue@^1.0.0:
resolved "https://registry.yarnpkg.com/yocto-queue/-/yocto-queue-1.1.1.tgz#fef65ce3ac9f8a32ceac5a634f74e17e5b232110"
integrity sha512-b4JR1PFR10y1mKjhHY9LaGo6tmrgjit7hxVIeAmyMw3jegXR4dhYqLaQF5zMXZxY7tLpMyJeLjr1C4rLmkVe8g==

yup@^1.4.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/yup/-/yup-1.4.0.tgz#898dcd660f9fb97c41f181839d3d65c3ee15a43e"
integrity sha512-wPbgkJRCqIf+OHyiTBQoJiP5PFuAXaWiJK6AmYkzQAh5/c2K9hzSApBZG5wV9KoKSePF7sAxmNSvh/13YHkFDg==
dependencies:
property-expr "^2.0.5"
tiny-case "^1.0.3"
toposort "^2.0.2"
type-fest "^2.19.0"

zod-validation-error@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/zod-validation-error/-/zod-validation-error-2.1.0.tgz#208eac75237dfed47c0018d2fe8fd03501bfc9ac"
Expand All @@ -10251,7 +10221,7 @@ zod-validation-error@^3.0.3:
resolved "https://registry.yarnpkg.com/zod-validation-error/-/zod-validation-error-3.4.0.tgz#3a8a1f55c65579822d7faa190b51336c61bee2a6"
integrity sha512-ZOPR9SVY6Pb2qqO5XHt+MkkTRxGXb4EVtnjc9JpXUOtUB1T9Ru7mZOT361AN3MsetVe7R0a1KZshJDZdgp9miQ==

zod@^3.22.4:
zod@^3.22.4, zod@^3.23.8:
version "3.23.8"
resolved "https://registry.yarnpkg.com/zod/-/zod-3.23.8.tgz#e37b957b5d52079769fb8097099b592f0ef4067d"
integrity sha512-XBx9AXhXktjUqnepgTiE5flcKIYWi/rme0Eaj+5Y0lftuGBq+jyRu/md4WnuxqgP1ubdpNCsYEYPxrzVHD8d6g==

0 comments on commit 497e177

Please sign in to comment.