From c98ab106a5cdd57a5047e25b68d3e0d9f6c37b9a Mon Sep 17 00:00:00 2001 From: Karl Aleksandrov Date: Sat, 2 Mar 2024 11:27:09 +0100 Subject: [PATCH] feat: yup - switched to yup - zod doesn't transform boolean --- config/spec/global.ts | 1 - package.json | 2 +- src/common/decoder/index.ts | 22 +++++++++++----------- src/common/env/env.util.ts | 16 +++++++++------- 4 files changed, 21 insertions(+), 20 deletions(-) diff --git a/config/spec/global.ts b/config/spec/global.ts index f01e6a0..b6d2a3c 100644 --- a/config/spec/global.ts +++ b/config/spec/global.ts @@ -1,4 +1,3 @@ module.exports = async () => { process.env.HOST = 'http://localhost:3000' - process.env.NODE_ENV = 'development' } diff --git a/package.json b/package.json index 8f5f050..64209dc 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/common/decoder/index.ts b/src/common/decoder/index.ts index 276764d..6efcfd7 100644 --- a/src/common/decoder/index.ts +++ b/src/common/decoder/index.ts @@ -1,17 +1,17 @@ -import type { ZodType, TypeOf } from 'zod' +import type { AnySchema, InferType } from 'yup' -export const decode = (schema: S, data: Record) => { - function getDecoder(variable?: never, initial?: never): TypeOf - function getDecoder>(variable: T, initial?: never): TypeOf[T] - function getDecoder>( +export const decode = (schema: S, data: Collection) => { + function getDecoder(variable?: never, initial?: never): InferType + function getDecoder>(variable: T, initial?: never): InferType[T] + function getDecoder>( variable: T, - initial: NonNullable[T]> - ): NonNullable[T]> - function getDecoder>( + initial: NonNullable[T]> + ): NonNullable[T]> + function getDecoder>( variable: T, - initial: TypeOf[T] - ): TypeOf[T] { - const source = schema.parse(data) + initial: InferType[T] + ): InferType[T] { + const source = schema.validateSync(data) if (!variable) return source return source[variable] ?? initial diff --git a/src/common/env/env.util.ts b/src/common/env/env.util.ts index cf38050..e7b7f1c 100644 --- a/src/common/env/env.util.ts +++ b/src/common/env/env.util.ts @@ -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 +export type Env = InferType export const getENV = decode(envSchema, IS_SERVER || IS_SPA ? process.env : window.env_vars)