zod 4.2.0-canary.20250826T001214 → 4.2.0-canary.20250827T070334

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zod",
3
- "version": "4.2.0-canary.20250826T001214",
3
+ "version": "4.2.0-canary.20250827T070334",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "author": "Colin McDonnell <zod@colinhacks.com>",
@@ -1,7 +1,34 @@
1
+ import { Validator } from "@seriousme/openapi-schema-validator";
1
2
  import { describe, expect, test } from "vitest";
2
3
  import * as z from "zod/v4";
3
4
  // import * as zCore from "zod/v4/core";
4
5
 
6
+ const openAPI30Validator = new Validator();
7
+ /** @see https://github.com/colinhacks/zod/issues/5147 */
8
+ const validateOpenAPI30Schema = async (zodJSONSchema: Record<string, unknown>): Promise<true> => {
9
+ const res = await openAPI30Validator.validate({
10
+ openapi: "3.0.0",
11
+ info: {
12
+ title: "SampleApi",
13
+ description: "Sample backend service",
14
+ version: "1.0.0",
15
+ },
16
+ components: { schemas: { test: zodJSONSchema } },
17
+ paths: {},
18
+ });
19
+
20
+ if (!res.valid) {
21
+ // `console.error` should make `vitest` trow an unhandled error
22
+ // printing the validation messages in consoles
23
+ console.error(
24
+ `OpenAPI schema is not valid against ${openAPI30Validator.version}`,
25
+ JSON.stringify(res.errors, null, 2)
26
+ );
27
+ }
28
+
29
+ return true;
30
+ };
31
+
5
32
  describe("toJSONSchema", () => {
6
33
  test("primitive types", () => {
7
34
  expect(z.toJSONSchema(z.string())).toMatchInlineSnapshot(`
@@ -552,7 +579,7 @@ describe("toJSONSchema", () => {
552
579
  `);
553
580
  });
554
581
 
555
- test("nullable openapi", () => {
582
+ test("nullable openapi-3.0", () => {
556
583
  expect(z.toJSONSchema(z.string().nullable(), { target: "openapi-3.0" })).toMatchInlineSnapshot(`
557
584
  {
558
585
  "nullable": true,
@@ -561,17 +588,27 @@ describe("toJSONSchema", () => {
561
588
  `);
562
589
  });
563
590
 
564
- test("union with null openapi", () => {
591
+ test("union with null openapi-3.0", () => {
565
592
  const schema = z.union([z.string(), z.null()]);
566
593
  expect(z.toJSONSchema(schema, { target: "openapi-3.0" })).toMatchInlineSnapshot(`
567
594
  {
568
- "nullable": true,
569
- "type": "string",
595
+ "anyOf": [
596
+ {
597
+ "type": "string",
598
+ },
599
+ {
600
+ "enum": [
601
+ null,
602
+ ],
603
+ "nullable": true,
604
+ "type": "string",
605
+ },
606
+ ],
570
607
  }
571
608
  `);
572
609
  });
573
610
 
574
- test("number with exclusive min-max openapi", () => {
611
+ test("number with exclusive min-max openapi-3.0", () => {
575
612
  const schema = z.number().lt(100).gt(1);
576
613
  expect(z.toJSONSchema(schema, { target: "openapi-3.0" })).toMatchInlineSnapshot(`
577
614
  {
@@ -665,7 +702,7 @@ describe("toJSONSchema", () => {
665
702
  `);
666
703
  });
667
704
 
668
- test("record openapi", () => {
705
+ test("record openapi-3.0", () => {
669
706
  const schema = z.record(z.string(), z.boolean());
670
707
  expect(z.toJSONSchema(schema, { target: "openapi-3.0" })).toMatchInlineSnapshot(`
671
708
  {
@@ -716,9 +753,11 @@ describe("toJSONSchema", () => {
716
753
  `);
717
754
  });
718
755
 
719
- test("tuple openapi", () => {
756
+ test("tuple openapi-3.0", () => {
720
757
  const schema = z.tuple([z.string(), z.number()]);
721
- expect(z.toJSONSchema(schema, { target: "openapi-3.0" })).toMatchInlineSnapshot(`
758
+ const jsonSchema = z.toJSONSchema(schema, { target: "openapi-3.0" });
759
+ validateOpenAPI30Schema(jsonSchema);
760
+ expect(jsonSchema).toMatchInlineSnapshot(`
722
761
  {
723
762
  "items": {
724
763
  "anyOf": [
@@ -737,9 +776,11 @@ describe("toJSONSchema", () => {
737
776
  `);
738
777
  });
739
778
 
740
- test("tuple with rest openapi", () => {
779
+ test("tuple with rest openapi-3.0", () => {
741
780
  const schema = z.tuple([z.string(), z.number()]).rest(z.boolean());
742
- expect(z.toJSONSchema(schema, { target: "openapi-3.0" })).toMatchInlineSnapshot(`
781
+ const jsonSchema = z.toJSONSchema(schema, { target: "openapi-3.0" });
782
+ validateOpenAPI30Schema(jsonSchema);
783
+ expect(jsonSchema).toMatchInlineSnapshot(`
743
784
  {
744
785
  "items": {
745
786
  "anyOf": [
@@ -754,7 +795,37 @@ describe("toJSONSchema", () => {
754
795
  },
755
796
  ],
756
797
  },
757
- "minItems": 2,
798
+ "minItems": 3,
799
+ "type": "array",
800
+ }
801
+ `);
802
+ });
803
+
804
+ test("tuple with null openapi-3.0", () => {
805
+ const schema = z.tuple([z.string(), z.number(), z.null()]);
806
+ const jsonSchema = z.toJSONSchema(schema, { target: "openapi-3.0" });
807
+ validateOpenAPI30Schema(jsonSchema);
808
+ expect(jsonSchema).toMatchInlineSnapshot(`
809
+ {
810
+ "items": {
811
+ "anyOf": [
812
+ {
813
+ "type": "string",
814
+ },
815
+ {
816
+ "type": "number",
817
+ },
818
+ {
819
+ "enum": [
820
+ null,
821
+ ],
822
+ "nullable": true,
823
+ "type": "string",
824
+ },
825
+ ],
826
+ },
827
+ "maxItems": 3,
828
+ "minItems": 3,
758
829
  "type": "array",
759
830
  }
760
831
  `);
@@ -236,7 +236,11 @@ export class JSONSchemaGenerator {
236
236
  break;
237
237
  }
238
238
  case "null": {
239
- _json.type = "null";
239
+ if (this.target === "openapi-3.0") {
240
+ _json.type = "string";
241
+ _json.nullable = true;
242
+ _json.enum = [null];
243
+ } else _json.type = "null";
240
244
  break;
241
245
  }
242
246
  case "any": {
@@ -332,18 +336,7 @@ export class JSONSchemaGenerator {
332
336
  path: [...params.path, "anyOf", i],
333
337
  })
334
338
  );
335
- if (this.target === "openapi-3.0") {
336
- const nonNull = options.filter((x) => (x as any).type !== "null");
337
- const hasNull = nonNull.length !== options.length;
338
- if (nonNull.length === 1) {
339
- Object.assign(json, nonNull[0]!);
340
- } else {
341
- json.anyOf = nonNull;
342
- }
343
- if (hasNull) (json as any).nullable = true;
344
- } else {
345
- json.anyOf = options;
346
- }
339
+ json.anyOf = options;
347
340
  break;
348
341
  }
349
342
  case "intersection": {
@@ -393,8 +386,9 @@ export class JSONSchemaGenerator {
393
386
  }
394
387
  } else if (this.target === "openapi-3.0") {
395
388
  json.items = {
396
- anyOf: [...prefixItems],
389
+ anyOf: prefixItems,
397
390
  };
391
+
398
392
  if (rest) {
399
393
  json.items.anyOf!.push(rest);
400
394
  }
@@ -534,9 +528,8 @@ export class JSONSchemaGenerator {
534
528
  case "nullable": {
535
529
  const inner = this.process(def.innerType, params);
536
530
  if (this.target === "openapi-3.0") {
537
- Object.assign(_json, inner);
538
- (_json as any).nullable = true;
539
531
  result.ref = def.innerType;
532
+ _json.nullable = true;
540
533
  } else {
541
534
  _json.anyOf = [inner, { type: "null" }];
542
535
  }
@@ -1,5 +1,5 @@
1
1
  export const version = {
2
2
  major: 4,
3
3
  minor: 1,
4
- patch: 2 as number,
4
+ patch: 3 as number,
5
5
  } as const;
@@ -158,7 +158,13 @@ class JSONSchemaGenerator {
158
158
  break;
159
159
  }
160
160
  case "null": {
161
- _json.type = "null";
161
+ if (this.target === "openapi-3.0") {
162
+ _json.type = "string";
163
+ _json.nullable = true;
164
+ _json.enum = [null];
165
+ }
166
+ else
167
+ _json.type = "null";
162
168
  break;
163
169
  }
164
170
  case "any": {
@@ -250,21 +256,7 @@ class JSONSchemaGenerator {
250
256
  ...params,
251
257
  path: [...params.path, "anyOf", i],
252
258
  }));
253
- if (this.target === "openapi-3.0") {
254
- const nonNull = options.filter((x) => x.type !== "null");
255
- const hasNull = nonNull.length !== options.length;
256
- if (nonNull.length === 1) {
257
- Object.assign(json, nonNull[0]);
258
- }
259
- else {
260
- json.anyOf = nonNull;
261
- }
262
- if (hasNull)
263
- json.nullable = true;
264
- }
265
- else {
266
- json.anyOf = options;
267
- }
259
+ json.anyOf = options;
268
260
  break;
269
261
  }
270
262
  case "intersection": {
@@ -308,7 +300,7 @@ class JSONSchemaGenerator {
308
300
  }
309
301
  else if (this.target === "openapi-3.0") {
310
302
  json.items = {
311
- anyOf: [...prefixItems],
303
+ anyOf: prefixItems,
312
304
  };
313
305
  if (rest) {
314
306
  json.items.anyOf.push(rest);
@@ -461,9 +453,8 @@ class JSONSchemaGenerator {
461
453
  case "nullable": {
462
454
  const inner = this.process(def.innerType, params);
463
455
  if (this.target === "openapi-3.0") {
464
- Object.assign(_json, inner);
465
- _json.nullable = true;
466
456
  result.ref = def.innerType;
457
+ _json.nullable = true;
467
458
  }
468
459
  else {
469
460
  _json.anyOf = [inner, { type: "null" }];
@@ -154,7 +154,13 @@ export class JSONSchemaGenerator {
154
154
  break;
155
155
  }
156
156
  case "null": {
157
- _json.type = "null";
157
+ if (this.target === "openapi-3.0") {
158
+ _json.type = "string";
159
+ _json.nullable = true;
160
+ _json.enum = [null];
161
+ }
162
+ else
163
+ _json.type = "null";
158
164
  break;
159
165
  }
160
166
  case "any": {
@@ -246,21 +252,7 @@ export class JSONSchemaGenerator {
246
252
  ...params,
247
253
  path: [...params.path, "anyOf", i],
248
254
  }));
249
- if (this.target === "openapi-3.0") {
250
- const nonNull = options.filter((x) => x.type !== "null");
251
- const hasNull = nonNull.length !== options.length;
252
- if (nonNull.length === 1) {
253
- Object.assign(json, nonNull[0]);
254
- }
255
- else {
256
- json.anyOf = nonNull;
257
- }
258
- if (hasNull)
259
- json.nullable = true;
260
- }
261
- else {
262
- json.anyOf = options;
263
- }
255
+ json.anyOf = options;
264
256
  break;
265
257
  }
266
258
  case "intersection": {
@@ -304,7 +296,7 @@ export class JSONSchemaGenerator {
304
296
  }
305
297
  else if (this.target === "openapi-3.0") {
306
298
  json.items = {
307
- anyOf: [...prefixItems],
299
+ anyOf: prefixItems,
308
300
  };
309
301
  if (rest) {
310
302
  json.items.anyOf.push(rest);
@@ -457,9 +449,8 @@ export class JSONSchemaGenerator {
457
449
  case "nullable": {
458
450
  const inner = this.process(def.innerType, params);
459
451
  if (this.target === "openapi-3.0") {
460
- Object.assign(_json, inner);
461
- _json.nullable = true;
462
452
  result.ref = def.innerType;
453
+ _json.nullable = true;
463
454
  }
464
455
  else {
465
456
  _json.anyOf = [inner, { type: "null" }];
@@ -4,5 +4,5 @@ exports.version = void 0;
4
4
  exports.version = {
5
5
  major: 4,
6
6
  minor: 1,
7
- patch: 2,
7
+ patch: 3,
8
8
  };
@@ -1,5 +1,5 @@
1
1
  export const version = {
2
2
  major: 4,
3
3
  minor: 1,
4
- patch: 2,
4
+ patch: 3,
5
5
  };