Skip to content

Commit

Permalink
feat: yup
Browse files Browse the repository at this point in the history
- switched to yup
- zod doesn't transform boolean
  • Loading branch information
denchiklut committed Mar 2, 2024
1 parent 3db36f5 commit c98ab10
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 20 deletions.
1 change: 0 additions & 1 deletion config/spec/global.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
module.exports = async () => {
process.env.HOST = 'http://localhost:3000'
process.env.NODE_ENV = 'development'
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"uuid": "^9.0.1",
"winston": "^3.10.0",
"workbox-window": "^7.0.0",
"zod": "^3.22.2"
"yup": "^1.3.3"
},
"devDependencies": {
"@babel/cli": "7.22.10",
Expand Down
22 changes: 11 additions & 11 deletions src/common/decoder/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import type { ZodType, TypeOf } from 'zod'
import type { AnySchema, InferType } from 'yup'

export const decode = <S extends ZodType>(schema: S, data: Record<string, string | undefined>) => {
function getDecoder(variable?: never, initial?: never): TypeOf<S>
function getDecoder<T extends keyof TypeOf<S>>(variable: T, initial?: never): TypeOf<S>[T]
function getDecoder<T extends keyof TypeOf<S>>(
export const decode = <S extends AnySchema>(schema: S, data: Collection<string, unknown>) => {
function getDecoder(variable?: never, initial?: never): InferType<S>
function getDecoder<T extends keyof InferType<S>>(variable: T, initial?: never): InferType<S>[T]
function getDecoder<T extends keyof InferType<S>>(
variable: T,
initial: NonNullable<TypeOf<S>[T]>
): NonNullable<TypeOf<S>[T]>
function getDecoder<T extends keyof TypeOf<S>>(
initial: NonNullable<InferType<S>[T]>
): NonNullable<InferType<S>[T]>
function getDecoder<T extends keyof InferType<S>>(
variable: T,
initial: TypeOf<S>[T]
): TypeOf<S>[T] {
const source = schema.parse(data)
initial: InferType<S>[T]
): InferType<S>[T] {
const source = schema.validateSync(data)
if (!variable) return source

return source[variable] ?? initial
Expand Down
16 changes: 9 additions & 7 deletions src/common/env/env.util.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { decode } from '../decoder'
import { z, type TypeOf } from 'zod'
import { string, mixed, object, type InferType } from 'yup'

const envSchema = z.object({
HOST: z.string().url().default('http://localhost:3000'),
PUBLIC_PATH: z.string().default('/'),
APP_VERSION: z.string().default('0.0.0'),
NODE_ENV: z.enum(['production', 'development']).default('development')
const envSchema = object({
HOST: string().default('http://localhost:3000'),
PUBLIC_PATH: string().default('/'),
APP_VERSION: string().default('0.0.0'),
NODE_ENV: mixed<'production' | 'development' | 'test'>()
.oneOf(['production', 'development', 'test'])
.default('development')
})

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

export const getENV = decode(envSchema, IS_SERVER || IS_SPA ? process.env : window.env_vars)

Expand Down

0 comments on commit c98ab10

Please sign in to comment.