Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
223 changes: 221 additions & 2 deletions packages/zod/src/v4/classic/tests/to-json-schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -817,7 +817,6 @@ describe("toJSONSchema", () => {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"allOf": [
{
"additionalProperties": false,
"properties": {
"name": {
"type": "string",
Expand All @@ -829,7 +828,6 @@ describe("toJSONSchema", () => {
"type": "object",
},
{
"additionalProperties": false,
"properties": {
"age": {
"type": "number",
Expand All @@ -841,6 +839,7 @@ describe("toJSONSchema", () => {
"type": "object",
},
],
"unevaluatedProperties": false,
}
`);
});
Expand Down Expand Up @@ -1040,6 +1039,220 @@ describe("toJSONSchema", () => {
`);
});

test("intersection with discriminated union", () => {
const base = z.object({ name: z.string() });
const variant = z.discriminatedUnion("type", [
z.object({ type: z.literal("a"), value: z.string() }),
z.object({ type: z.literal("b"), count: z.number() }),
]);
const schema = base.and(variant);

expect(z.toJSONSchema(schema)).toMatchInlineSnapshot(`
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"allOf": [
{
"properties": {
"name": {
"type": "string",
},
},
"required": [
"name",
],
"type": "object",
},
{
"oneOf": [
{
"properties": {
"type": {
"const": "a",
"type": "string",
},
"value": {
"type": "string",
},
},
"required": [
"type",
"value",
],
"type": "object",
},
{
"properties": {
"count": {
"type": "number",
},
"type": {
"const": "b",
"type": "string",
},
},
"required": [
"type",
"count",
],
"type": "object",
},
],
},
],
"unevaluatedProperties": false,
}
`);
});

test("intersection of strict and loose objects uses placeholders on draft-2020-12", () => {
const schema = z.looseObject({ name: z.string() }).and(z.object({ value: z.number() }));

expect(z.toJSONSchema(schema)).toMatchInlineSnapshot(`
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"allOf": [
{
"additionalProperties": {},
"properties": {
"name": {
"type": "string",
},
},
"required": [
"name",
],
"type": "object",
},
{
"additionalProperties": false,
"properties": {
"name": {},
"value": {
"type": "number",
},
},
"required": [
"value",
],
"type": "object",
},
],
}
`);
});

test("intersection of two objects on draft-07", () => {
const schema = z.intersection(z.object({ name: z.string() }), z.object({ age: z.number() }));

expect(z.toJSONSchema(schema, { target: "draft-07" })).toMatchInlineSnapshot(`
{
"$schema": "http://json-schema.org/draft-07/schema#",
"allOf": [
{
"additionalProperties": false,
"properties": {
"age": {},
"name": {
"type": "string",
},
},
"required": [
"name",
],
"type": "object",
},
{
"additionalProperties": false,
"properties": {
"age": {
"type": "number",
},
"name": {},
},
"required": [
"age",
],
"type": "object",
},
],
}
`);
});

test("intersection with discriminated union on draft-07", () => {
const base = z.object({ name: z.string() });
const variant = z.discriminatedUnion("type", [
z.object({ type: z.literal("a"), value: z.string() }),
z.object({ type: z.literal("b"), count: z.number() }),
]);
const schema = base.and(variant);

expect(z.toJSONSchema(schema, { target: "draft-07" })).toMatchInlineSnapshot(`
{
"$schema": "http://json-schema.org/draft-07/schema#",
"allOf": [
{
"additionalProperties": false,
"properties": {
"count": {},
"name": {
"type": "string",
},
"type": {},
"value": {},
},
"required": [
"name",
],
"type": "object",
},
{
"oneOf": [
{
"additionalProperties": false,
"properties": {
"count": {},
"name": {},
"type": {
"const": "a",
"type": "string",
},
"value": {
"type": "string",
},
},
"required": [
"type",
"value",
],
"type": "object",
},
{
"additionalProperties": false,
"properties": {
"count": {
"type": "number",
},
"name": {},
"type": {
"const": "b",
"type": "string",
},
"value": {},
},
"required": [
"type",
"count",
],
"type": "object",
},
],
},
],
}
`);
});

test("tuple", () => {
const schema = z.tuple([z.string(), z.number()]);
expect(z.toJSONSchema(schema)).toMatchInlineSnapshot(`
Expand Down Expand Up @@ -2852,9 +3065,11 @@ test("flatten simple intersections", () => {
{
"additionalProperties": false,
"properties": {
"testBool": {},
"testNum": {
"type": "number",
},
"testStr": {},
},
"required": [
"testNum",
Expand All @@ -2864,6 +3079,8 @@ test("flatten simple intersections", () => {
{
"additionalProperties": false,
"properties": {
"testBool": {},
"testNum": {},
"testStr": {
"type": "string",
},
Expand All @@ -2879,6 +3096,8 @@ test("flatten simple intersections", () => {
"testBool": {
"type": "boolean",
},
"testNum": {},
"testStr": {},
},
"required": [
"testBool",
Expand Down
Loading