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
@@ -17,15 +17,13 @@ test("transform ctx.addIssue with parse", () => {
17
17
  const result = schema.safeParse("asdf");
18
18
  expect(result.success).toEqual(false);
19
19
  expect(result.error!).toMatchInlineSnapshot(`
20
- ZodError {
21
- "issues": [
22
- {
23
- "code": "custom",
24
- "message": "asdf is not one of our allowed strings",
25
- "path": [],
26
- },
27
- ],
28
- }
20
+ [ZodError: [
21
+ {
22
+ "code": "custom",
23
+ "message": "asdf is not one of our allowed strings",
24
+ "path": []
25
+ }
26
+ ]]
29
27
  `);
30
28
  });
31
29
 
@@ -49,15 +47,13 @@ test("transform ctx.addIssue with parseAsync", async () => {
49
47
 
50
48
  expect(result).toMatchInlineSnapshot(`
51
49
  {
52
- "error": ZodError {
53
- "issues": [
54
- {
55
- "code": "custom",
56
- "message": "asdf is not one of our allowed strings",
57
- "path": [],
58
- },
59
- ],
60
- },
50
+ "error": [ZodError: [
51
+ {
52
+ "code": "custom",
53
+ "message": "asdf is not one of our allowed strings",
54
+ "path": []
55
+ }
56
+ ]],
61
57
  "success": false,
62
58
  }
63
59
  `);
@@ -204,15 +200,13 @@ test("short circuit on dirty", () => {
204
200
  expect(result.success).toEqual(false);
205
201
 
206
202
  expect(result.error).toMatchInlineSnapshot(`
207
- ZodError {
208
- "issues": [
209
- {
210
- "code": "custom",
211
- "message": "Invalid input",
212
- "path": [],
213
- },
214
- ],
215
- }
203
+ [ZodError: [
204
+ {
205
+ "code": "custom",
206
+ "path": [],
207
+ "message": "Invalid input"
208
+ }
209
+ ]]
216
210
  `);
217
211
 
218
212
  const result2 = schema.safeParse(1234);
@@ -231,30 +225,26 @@ test("async short circuit on dirty", async () => {
231
225
  expect(result.success).toEqual(false);
232
226
 
233
227
  expect(result.error).toMatchInlineSnapshot(`
234
- ZodError {
235
- "issues": [
236
- {
237
- "code": "custom",
238
- "message": "Invalid input",
239
- "path": [],
240
- },
241
- ],
242
- }
228
+ [ZodError: [
229
+ {
230
+ "code": "custom",
231
+ "path": [],
232
+ "message": "Invalid input"
233
+ }
234
+ ]]
243
235
  `);
244
236
 
245
237
  const result2 = await schema.spa(1234);
246
238
  expect(result2.success).toEqual(false);
247
239
 
248
240
  expect(result2.error).toMatchInlineSnapshot(`
249
- ZodError {
250
- "issues": [
251
- {
252
- "code": "invalid_type",
253
- "expected": "string",
254
- "message": "Invalid input: expected string, received number",
255
- "path": [],
256
- },
257
- ],
258
- }
241
+ [ZodError: [
242
+ {
243
+ "expected": "string",
244
+ "code": "invalid_type",
245
+ "path": [],
246
+ "message": "Invalid input: expected string, received number"
247
+ }
248
+ ]]
259
249
  `);
260
250
  });
@@ -11,49 +11,43 @@ test("successful validation", () => {
11
11
  const r1 = testTuple.safeParse(["asdf", "asdf"]);
12
12
  expect(r1.success).toEqual(false);
13
13
  expect(r1.error!).toMatchInlineSnapshot(`
14
- ZodError {
15
- "issues": [
16
- {
17
- "code": "invalid_type",
18
- "expected": "number",
19
- "message": "Invalid input: expected number, received string",
20
- "path": [
21
- 1,
22
- ],
23
- },
24
- ],
25
- }
14
+ [ZodError: [
15
+ {
16
+ "expected": "number",
17
+ "code": "invalid_type",
18
+ "path": [
19
+ 1
20
+ ],
21
+ "message": "Invalid input: expected number, received string"
22
+ }
23
+ ]]
26
24
  `);
27
25
 
28
26
  const r2 = testTuple.safeParse(["asdf", 1234, true]);
29
27
  expect(r2.success).toEqual(false);
30
28
  expect(r2.error!).toMatchInlineSnapshot(`
31
- ZodError {
32
- "issues": [
33
- {
34
- "code": "too_big",
35
- "maximum": 2,
36
- "message": "Too big: expected array to have <2 items",
37
- "origin": "array",
38
- "path": [],
39
- },
40
- ],
41
- }
29
+ [ZodError: [
30
+ {
31
+ "origin": "array",
32
+ "code": "too_big",
33
+ "maximum": 2,
34
+ "path": [],
35
+ "message": "Too big: expected array to have <2 items"
36
+ }
37
+ ]]
42
38
  `);
43
39
 
44
40
  const r3 = testTuple.safeParse({});
45
41
  expect(r3.success).toEqual(false);
46
42
  expect(r3.error!).toMatchInlineSnapshot(`
47
- ZodError {
48
- "issues": [
49
- {
50
- "code": "invalid_type",
51
- "expected": "tuple",
52
- "message": "Invalid input: expected tuple, received object",
53
- "path": [],
54
- },
55
- ],
56
- }
43
+ [ZodError: [
44
+ {
45
+ "expected": "tuple",
46
+ "code": "invalid_type",
47
+ "path": [],
48
+ "message": "Invalid input: expected tuple, received object"
49
+ }
50
+ ]]
57
51
  `);
58
52
  });
59
53
 
@@ -69,49 +63,43 @@ test("async validation", async () => {
69
63
  const r1 = await testTuple.safeParseAsync(["asdf", "asdf"]);
70
64
  expect(r1.success).toEqual(false);
71
65
  expect(r1.error!).toMatchInlineSnapshot(`
72
- ZodError {
73
- "issues": [
74
- {
75
- "code": "invalid_type",
76
- "expected": "number",
77
- "message": "Invalid input: expected number, received string",
78
- "path": [
79
- 1,
80
- ],
81
- },
82
- ],
83
- }
66
+ [ZodError: [
67
+ {
68
+ "expected": "number",
69
+ "code": "invalid_type",
70
+ "path": [
71
+ 1
72
+ ],
73
+ "message": "Invalid input: expected number, received string"
74
+ }
75
+ ]]
84
76
  `);
85
77
 
86
78
  const r2 = await testTuple.safeParseAsync(["asdf", 1234, true]);
87
79
  expect(r2.success).toEqual(false);
88
80
  expect(r2.error!).toMatchInlineSnapshot(`
89
- ZodError {
90
- "issues": [
91
- {
92
- "code": "too_big",
93
- "maximum": 2,
94
- "message": "Too big: expected array to have <2 items",
95
- "origin": "array",
96
- "path": [],
97
- },
98
- ],
99
- }
81
+ [ZodError: [
82
+ {
83
+ "origin": "array",
84
+ "code": "too_big",
85
+ "maximum": 2,
86
+ "path": [],
87
+ "message": "Too big: expected array to have <2 items"
88
+ }
89
+ ]]
100
90
  `);
101
91
 
102
92
  const r3 = await testTuple.safeParseAsync({});
103
93
  expect(r3.success).toEqual(false);
104
94
  expect(r3.error!).toMatchInlineSnapshot(`
105
- ZodError {
106
- "issues": [
107
- {
108
- "code": "invalid_type",
109
- "expected": "tuple",
110
- "message": "Invalid input: expected tuple, received object",
111
- "path": [],
112
- },
113
- ],
114
- }
95
+ [ZodError: [
96
+ {
97
+ "expected": "tuple",
98
+ "code": "invalid_type",
99
+ "path": [],
100
+ "message": "Invalid input: expected tuple, received object"
101
+ }
102
+ ]]
115
103
  `);
116
104
  });
117
105
 
@@ -1,7 +1,7 @@
1
1
  import type { $ZodCheck, $ZodStringFormats } from "./checks.js";
2
2
  import { $constructor } from "./core.js";
3
3
  import type { $ZodType } from "./schemas.js";
4
- import type * as util from "./util.js";
4
+ import * as util from "./util.js";
5
5
 
6
6
  ///////////////////////////
7
7
  //// base type ////
@@ -181,14 +181,25 @@ export interface $ZodError<T = unknown> extends Error {
181
181
  }
182
182
 
183
183
  const initializer = (inst: $ZodError, def: $ZodIssue[]): void => {
184
+ inst.name = "$ZodError";
184
185
  Object.defineProperty(inst, "_zod", {
185
186
  value: inst._zod,
186
187
  enumerable: false,
187
188
  });
188
189
  Object.defineProperty(inst, "issues", {
189
190
  value: def,
191
+ enumerable: false,
192
+ });
193
+ // inst.message = JSON.stringify(def, util.jsonStringifyReplacer, 2);
194
+ Object.defineProperty(inst, "message", {
195
+ get() {
196
+ return JSON.stringify(def, util.jsonStringifyReplacer, 2);
197
+ },
190
198
  enumerable: true,
199
+ // configurable: false,
191
200
  });
201
+ // inst.toString = () => inst.message;
202
+
192
203
  // inst.message = `Invalid input`;
193
204
  // Object.defineProperty(inst, "message", {
194
205
  // get() {
@@ -70,7 +70,7 @@ export const _safeParse: (_Err: $ZodErrorClass) => $SafeParse = (_Err) => (schem
70
70
  }
71
71
  : { success: true, data: result.value };
72
72
  };
73
- export const safeParse: $SafeParse = /* @__PURE__*/ _safeParse(errors.$ZodError);
73
+ export const safeParse: $SafeParse = /* @__PURE__*/ _safeParse(errors.$ZodRealError);
74
74
 
75
75
  export type $SafeParseAsync = <T extends schemas.$ZodType>(
76
76
  schema: T,
@@ -91,4 +91,4 @@ export const _safeParseAsync: (_Err: $ZodErrorClass) => $SafeParseAsync = (_Err)
91
91
  : { success: true, data: result.value };
92
92
  };
93
93
 
94
- export const safeParseAsync: $SafeParseAsync = /* @__PURE__*/ _safeParseAsync(errors.$ZodError);
94
+ export const safeParseAsync: $SafeParseAsync = /* @__PURE__*/ _safeParseAsync(errors.$ZodRealError);
@@ -1,5 +1,46 @@
1
- import { expect, test } from "vitest";
1
+ import { expect, expectTypeOf, test } from "vitest";
2
+ import * as z from "zod/v3";
2
3
 
3
4
  test("test", () => {
4
5
  expect(true).toBe(true);
5
6
  });
7
+
8
+ test("test2", () => {
9
+ expect(() => z.string().parse(234)).toThrowErrorMatchingInlineSnapshot(`
10
+ [ZodError: [
11
+ {
12
+ "code": "invalid_type",
13
+ "expected": "string",
14
+ "received": "number",
15
+ "path": [],
16
+ "message": "Expected string, received number"
17
+ }
18
+ ]]
19
+ `);
20
+ });
21
+
22
+ test("async validation", async () => {
23
+ const testTuple = z
24
+ .tuple([z.string().refine(async () => true), z.number().refine(async () => true)])
25
+ .refine(async () => true);
26
+ expectTypeOf<typeof testTuple._output>().toEqualTypeOf<[string, number]>();
27
+
28
+ const val = await testTuple.parseAsync(["asdf", 1234]);
29
+ expect(val).toEqual(val);
30
+
31
+ const r1 = await testTuple.safeParseAsync(["asdf", "asdf"]);
32
+ expect(r1.success).toEqual(false);
33
+ expect(r1.error!).toMatchInlineSnapshot(`
34
+ [ZodError: [
35
+ {
36
+ "code": "invalid_type",
37
+ "expected": "number",
38
+ "received": "string",
39
+ "path": [
40
+ 1
41
+ ],
42
+ "message": "Expected number, received string"
43
+ }
44
+ ]]
45
+ `);
46
+ });
@@ -11,7 +11,7 @@ test("no locale by default", () => {
11
11
  test("error inheritance", () => {
12
12
  const e1 = z.string().safeParse(123).error!;
13
13
  expect(e1).toBeInstanceOf(z.core.$ZodError);
14
- expect(e1).not.toBeInstanceOf(Error);
14
+ // expect(e1).not.toBeInstanceOf(Error);
15
15
 
16
16
  try {
17
17
  z.string().parse(123);