zod 3.25.4 → 3.25.5

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 (42) hide show
  1. package/dist/commonjs/v4/classic/errors.d.ts +1 -1
  2. package/dist/commonjs/v4/classic/errors.js +1 -0
  3. package/dist/commonjs/v4/classic/parse.d.ts +1 -1
  4. package/dist/commonjs/v4/classic/parse.js +2 -2
  5. package/dist/commonjs/v4/core/errors.d.ts +1 -1
  6. package/dist/commonjs/v4/core/errors.js +44 -0
  7. package/dist/commonjs/v4/core/parse.js +2 -2
  8. package/dist/esm/v4/classic/errors.d.ts +1 -1
  9. package/dist/esm/v4/classic/errors.js +1 -0
  10. package/dist/esm/v4/classic/parse.d.ts +1 -1
  11. package/dist/esm/v4/classic/parse.js +3 -3
  12. package/dist/esm/v4/core/errors.d.ts +1 -1
  13. package/dist/esm/v4/core/errors.js +11 -0
  14. package/dist/esm/v4/core/parse.js +2 -2
  15. package/package.json +1 -1
  16. package/src/v4/classic/errors.ts +2 -1
  17. package/src/v4/classic/parse.ts +3 -3
  18. package/src/v4/classic/tests/array.test.ts +42 -46
  19. package/src/v4/classic/tests/async-refinements.test.ts +14 -18
  20. package/src/v4/classic/tests/catch.test.ts +10 -12
  21. package/src/v4/classic/tests/discriminated-unions.test.ts +77 -87
  22. package/src/v4/classic/tests/enum.test.ts +33 -39
  23. package/src/v4/classic/tests/error-utils.test.ts +25 -27
  24. package/src/v4/classic/tests/error.test.ts +115 -128
  25. package/src/v4/classic/tests/literal.test.ts +10 -12
  26. package/src/v4/classic/tests/map.test.ts +60 -68
  27. package/src/v4/classic/tests/nested-refine.test.ts +68 -72
  28. package/src/v4/classic/tests/nonoptional.test.ts +36 -44
  29. package/src/v4/classic/tests/number.test.ts +36 -44
  30. package/src/v4/classic/tests/pipe.test.ts +18 -22
  31. package/src/v4/classic/tests/preprocess.test.ts +91 -107
  32. package/src/v4/classic/tests/primitive.test.ts +9 -11
  33. package/src/v4/classic/tests/record.test.ts +101 -121
  34. package/src/v4/classic/tests/set.test.ts +30 -36
  35. package/src/v4/classic/tests/string.test.ts +50 -60
  36. package/src/v4/classic/tests/template-literal.test.ts +45 -55
  37. package/src/v4/classic/tests/transform.test.ts +36 -46
  38. package/src/v4/classic/tests/tuple.test.ts +54 -66
  39. package/src/v4/core/errors.ts +12 -1
  40. package/src/v4/core/parse.ts +2 -2
  41. package/src/v4/core/tests/index.test.ts +42 -1
  42. package/src/v4/mini/tests/error.test.ts +1 -1
@@ -32,15 +32,13 @@ test("ctx.addIssue accepts string", () => {
32
32
  expect(result.error!.issues).toHaveLength(1);
33
33
  expect(result).toMatchInlineSnapshot(`
34
34
  {
35
- "error": ZodError {
36
- "issues": [
37
- {
38
- "code": "custom",
39
- "message": "bad stuff",
40
- "path": [],
41
- },
42
- ],
43
- },
35
+ "error": [ZodError: [
36
+ {
37
+ "message": "bad stuff",
38
+ "code": "custom",
39
+ "path": []
40
+ }
41
+ ]],
44
42
  "success": false,
45
43
  }
46
44
  `);
@@ -63,15 +61,13 @@ test("preprocess ctx.addIssue with parse", () => {
63
61
  expect(result.error!.issues).toHaveLength(1);
64
62
  expect(result).toMatchInlineSnapshot(`
65
63
  {
66
- "error": ZodError {
67
- "issues": [
68
- {
69
- "code": "custom",
70
- "message": "asdf is not one of our allowed strings",
71
- "path": [],
72
- },
73
- ],
74
- },
64
+ "error": [ZodError: [
65
+ {
66
+ "code": "custom",
67
+ "message": "asdf is not one of our allowed strings",
68
+ "path": []
69
+ }
70
+ ]],
75
71
  "success": false,
76
72
  }
77
73
  `);
@@ -90,21 +86,19 @@ test("preprocess ctx.addIssue non-fatal by default", () => {
90
86
  expect(result.error!.issues).toHaveLength(2);
91
87
  expect(result).toMatchInlineSnapshot(`
92
88
  {
93
- "error": ZodError {
94
- "issues": [
95
- {
96
- "code": "custom",
97
- "message": "custom error",
98
- "path": [],
99
- },
100
- {
101
- "code": "invalid_type",
102
- "expected": "string",
103
- "message": "Invalid input: expected string, received number",
104
- "path": [],
105
- },
106
- ],
89
+ "error": [ZodError: [
90
+ {
91
+ "code": "custom",
92
+ "message": "custom error",
93
+ "path": []
107
94
  },
95
+ {
96
+ "expected": "string",
97
+ "code": "invalid_type",
98
+ "path": [],
99
+ "message": "Invalid input: expected string, received number"
100
+ }
101
+ ]],
108
102
  "success": false,
109
103
  }
110
104
  `);
@@ -127,17 +121,15 @@ test("preprocess ctx.addIssue fatal true", () => {
127
121
  expect(result.error!.issues).toHaveLength(1);
128
122
  expect(result).toMatchInlineSnapshot(`
129
123
  {
130
- "error": ZodError {
131
- "issues": [
132
- {
133
- "code": "custom",
134
- "fatal": true,
135
- "message": "custom error",
136
- "origin": "custom",
137
- "path": [],
138
- },
139
- ],
140
- },
124
+ "error": [ZodError: [
125
+ {
126
+ "code": "custom",
127
+ "origin": "custom",
128
+ "message": "custom error",
129
+ "fatal": true,
130
+ "path": []
131
+ }
132
+ ]],
141
133
  "success": false,
142
134
  }
143
135
  `);
@@ -158,15 +150,13 @@ test("async preprocess ctx.addIssue with parseAsync", async () => {
158
150
  expect(result.error!.issues).toHaveLength(1);
159
151
  expect(result).toMatchInlineSnapshot(`
160
152
  {
161
- "error": ZodError {
162
- "issues": [
163
- {
164
- "code": "custom",
165
- "message": "asdf is not one of our allowed strings",
166
- "path": [],
167
- },
168
- ],
169
- },
153
+ "error": [ZodError: [
154
+ {
155
+ "code": "custom",
156
+ "message": "asdf is not one of our allowed strings",
157
+ "path": []
158
+ }
159
+ ]],
170
160
  "success": false,
171
161
  }
172
162
  `);
@@ -188,21 +178,19 @@ test("z.NEVER in preprocess", () => {
188
178
  expect(result.error!.issues).toHaveLength(2);
189
179
  expect(result).toMatchInlineSnapshot(`
190
180
  {
191
- "error": ZodError {
192
- "issues": [
193
- {
194
- "code": "custom",
195
- "message": "bad",
196
- "path": [],
197
- },
198
- {
199
- "code": "invalid_type",
200
- "expected": "number",
201
- "message": "Invalid input: expected number, received object",
202
- "path": [],
203
- },
204
- ],
181
+ "error": [ZodError: [
182
+ {
183
+ "code": "custom",
184
+ "message": "bad",
185
+ "path": []
205
186
  },
187
+ {
188
+ "expected": "number",
189
+ "code": "invalid_type",
190
+ "path": [],
191
+ "message": "Invalid input: expected number, received object"
192
+ }
193
+ ]],
206
194
  "success": false,
207
195
  }
208
196
  `);
@@ -221,29 +209,27 @@ test("preprocess as the second property of object", () => {
221
209
  expect(result.error!.issues).toHaveLength(2);
222
210
  expect(result).toMatchInlineSnapshot(`
223
211
  {
224
- "error": ZodError {
225
- "issues": [
226
- {
227
- "code": "too_small",
228
- "message": "Too small: expected string to have >1 characters",
229
- "minimum": 1,
230
- "origin": "string",
231
- "path": [
232
- "nonEmptyStr",
233
- ],
234
- },
235
- {
236
- "code": "too_small",
237
- "inclusive": false,
238
- "message": "Too small: expected number to be >0",
239
- "minimum": 0,
240
- "origin": "number",
241
- "path": [
242
- "positiveNum",
243
- ],
244
- },
212
+ "error": [ZodError: [
213
+ {
214
+ "origin": "string",
215
+ "code": "too_small",
216
+ "minimum": 1,
217
+ "path": [
218
+ "nonEmptyStr"
245
219
  ],
220
+ "message": "Too small: expected string to have >1 characters"
246
221
  },
222
+ {
223
+ "origin": "number",
224
+ "code": "too_small",
225
+ "minimum": 0,
226
+ "inclusive": false,
227
+ "path": [
228
+ "positiveNum"
229
+ ],
230
+ "message": "Too small: expected number to be >0"
231
+ }
232
+ ]],
247
233
  "success": false,
248
234
  }
249
235
  `);
@@ -260,28 +246,26 @@ test("preprocess validates with sibling errors", () => {
260
246
  expect(result.error!.issues).toHaveLength(2);
261
247
  expect(result).toMatchInlineSnapshot(`
262
248
  {
263
- "error": ZodError {
264
- "issues": [
265
- {
266
- "code": "invalid_type",
267
- "expected": "string",
268
- "message": "Invalid input: expected string, received undefined",
269
- "path": [
270
- "missing",
271
- ],
272
- },
273
- {
274
- "code": "invalid_format",
275
- "format": "regex",
276
- "message": "Invalid string: must match pattern / asdf/",
277
- "origin": "string",
278
- "path": [
279
- "preprocess",
280
- ],
281
- "pattern": "/ asdf/",
282
- },
249
+ "error": [ZodError: [
250
+ {
251
+ "expected": "string",
252
+ "code": "invalid_type",
253
+ "path": [
254
+ "missing"
283
255
  ],
256
+ "message": "Invalid input: expected string, received undefined"
284
257
  },
258
+ {
259
+ "origin": "string",
260
+ "code": "invalid_format",
261
+ "format": "regex",
262
+ "pattern": "/ asdf/",
263
+ "path": [
264
+ "preprocess"
265
+ ],
266
+ "message": "Invalid string: must match pattern / asdf/"
267
+ }
268
+ ]],
285
269
  "success": false,
286
270
  }
287
271
  `);
@@ -102,17 +102,15 @@ test("date schema", async () => {
102
102
  expect(() => dateSchema.parse(null)).toThrow();
103
103
  expect(await dateSchema.safeParseAsync(new Date("invalid"))).toMatchInlineSnapshot(`
104
104
  {
105
- "error": ZodError {
106
- "issues": [
107
- {
108
- "code": "invalid_type",
109
- "expected": "date",
110
- "message": "Invalid input: expected date, received Date",
111
- "path": [],
112
- "received": "Invalid Date",
113
- },
114
- ],
115
- },
105
+ "error": [ZodError: [
106
+ {
107
+ "expected": "date",
108
+ "code": "invalid_type",
109
+ "received": "Invalid Date",
110
+ "path": [],
111
+ "message": "Invalid input: expected date, received Date"
112
+ }
113
+ ]],
116
114
  "success": false,
117
115
  }
118
116
  `);
@@ -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