-
-
Couldn't load subscription status.
- Fork 1.7k
Closed
Description
When using the z.toJSONSchema optional fields will be marked as required when using the .default on this field.
Example:
z.toJSONSchema(
z.object({
test: z.string().optional().default('abc')
})
)will result in the following json schema:
'$schema': 'https://json-schema.org/draft/2020-12/schema',
type: 'object',
properties: { test: { default: 'abc', type: 'string' } },
required: [ 'test' ],
additionalProperties: false
}
Even though test is not required, because its marked as optional.
When using optional without default works perfectly fine:
z.toJSONSchema(
z.object({
test: z.string().optional()
})
)will become
{
'$schema': 'https://json-schema.org/draft/2020-12/schema',
type: 'object',
properties: { test: { type: 'string' } },
additionalProperties: false
}
Using zod 3.25.64
Metadata
Metadata
Assignees
Labels
No labels