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 1fa8689

Browse filesBrowse files
committed
Move to top
1 parent 6b13cc9 commit 1fa8689
Copy full SHA for 1fa8689

File tree

Expand file treeCollapse file tree

2 files changed

+13
-50
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+13
-50
lines changed
Open diff view settings
Collapse file

‎packages/zod/src/v4/core/to-json-schema.ts‎

Copy file name to clipboardExpand all lines: packages/zod/src/v4/core/to-json-schema.ts
+10-9Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -709,7 +709,16 @@ export class JSONSchemaGenerator {
709709
flattenRef(entry[0], { target: this.target });
710710
}
711711

712-
const result = { ...root.def };
712+
const result: JSONSchema.BaseSchema = {};
713+
if (this.target === "draft-2020-12") {
714+
result.$schema = "https://json-schema.org/draft/2020-12/schema";
715+
} else if (this.target === "draft-7") {
716+
result.$schema = "http://json-schema.org/draft-07/schema#";
717+
} else {
718+
console.warn(`Invalid target: ${this.target}`);
719+
}
720+
721+
Object.assign(result, root.def);
713722

714723
const defs: JSONSchema.BaseSchema["$defs"] = params.external?.defs ?? {};
715724
for (const entry of this.seen.entries()) {
@@ -728,14 +737,6 @@ export class JSONSchemaGenerator {
728737
}
729738
}
730739

731-
if (this.target === "draft-2020-12") {
732-
result.$schema = "https://json-schema.org/draft/2020-12/schema";
733-
} else if (this.target === "draft-7") {
734-
result.$schema = "http://json-schema.org/draft-07/schema#";
735-
} else {
736-
console.warn(`Invalid target: ${this.target}`);
737-
}
738-
739740
try {
740741
// this "finalizes" this schema and ensures all cycles are removed
741742
// each call to .emit() is functionally independent
Collapse file

‎play.ts‎

Copy file name to clipboard
+3-41Lines changed: 3 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,5 @@
1-
import { z } from "zod/v4-mini";
1+
import { z } from "zod/v4";
22

3-
// const zval = z
4-
// .string()
5-
// .check((ctx) => {
6-
// console.log(`Checking value: ${ctx.value}`);
7-
// if (ctx.value !== "expected") {
8-
// ctx.issues.push({
9-
// message: `Value must be 'expected', got '${ctx.value}'`,
10-
// input: ctx.value,
11-
// code: "custom",
12-
// fatal: true,
13-
// // continue: false,
14-
// // fa
15-
// });
16-
// }
17-
// })
18-
// .transform((val) => {
19-
// console.log(`Transforming value: ${val}`);
20-
// return val.toUpperCase();
21-
// })
22-
// .prefault("unexpected");
23-
24-
// const obj = z.object({
25-
// test: zval,
26-
// });
27-
28-
// console.log("NESTED");
29-
// console.log(obj.safeParse({ test: undefined }, { jitless: true })); // check runs, doesn't throw, transform not run
30-
31-
// console.log("\n\nTOP LEVEL");
32-
// console.log(zval.safeParse(undefined, { jitless: true })); // check runs, throws
33-
34-
const Shared = z.object({
35-
label: z.string(),
36-
description: z.string(),
3+
console.dir(z.toJSONSchema(z.string().startsWith("hello").includes("cruel").endsWith("world").regex(/stuff/)), {
4+
depth: null,
375
});
38-
39-
const DU = z.discriminatedUnion("type", [
40-
z.extend(Shared, { type: z.literal("a"), value: z.number() }),
41-
z.extend(Shared, { type: z.literal("b"), value: z.string() }),
42-
z.extend(Shared, { type: z.literal("c"), value: z.boolean() }),
43-
]);

0 commit comments

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