From d7c8fd1a2d4e80685c82eb5fbb97cb606e13fcde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Valverd=C3=A9?= Date: Fri, 15 Mar 2024 19:18:10 +0100 Subject: [PATCH] stripZodSchemaDefaults util --- src/util/index.ts | 1 + src/util/zod.ts | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 src/util/zod.ts diff --git a/src/util/index.ts b/src/util/index.ts index 7bc5968..03b6dcd 100644 --- a/src/util/index.ts +++ b/src/util/index.ts @@ -2,3 +2,4 @@ export * from "./class" export * from "./effect" export * from "./extend" export * from "./misc" +export * from "./zod" diff --git a/src/util/zod.ts b/src/util/zod.ts new file mode 100644 index 0000000..05b00a9 --- /dev/null +++ b/src/util/zod.ts @@ -0,0 +1,25 @@ +import { mapValues } from "lodash-es" +import { z } from "zod" + + +export function stripZodObjectDefaults< + T extends z.ZodRawShape, + UnknownKeys extends z.UnknownKeysParam, + Catchall extends z.ZodTypeAny, + Output, + Input, +>( + schema: z.ZodObject +) { + return schema.extend( + mapValues(schema.shape, v => + v instanceof z.ZodDefault + ? v.removeDefault() + : v + ) as { + [K in keyof T]: T[K] extends z.ZodDefault + ? NestedDef + : T[K] + } + ) +}