Replies: 2 comments
-
|
Some more examples: // Standard JSON Schema formats
z.toJSONSchema(z.string().email()) // { "type": "string", "format": "email" }
z.toJSONSchema(z.string().url()) // { "type": "string", "format": "uri" }
z.toJSONSchema(z.string().uuid()) // { "type": "string", "format": "uuid" }
z.toJSONSchema(z.string().datetime()) // { "type": "string", "format": "date-time" }
z.toJSONSchema(z.string().ip()) // { "type": "string", "format": "ipv4" }
// Can use format for semantic meaning
z.toJSONSchema(z.date()) // { "type": "string", "format": "date" }
z.toJSONSchema(z.bigint()) // { "type": "string", "format": "bigint" }
z.toJSONSchema(z.int64()) // { "type": "integer", "format": "int64" } |
Beta Was this translation helpful? Give feedback.
0 replies
-
|
If you carefully read the documentation, there is an // support z.date() as ISO datetime strings
const result = z.toJSONSchema(z.date(), {
unrepresentable: "any",
override: (ctx) => {
const def = ctx.zodSchema._zod.def;
if(def.type ==="date"){
ctx.jsonSchema.type = "string";
ctx.jsonSchema.format = "date-time";
}
},
}); |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
https://zod.dev/json-schema#unrepresentable
By default, Zod will throw an error if any of these are encountered.
You can change this behavior by setting the unrepresentable option to "any". This will convert any unrepresentable types to {} (the equivalent of unknown in JSON Schema).
The chosen default to
{}is overly conservative in my opinion. Some types can be represented as{ type: "string", format: "date" }quite nicely.JSON Schema supports the
formatproperty. It's used to provide semantic validation for strings and is defined in the JSON Schema specification.The format property allows you to specify that a string should conform to a particular format. Common formats include:
This could be a way forward
{ "type": "string", "format": "bigint" }But to be honest I'd even go with some sensible defaults:
z.functionis true unrepresentablez.dateis better with formatBeta Was this translation helpful? Give feedback.
All reactions