Open robogeek opened 10 months ago
I think i had same problem.
You CANT use .optional()
with .default()
because it not gonna generate them + default .default()
make them optional.
To not get error which tell you about needed input which is default generated - so optional
use z.input<>
instead z.infer<>
.
z.infer it's more like final form when z.input its correctness before parsing.
I hope it gonna help.
Consider these three schema objects -- all generated by
ts-to-zod
from TypeScript source.In
durationSchema
there is a default value, which is a Duration string for zero seconds.Calling
intervalPeriodSchema.parse
with an object that does not include therandomizeStart
field, I get the following:What I'm reasoning is that
intervalPeriodSchema.parse
callsdurationSchema.parse
which notices the missing field, notices the.optional
, and therefore does not call intodurationSchema.parse
, and therefore the.default
value is not substituted.I have generated the same schema using a different tool -
openapi-to-zod
- which generates the following schema, and in that case the default value shows up.