Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e012bd2

Browse filesBrowse files
committed
Improve override docs
1 parent 27ccaf1 commit e012bd2
Copy full SHA for e012bd2

File tree

Expand file treeCollapse file tree

2 files changed

+32
-1
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+32
-1
lines changed
Open diff view settings
Collapse file

‎packages/docs/content/json-schema.mdx‎

Copy file name to clipboardExpand all lines: packages/docs/content/json-schema.mdx
+15-1Lines changed: 15 additions & 1 deletion
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ z.toJSONSchema(User, { reused: "ref" });
398398

399399
### `override`
400400

401-
To define some custom override logic, use `override`. The provided callback has access to the original Zod schema and the default JSON Schema. *This function should dircectly modify `ctx.jsonSchema`.*
401+
To define some custom override logic, use `override`. The provided callback has access to the original Zod schema and the default JSON Schema. *This function should directly modify `ctx.jsonSchema`.*
402402

403403

404404
```ts
@@ -416,6 +416,20 @@ z.toJSONSchema(mySchema, {
416416

417417
Note that unrepresentable types will throw an `Error` before this functions is called. If you are trying to define custom behavior for an unrepresentable type, you'll need to use set the `unrepresentable: "any"` alongside `override`.
418418

419+
```ts
420+
// support z.date() as ISO datetime strings
421+
const result = z.toJSONSchema(z.date(), {
422+
unrepresentable: "any",
423+
override: (ctx) => {
424+
const def = ctx.zodSchema._zod.def;
425+
if(def.type ==="date"){
426+
ctx.jsonSchema.type = "string";
427+
ctx.jsonSchema.format = "date-time";
428+
}
429+
},
430+
});
431+
```
432+
419433
### `io`
420434

421435
Some schema types have different input and output types, e.g. `ZodPipe`, `ZodDefault`, and coerced primitives. By default, the result of `z.toJSONSchema` represents the *output type*; use `"io": "input"` to extract the input type instead.
Collapse file

‎play.ts‎

Copy file name to clipboard
+17Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
11
import * as z from "zod/v4";
22

33
z;
4+
5+
const result = z.toJSONSchema(z.date(), {
6+
unrepresentable: "any",
7+
override: (ctx) => {
8+
const def = ctx.zodSchema._zod.def;
9+
if (def.type === "date") {
10+
ctx.jsonSchema.type = "string";
11+
ctx.jsonSchema.format = "date-time";
12+
}
13+
},
14+
});
15+
/* => {
16+
type: 'string',
17+
format: 'date-time',
18+
'$schema': 'https://json-schema.org/draft/2020-12/schema'
19+
} */
20+
console.dir(result, { depth: null });

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.