Replies: 1 comment
-
|
Hi @px-d! I'm Dosu and I’m helping the zod team. This is intentional and matches how Zod interprets If you want the field to remain optional in the generated JSON Schema while still having a default, you can pass z.toJSONSchema(schema, { io: 'input' });This preserves the optional status of fields with defaults in the schema output. More details and discussion can be found in this issue and this comment. If this answers your question, feel free to close the issue! To reply, just mention @dosu. How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hey Zod Community,
I use
toJSONSchemaon an object that defines an optional number and a default value for said number. The generated JSON Schema marks the number as required though, is this intended?.catch()generates a default value and the field is not required.Quick example:
{ "$schema": "https://json-schema.org/draft/2020-12/schema", "type": "object", "properties": { "n1": { "default": 1, "type": "number" }, "n2": { "default": 1, "type": "number" } }, "required": [ "n1" ], "additionalProperties": false }Semantically, I’d expect
optional().default()to provide a default value without making the field required, similar to.catch().What’s the intended behavior here?
Thanks for taking the time!
Beta Was this translation helpful? Give feedback.
All reactions