Z
Zod•2mo ago
Whimsy

Whimsy - is there any function or way to make s...

is there any function or way to make schema fully optional, like literally every property, every nested lazy model? this does work, but is it like correct, is there anything seriously wrong with it or
export function deepPartialify(schema: z.ZodTypeAny): z.ZodTypeAny {
if (schema instanceof ZodObject) {
const newShape: z.ZodRawShape = {};

for (const key in schema.shape) {
const fieldSchema = schema.shape[key];
newShape[key] = ZodOptional.create(deepPartialify(fieldSchema)) as typeof newShape[keyof typeof newShape];
}

return new ZodObject({ ...schema._def, shape: () => newShape });
} else if (schema instanceof ZodArray) {
return new ZodArray({ ...schema._def, type: deepPartialify(schema.element) });
} else if (schema instanceof ZodOptional) {
return ZodOptional.create(deepPartialify(schema.unwrap()));
} else if (schema instanceof ZodNullable) {
return ZodNullable.create(deepPartialify(schema.unwrap()));
} else if (schema instanceof ZodTuple) {
return ZodTuple.create(schema.items.map((item: z.ZodTypeAny) => deepPartialify(item)));
} else if (schema instanceof z.ZodUnion) {
return z.union(schema.options.map((option: z.ZodTypeAny) => deepPartialify(option)));
} else if (schema instanceof ZodLazy) {
return ZodLazy.create(() => deepPartialify(schema._def.getter()));
} else {
return schema;
}
}
export function deepPartialify(schema: z.ZodTypeAny): z.ZodTypeAny {
if (schema instanceof ZodObject) {
const newShape: z.ZodRawShape = {};

for (const key in schema.shape) {
const fieldSchema = schema.shape[key];
newShape[key] = ZodOptional.create(deepPartialify(fieldSchema)) as typeof newShape[keyof typeof newShape];
}

return new ZodObject({ ...schema._def, shape: () => newShape });
} else if (schema instanceof ZodArray) {
return new ZodArray({ ...schema._def, type: deepPartialify(schema.element) });
} else if (schema instanceof ZodOptional) {
return ZodOptional.create(deepPartialify(schema.unwrap()));
} else if (schema instanceof ZodNullable) {
return ZodNullable.create(deepPartialify(schema.unwrap()));
} else if (schema instanceof ZodTuple) {
return ZodTuple.create(schema.items.map((item: z.ZodTypeAny) => deepPartialify(item)));
} else if (schema instanceof z.ZodUnion) {
return z.union(schema.options.map((option: z.ZodTypeAny) => deepPartialify(option)));
} else if (schema instanceof ZodLazy) {
return ZodLazy.create(() => deepPartialify(schema._def.getter()));
} else {
return schema;
}
}
10 Replies
Scott Trinh
Scott Trinh•2mo ago
I think this is roughly the right approach (missing ZodDiscriminatedUnion, maybe others?)
Whimsy
WhimsyOP•2mo ago
and zodeffects class for .refine stuff
Unknown User
Unknown User•2mo ago
Message Not Public
Sign In & Join Server To View
Scott Trinh
Scott Trinh•2mo ago
I don't think deepPartial works on lazy schemas
Unknown User
Unknown User•2mo ago
Message Not Public
Sign In & Join Server To View
Scott Trinh
Scott Trinh•2mo ago
No prob! Here's the docs: https://zod.dev/?id=recursive-types
GitHub
TypeScript-first schema validation with static type inference
TypeScript-first schema validation with static type inference
Unknown User
Unknown User•2mo ago
Message Not Public
Sign In & Join Server To View
Scott Trinh
Scott Trinh•2mo ago
Yeah, that's right. You can use it for anything you want (similar to z.custom which is undocumented), but the only good use case is recursive types.
Unknown User
Unknown User•2mo ago
Message Not Public
Sign In & Join Server To View
Scott Trinh
Scott Trinh•2mo ago
ahh, nice, that's newish, I think? Or maybe I couldn't find it last time I looked 😂
Want results from more Discord servers?
Add your server