zod 3.25.4 → 3.25.6

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.
Files changed (45) hide show
  1. package/dist/commonjs/v3/types.d.ts +1 -1
  2. package/dist/commonjs/v4/classic/errors.d.ts +1 -1
  3. package/dist/commonjs/v4/classic/errors.js +1 -0
  4. package/dist/commonjs/v4/classic/parse.d.ts +1 -1
  5. package/dist/commonjs/v4/classic/parse.js +2 -2
  6. package/dist/commonjs/v4/core/errors.d.ts +1 -1
  7. package/dist/commonjs/v4/core/errors.js +44 -0
  8. package/dist/commonjs/v4/core/parse.js +2 -2
  9. package/dist/esm/v3/types.d.ts +1 -1
  10. package/dist/esm/v4/classic/errors.d.ts +1 -1
  11. package/dist/esm/v4/classic/errors.js +1 -0
  12. package/dist/esm/v4/classic/parse.d.ts +1 -1
  13. package/dist/esm/v4/classic/parse.js +3 -3
  14. package/dist/esm/v4/core/errors.d.ts +1 -1
  15. package/dist/esm/v4/core/errors.js +11 -0
  16. package/dist/esm/v4/core/parse.js +2 -2
  17. package/package.json +1 -1
  18. package/src/v3/types.ts +1 -1
  19. package/src/v4/classic/errors.ts +2 -1
  20. package/src/v4/classic/parse.ts +3 -3
  21. package/src/v4/classic/tests/array.test.ts +42 -46
  22. package/src/v4/classic/tests/async-refinements.test.ts +14 -18
  23. package/src/v4/classic/tests/catch.test.ts +10 -12
  24. package/src/v4/classic/tests/discriminated-unions.test.ts +77 -87
  25. package/src/v4/classic/tests/enum.test.ts +33 -39
  26. package/src/v4/classic/tests/error-utils.test.ts +25 -27
  27. package/src/v4/classic/tests/error.test.ts +115 -128
  28. package/src/v4/classic/tests/literal.test.ts +10 -12
  29. package/src/v4/classic/tests/map.test.ts +60 -68
  30. package/src/v4/classic/tests/nested-refine.test.ts +68 -72
  31. package/src/v4/classic/tests/nonoptional.test.ts +36 -44
  32. package/src/v4/classic/tests/number.test.ts +36 -44
  33. package/src/v4/classic/tests/pipe.test.ts +18 -22
  34. package/src/v4/classic/tests/preprocess.test.ts +91 -107
  35. package/src/v4/classic/tests/primitive.test.ts +9 -11
  36. package/src/v4/classic/tests/record.test.ts +101 -121
  37. package/src/v4/classic/tests/set.test.ts +30 -36
  38. package/src/v4/classic/tests/string.test.ts +50 -60
  39. package/src/v4/classic/tests/template-literal.test.ts +45 -55
  40. package/src/v4/classic/tests/transform.test.ts +36 -46
  41. package/src/v4/classic/tests/tuple.test.ts +54 -66
  42. package/src/v4/core/errors.ts +12 -1
  43. package/src/v4/core/parse.ts +2 -2
  44. package/src/v4/core/tests/index.test.ts +42 -1
  45. package/src/v4/mini/tests/error.test.ts +1 -1
@@ -34,35 +34,31 @@ test("enum exhaustiveness", () => {
34
34
 
35
35
  expect(schema.safeParse({ Tuna: "asdf", Salmon: "asdf", Trout: "asdf" })).toMatchInlineSnapshot(`
36
36
  {
37
- "error": ZodError {
38
- "issues": [
39
- {
40
- "code": "unrecognized_keys",
41
- "keys": [
42
- "Trout",
43
- ],
44
- "message": "Unrecognized key: "Trout"",
45
- "path": [],
46
- },
37
+ "error": [ZodError: [
38
+ {
39
+ "code": "unrecognized_keys",
40
+ "keys": [
41
+ "Trout"
47
42
  ],
48
- },
43
+ "path": [],
44
+ "message": "Unrecognized key: \\"Trout\\""
45
+ }
46
+ ]],
49
47
  "success": false,
50
48
  }
51
49
  `);
52
50
  expect(schema.safeParse({ Tuna: "asdf" })).toMatchInlineSnapshot(`
53
51
  {
54
- "error": ZodError {
55
- "issues": [
56
- {
57
- "code": "invalid_type",
58
- "expected": "string",
59
- "message": "Invalid input: expected string, received undefined",
60
- "path": [
61
- "Salmon",
62
- ],
63
- },
52
+ "error": [ZodError: [
53
+ {
54
+ "expected": "string",
55
+ "code": "invalid_type",
56
+ "path": [
57
+ "Salmon"
64
58
  ],
65
- },
59
+ "message": "Invalid input: expected string, received undefined"
60
+ }
61
+ ]],
66
62
  "success": false,
67
63
  }
68
64
  `);
@@ -77,35 +73,31 @@ test("literal exhaustiveness", () => {
77
73
 
78
74
  expect(schema.safeParse({ Tuna: "asdf", Salmon: "asdf", Trout: "asdf" })).toMatchInlineSnapshot(`
79
75
  {
80
- "error": ZodError {
81
- "issues": [
82
- {
83
- "code": "unrecognized_keys",
84
- "keys": [
85
- "Trout",
86
- ],
87
- "message": "Unrecognized key: "Trout"",
88
- "path": [],
89
- },
76
+ "error": [ZodError: [
77
+ {
78
+ "code": "unrecognized_keys",
79
+ "keys": [
80
+ "Trout"
90
81
  ],
91
- },
82
+ "path": [],
83
+ "message": "Unrecognized key: \\"Trout\\""
84
+ }
85
+ ]],
92
86
  "success": false,
93
87
  }
94
88
  `);
95
89
  expect(schema.safeParse({ Tuna: "asdf" })).toMatchInlineSnapshot(`
96
90
  {
97
- "error": ZodError {
98
- "issues": [
99
- {
100
- "code": "invalid_type",
101
- "expected": "string",
102
- "message": "Invalid input: expected string, received undefined",
103
- "path": [
104
- "Salmon",
105
- ],
106
- },
91
+ "error": [ZodError: [
92
+ {
93
+ "expected": "string",
94
+ "code": "invalid_type",
95
+ "path": [
96
+ "Salmon"
107
97
  ],
108
- },
98
+ "message": "Invalid input: expected string, received undefined"
99
+ }
100
+ ]],
109
101
  "success": false,
110
102
  }
111
103
  `);
@@ -120,35 +112,31 @@ test("pipe exhaustiveness", () => {
120
112
 
121
113
  expect(schema.safeParse({ Tuna: "asdf", Salmon: "asdf", Trout: "asdf" })).toMatchInlineSnapshot(`
122
114
  {
123
- "error": ZodError {
124
- "issues": [
125
- {
126
- "code": "unrecognized_keys",
127
- "keys": [
128
- "Trout",
129
- ],
130
- "message": "Unrecognized key: "Trout"",
131
- "path": [],
132
- },
115
+ "error": [ZodError: [
116
+ {
117
+ "code": "unrecognized_keys",
118
+ "keys": [
119
+ "Trout"
133
120
  ],
134
- },
121
+ "path": [],
122
+ "message": "Unrecognized key: \\"Trout\\""
123
+ }
124
+ ]],
135
125
  "success": false,
136
126
  }
137
127
  `);
138
128
  expect(schema.safeParse({ Tuna: "asdf" })).toMatchInlineSnapshot(`
139
129
  {
140
- "error": ZodError {
141
- "issues": [
142
- {
143
- "code": "invalid_type",
144
- "expected": "string",
145
- "message": "Invalid input: expected string, received undefined",
146
- "path": [
147
- "Salmon",
148
- ],
149
- },
130
+ "error": [ZodError: [
131
+ {
132
+ "expected": "string",
133
+ "code": "invalid_type",
134
+ "path": [
135
+ "Salmon"
150
136
  ],
151
- },
137
+ "message": "Invalid input: expected string, received undefined"
138
+ }
139
+ ]],
152
140
  "success": false,
153
141
  }
154
142
  `);
@@ -163,35 +151,31 @@ test("union exhaustiveness", () => {
163
151
 
164
152
  expect(schema.safeParse({ Tuna: "asdf", Salmon: "asdf", Trout: "asdf" })).toMatchInlineSnapshot(`
165
153
  {
166
- "error": ZodError {
167
- "issues": [
168
- {
169
- "code": "unrecognized_keys",
170
- "keys": [
171
- "Trout",
172
- ],
173
- "message": "Unrecognized key: "Trout"",
174
- "path": [],
175
- },
154
+ "error": [ZodError: [
155
+ {
156
+ "code": "unrecognized_keys",
157
+ "keys": [
158
+ "Trout"
176
159
  ],
177
- },
160
+ "path": [],
161
+ "message": "Unrecognized key: \\"Trout\\""
162
+ }
163
+ ]],
178
164
  "success": false,
179
165
  }
180
166
  `);
181
167
  expect(schema.safeParse({ Tuna: "asdf" })).toMatchInlineSnapshot(`
182
168
  {
183
- "error": ZodError {
184
- "issues": [
185
- {
186
- "code": "invalid_type",
187
- "expected": "string",
188
- "message": "Invalid input: expected string, received undefined",
189
- "path": [
190
- "Salmon",
191
- ],
192
- },
169
+ "error": [ZodError: [
170
+ {
171
+ "expected": "string",
172
+ "code": "invalid_type",
173
+ "path": [
174
+ "Salmon"
193
175
  ],
194
- },
176
+ "message": "Invalid input: expected string, received undefined"
177
+ }
178
+ ]],
195
179
  "success": false,
196
180
  }
197
181
  `);
@@ -208,16 +192,14 @@ test("string record parse - pass", () => {
208
192
  expect(schema.safeParse({ asdf: 1234 }).success).toEqual(false);
209
193
  expect(schema.safeParse("asdf")).toMatchInlineSnapshot(`
210
194
  {
211
- "error": ZodError {
212
- "issues": [
213
- {
214
- "code": "invalid_type",
215
- "expected": "record",
216
- "message": "Invalid input: expected record, received string",
217
- "path": [],
218
- },
219
- ],
220
- },
195
+ "error": [ZodError: [
196
+ {
197
+ "expected": "record",
198
+ "code": "invalid_type",
199
+ "path": [],
200
+ "message": "Invalid input: expected record, received string"
201
+ }
202
+ ]],
221
203
  "success": false,
222
204
  }
223
205
  `);
@@ -325,28 +307,26 @@ test("async parsing", async () => {
325
307
  const result = await schema.safeParseAsync(data);
326
308
  expect(result.success).toEqual(false);
327
309
  expect(result.error).toMatchInlineSnapshot(`
328
- ZodError {
329
- "issues": [
330
- {
331
- "code": "custom",
332
- "message": "Invalid input",
333
- "path": [
334
- "foo",
335
- ],
336
- },
337
- {
338
- "code": "custom",
339
- "message": "Invalid input",
340
- "path": [
341
- "baz",
342
- ],
343
- },
344
- {
345
- "code": "custom",
346
- "message": "Invalid input",
347
- "path": [],
348
- },
349
- ],
350
- }
310
+ [ZodError: [
311
+ {
312
+ "code": "custom",
313
+ "path": [
314
+ "foo"
315
+ ],
316
+ "message": "Invalid input"
317
+ },
318
+ {
319
+ "code": "custom",
320
+ "path": [
321
+ "baz"
322
+ ],
323
+ "message": "Invalid input"
324
+ },
325
+ {
326
+ "code": "custom",
327
+ "path": [],
328
+ "message": "Invalid input"
329
+ }
330
+ ]]
351
331
  `);
352
332
  });
@@ -94,16 +94,14 @@ test("throws when a Map is given", () => {
94
94
  const result = stringSet.safeParse(new Map([]));
95
95
  expect(result.success).toEqual(false);
96
96
  expect(result.error).toMatchInlineSnapshot(`
97
- ZodError {
98
- "issues": [
99
- {
100
- "code": "invalid_type",
101
- "expected": "set",
102
- "message": "Invalid input: expected set, received Map",
103
- "path": [],
104
- },
105
- ],
106
- }
97
+ [ZodError: [
98
+ {
99
+ "expected": "set",
100
+ "code": "invalid_type",
101
+ "path": [],
102
+ "message": "Invalid input: expected set, received Map"
103
+ }
104
+ ]]
107
105
  `);
108
106
  });
109
107
 
@@ -112,16 +110,14 @@ test("throws when the given set has invalid input", () => {
112
110
  expect(result.success).toEqual(false);
113
111
  expect(result.error!.issues.length).toEqual(1);
114
112
  expect(result.error).toMatchInlineSnapshot(`
115
- ZodError {
116
- "issues": [
117
- {
118
- "code": "invalid_type",
119
- "expected": "string",
120
- "message": "Invalid input: expected string, received symbol",
121
- "path": [],
122
- },
123
- ],
124
- }
113
+ [ZodError: [
114
+ {
115
+ "expected": "string",
116
+ "code": "invalid_type",
117
+ "path": [],
118
+ "message": "Invalid input: expected string, received symbol"
119
+ }
120
+ ]]
125
121
  `);
126
122
  });
127
123
 
@@ -130,22 +126,20 @@ test("throws when the given set has multiple invalid entries", () => {
130
126
  expect(result.success).toEqual(false);
131
127
  expect(result.error!.issues.length).toEqual(2);
132
128
  expect(result.error).toMatchInlineSnapshot(`
133
- ZodError {
134
- "issues": [
135
- {
136
- "code": "invalid_type",
137
- "expected": "string",
138
- "message": "Invalid input: expected string, received number",
139
- "path": [],
140
- },
141
- {
142
- "code": "invalid_type",
143
- "expected": "string",
144
- "message": "Invalid input: expected string, received number",
145
- "path": [],
146
- },
147
- ],
148
- }
129
+ [ZodError: [
130
+ {
131
+ "expected": "string",
132
+ "code": "invalid_type",
133
+ "path": [],
134
+ "message": "Invalid input: expected string, received number"
135
+ },
136
+ {
137
+ "expected": "string",
138
+ "code": "invalid_type",
139
+ "path": [],
140
+ "message": "Invalid input: expected string, received number"
141
+ }
142
+ ]]
149
143
  `);
150
144
  });
151
145
 
@@ -393,18 +393,16 @@ test("nanoid", () => {
393
393
 
394
394
  expect(result.error!.issues[0].message).toEqual("custom error");
395
395
  expect(result.error).toMatchInlineSnapshot(`
396
- ZodError {
397
- "issues": [
398
- {
399
- "code": "invalid_format",
400
- "format": "nanoid",
401
- "message": "custom error",
402
- "origin": "string",
403
- "path": [],
404
- "pattern": "/^[a-zA-Z0-9_-]{21}$/",
405
- },
406
- ],
407
- }
396
+ [ZodError: [
397
+ {
398
+ "origin": "string",
399
+ "code": "invalid_format",
400
+ "format": "nanoid",
401
+ "pattern": "/^[a-zA-Z0-9_-]{21}$/",
402
+ "path": [],
403
+ "message": "custom error"
404
+ }
405
+ ]]
408
406
  `);
409
407
  });
410
408
 
@@ -416,18 +414,16 @@ test("bad nanoid", () => {
416
414
 
417
415
  expect(result.error!.issues[0].message).toEqual("custom error");
418
416
  expect(result.error).toMatchInlineSnapshot(`
419
- ZodError {
420
- "issues": [
421
- {
422
- "code": "invalid_format",
423
- "format": "nanoid",
424
- "message": "custom error",
425
- "origin": "string",
426
- "path": [],
427
- "pattern": "/^[a-zA-Z0-9_-]{21}$/",
428
- },
429
- ],
430
- }
417
+ [ZodError: [
418
+ {
419
+ "origin": "string",
420
+ "code": "invalid_format",
421
+ "format": "nanoid",
422
+ "pattern": "/^[a-zA-Z0-9_-]{21}$/",
423
+ "path": [],
424
+ "message": "custom error"
425
+ }
426
+ ]]
431
427
  `);
432
428
  });
433
429
 
@@ -501,18 +497,16 @@ test("cuid", () => {
501
497
 
502
498
  expect(result.error!.issues[0].message).toEqual("Invalid cuid");
503
499
  expect(result.error).toMatchInlineSnapshot(`
504
- ZodError {
505
- "issues": [
506
- {
507
- "code": "invalid_format",
508
- "format": "cuid",
509
- "message": "Invalid cuid",
510
- "origin": "string",
511
- "path": [],
512
- "pattern": "/^[cC][^\\s-]{8,}$/",
513
- },
514
- ],
515
- }
500
+ [ZodError: [
501
+ {
502
+ "origin": "string",
503
+ "code": "invalid_format",
504
+ "format": "cuid",
505
+ "pattern": "/^[cC][^\\\\s-]{8,}$/",
506
+ "path": [],
507
+ "message": "Invalid cuid"
508
+ }
509
+ ]]
516
510
  `);
517
511
  });
518
512
 
@@ -552,18 +546,16 @@ test("ulid", () => {
552
546
 
553
547
  expect(result.error!.issues[0].message).toEqual("Invalid ULID");
554
548
  expect(result.error).toMatchInlineSnapshot(`
555
- ZodError {
556
- "issues": [
557
- {
558
- "code": "invalid_format",
559
- "format": "ulid",
560
- "message": "Invalid ULID",
561
- "origin": "string",
562
- "path": [],
563
- "pattern": "/^[0-9A-HJKMNP-TV-Za-hjkmnp-tv-z]{26}$/",
564
- },
565
- ],
566
- }
549
+ [ZodError: [
550
+ {
551
+ "origin": "string",
552
+ "code": "invalid_format",
553
+ "format": "ulid",
554
+ "pattern": "/^[0-9A-HJKMNP-TV-Za-hjkmnp-tv-z]{26}$/",
555
+ "path": [],
556
+ "message": "Invalid ULID"
557
+ }
558
+ ]]
567
559
  `);
568
560
  });
569
561
 
@@ -575,18 +567,16 @@ test("xid", () => {
575
567
 
576
568
  expect(result.error!.issues[0].message).toEqual("Invalid XID");
577
569
  expect(result.error).toMatchInlineSnapshot(`
578
- ZodError {
579
- "issues": [
580
- {
581
- "code": "invalid_format",
582
- "format": "xid",
583
- "message": "Invalid XID",
584
- "origin": "string",
585
- "path": [],
586
- "pattern": "/^[0-9a-vA-V]{20}$/",
587
- },
588
- ],
589
- }
570
+ [ZodError: [
571
+ {
572
+ "origin": "string",
573
+ "code": "invalid_format",
574
+ "format": "xid",
575
+ "pattern": "/^[0-9a-vA-V]{20}$/",
576
+ "path": [],
577
+ "message": "Invalid XID"
578
+ }
579
+ ]]
590
580
  `);
591
581
  });
592
582
 
@@ -686,82 +686,72 @@ test("template literal parsing - failure - complex cases", () => {
686
686
  test("template literal parsing - failure - issue format", () => {
687
687
  expect(anotherNull.safeParse("1null")).toMatchInlineSnapshot(`
688
688
  {
689
- "error": ZodError {
690
- "issues": [
691
- {
692
- "code": "invalid_format",
693
- "format": "template_literal",
694
- "message": "Invalid input",
695
- "path": [],
696
- "pattern": "^null$",
697
- },
698
- ],
699
- },
689
+ "error": [ZodError: [
690
+ {
691
+ "code": "invalid_format",
692
+ "format": "template_literal",
693
+ "pattern": "^null$",
694
+ "path": [],
695
+ "message": "Invalid input"
696
+ }
697
+ ]],
700
698
  "success": false,
701
699
  }
702
700
  `);
703
701
  expect(cuidZZZ.safeParse("1cjld2cyuq0000t3rmniod1foyZZZ")).toMatchInlineSnapshot(`
704
702
  {
705
- "error": ZodError {
706
- "issues": [
707
- {
708
- "code": "invalid_format",
709
- "format": "template_literal",
710
- "message": "Invalid input",
711
- "path": [],
712
- "pattern": "^[cC][^\\s-]{8,}ZZZ$",
713
- },
714
- ],
715
- },
703
+ "error": [ZodError: [
704
+ {
705
+ "code": "invalid_format",
706
+ "format": "template_literal",
707
+ "pattern": "^[cC][^\\\\s-]{8,}ZZZ$",
708
+ "path": [],
709
+ "message": "Invalid input"
710
+ }
711
+ ]],
716
712
  "success": false,
717
713
  }
718
714
  `);
719
715
  expect(stringMin5Max10.safeParse("1234")).toMatchInlineSnapshot(`
720
716
  {
721
- "error": ZodError {
722
- "issues": [
723
- {
724
- "code": "invalid_format",
725
- "format": "template_literal",
726
- "message": "Invalid input",
727
- "path": [],
728
- "pattern": "^[\\s\\S]{5,10}$",
729
- },
730
- ],
731
- },
717
+ "error": [ZodError: [
718
+ {
719
+ "code": "invalid_format",
720
+ "format": "template_literal",
721
+ "pattern": "^[\\\\s\\\\S]{5,10}$",
722
+ "path": [],
723
+ "message": "Invalid input"
724
+ }
725
+ ]],
732
726
  "success": false,
733
727
  }
734
728
  `);
735
729
  expect(connectionString.safeParse("mongodb://host:1234/defaultauthdb?authSourceadmin")).toMatchInlineSnapshot(`
736
730
  {
737
- "error": ZodError {
738
- "issues": [
739
- {
740
- "code": "invalid_format",
741
- "format": "template_literal",
742
- "message": "Invalid input",
743
- "path": [],
744
- "pattern": "^mongodb:\\/\\/(\\w+:\\w+@)?\\w+:\\d+(\\/(\\w+)?(\\?(\\w+=\\w+(&\\w+=\\w+)*)?)?)?$",
745
- },
746
- ],
747
- },
731
+ "error": [ZodError: [
732
+ {
733
+ "code": "invalid_format",
734
+ "format": "template_literal",
735
+ "pattern": "^mongodb:\\\\/\\\\/(\\\\w+:\\\\w+@)?\\\\w+:\\\\d+(\\\\/(\\\\w+)?(\\\\?(\\\\w+=\\\\w+(&\\\\w+=\\\\w+)*)?)?)?$",
736
+ "path": [],
737
+ "message": "Invalid input"
738
+ }
739
+ ]],
748
740
  "success": false,
749
741
  }
750
742
  `);
751
743
 
752
744
  expect(stringStartsWithMax5.safeParse("1hell")).toMatchInlineSnapshot(`
753
745
  {
754
- "error": ZodError {
755
- "issues": [
756
- {
757
- "code": "invalid_format",
758
- "format": "template_literal",
759
- "message": "Invalid input",
760
- "path": [],
761
- "pattern": "^hello.*$",
762
- },
763
- ],
764
- },
746
+ "error": [ZodError: [
747
+ {
748
+ "code": "invalid_format",
749
+ "format": "template_literal",
750
+ "pattern": "^hello.*$",
751
+ "path": [],
752
+ "message": "Invalid input"
753
+ }
754
+ ]],
765
755
  "success": false,
766
756
  }
767
757
  `);