Skip to content

Commit e012bd2

Browse files
committed
Improve override docs
1 parent 27ccaf1 commit e012bd2

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

packages/docs/content/json-schema.mdx

Lines changed: 15 additions & 1 deletion
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.

play.ts

Lines 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)