v4: toJSONSchema `.default()` `.catch()` input values considered non optional · Issue #4134 · colinhacks/zod · GitHub
More Web Proxy on the site http://driver.im/
You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Something tricky to deal with in my library when generating JSON Schema from Zod Types was figuring out whether to generate an input (request) type or a output (response) type. I assume that was the intention of the pipes parameter.
The current required implementation only considers the output type.
to a function instead of a getter which takes an optional output defaulted pipes value but that lead me down a rabbit hole... Keen to help you implement this but I understand that this might require quite a bit of tinkering.
console.log(JSON.stringify(z.toJSONSchema(z.object({jobId: z.string().default("foo"),// or .catch()}),{reused: "ref",pipes: "input",}),null,2));
In this scenario jobId should not be marked as required.
The text was updated successfully, but these errors were encountered:
samchungy
changed the title
v4: toJSONSchema .default().catch() considered non optional
v4: toJSONSchema .default().catch() input values considered non optional
Apr 13, 2025
The pipes param has been generalized to io. As you pointed out, input & output types diverge for a number of different schema types (not just pipes). ZodDefault is now reflected as optional with io: "input".
I think .catch() is a little trickier. Arguably the equivalent JSON Schema for ZodCatch is {} (anything) but in practice I don't think it makes sense to throw away all the type information here. The .catch() functionality is Zod-specific and it's safe to assume that in most cases you'll want the input to conform to the schema instead of just defaulting to the catch fallback.
Something tricky to deal with in my library when generating JSON Schema from Zod Types was figuring out whether to generate an
input
(request) type or aoutput
(response) type. I assume that was the intention of thepipes
parameter.The current
required
implementation only considers theoutput
type.zod/packages/core/src/util.ts
Line 538 in 2ade678
I considered changing
optional
zod/packages/core/src/schemas.ts
Line 1480 in 2ade678
to a function instead of a getter which takes an optional
output
defaultedpipes
value but that lead me down a rabbit hole... Keen to help you implement this but I understand that this might require quite a bit of tinkering.In this scenario
jobId
should not be marked as required.The text was updated successfully, but these errors were encountered: