stripZodObjectDefaults work
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Julien Valverdé
2024-03-24 16:35:30 +01:00
parent 9aa25a351d
commit d9493ab198

View File

@@ -11,15 +11,32 @@ export function stripZodObjectDefaults<
>(
schema: z.ZodObject<T, UnknownKeys, Catchall, Output, Input>
) {
return schema.extend(
const s = 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
[K in keyof T]: T[K] extends z.ZodDefault<infer Def>
? Def
: T[K]
}
)
type test = z.input<typeof s>
return s as unknown as z.ZodObject<
{
[K in keyof T]: T[K] extends z.ZodDefault<infer Def>
? Def
: T[K]
},
UnknownKeys,
Catchall,
{
[K in keyof z.output<typeof s>]: T[K] extends z.ZodDefault<infer Def>
? Def
: T[K]
},
Input
>
}