stripZodSchemaDefaults util
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Julien Valverdé
2024-03-15 19:18:10 +01:00
parent 07fb50d21d
commit d7c8fd1a2d
2 changed files with 26 additions and 0 deletions

View File

@@ -2,3 +2,4 @@ export * from "./class"
export * from "./effect"
export * from "./extend"
export * from "./misc"
export * from "./zod"

25
src/util/zod.ts Normal file
View File

@@ -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<T, UnknownKeys, Catchall, Output, Input>
) {
return schema.extend(
mapValues(schema.shape, v =>
v instanceof z.ZodDefault
? v.removeDefault()
: v
) as {
[K in keyof T]: T[K] extends z.ZodDefault<infer NestedDef>
? NestedDef
: T[K]
}
)
}