This commit is contained in:
@@ -2,3 +2,4 @@ export * from "./class"
|
|||||||
export * from "./effect"
|
export * from "./effect"
|
||||||
export * from "./extend"
|
export * from "./extend"
|
||||||
export * from "./misc"
|
export * from "./misc"
|
||||||
|
export * from "./zod"
|
||||||
|
|||||||
25
src/util/zod.ts
Normal file
25
src/util/zod.ts
Normal 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]
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user