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.
- package/dist/commonjs/v4/classic/errors.d.ts +1 -1
- package/dist/commonjs/v4/classic/errors.js +1 -0
- package/dist/commonjs/v4/classic/parse.d.ts +1 -1
- package/dist/commonjs/v4/classic/parse.js +2 -2
- package/dist/commonjs/v4/core/errors.d.ts +1 -1
- package/dist/commonjs/v4/core/errors.js +44 -0
- package/dist/commonjs/v4/core/parse.js +2 -2
- package/dist/esm/v4/classic/errors.d.ts +1 -1
- package/dist/esm/v4/classic/errors.js +1 -0
- package/dist/esm/v4/classic/parse.d.ts +1 -1
- package/dist/esm/v4/classic/parse.js +3 -3
- package/dist/esm/v4/core/errors.d.ts +1 -1
- package/dist/esm/v4/core/errors.js +11 -0
- package/dist/esm/v4/core/parse.js +2 -2
- package/package.json +1 -1
- package/src/v4/classic/errors.ts +2 -1
- package/src/v4/classic/parse.ts +3 -3
- package/src/v4/classic/tests/array.test.ts +42 -46
- package/src/v4/classic/tests/async-refinements.test.ts +14 -18
- package/src/v4/classic/tests/catch.test.ts +10 -12
- package/src/v4/classic/tests/discriminated-unions.test.ts +77 -87
- package/src/v4/classic/tests/enum.test.ts +33 -39
- package/src/v4/classic/tests/error-utils.test.ts +25 -27
- package/src/v4/classic/tests/error.test.ts +115 -128
- package/src/v4/classic/tests/literal.test.ts +10 -12
- package/src/v4/classic/tests/map.test.ts +60 -68
- package/src/v4/classic/tests/nested-refine.test.ts +68 -72
- package/src/v4/classic/tests/nonoptional.test.ts +36 -44
- package/src/v4/classic/tests/number.test.ts +36 -44
- package/src/v4/classic/tests/pipe.test.ts +18 -22
- package/src/v4/classic/tests/preprocess.test.ts +91 -107
- package/src/v4/classic/tests/primitive.test.ts +9 -11
- package/src/v4/classic/tests/record.test.ts +101 -121
- package/src/v4/classic/tests/set.test.ts +30 -36
- package/src/v4/classic/tests/string.test.ts +50 -60
- package/src/v4/classic/tests/template-literal.test.ts +45 -55
- package/src/v4/classic/tests/transform.test.ts +36 -46
- package/src/v4/classic/tests/tuple.test.ts +54 -66
- package/src/v4/core/errors.ts +12 -1
- package/src/v4/core/parse.ts +2 -2
- package/src/v4/core/tests/index.test.ts +42 -1
- package/src/v4/mini/tests/error.test.ts +1 -1
|
@@ -32,24 +32,22 @@ test("valid parse async", async () => {
|
|
|
32
32
|
const result = await asyncMap.safeParseAsync(new Map([["first", "foo"]]));
|
|
33
33
|
expect(result.success).toEqual(false);
|
|
34
34
|
expect(result.error).toMatchInlineSnapshot(`
|
|
35
|
-
ZodError
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
"
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
"
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
],
|
|
52
|
-
}
|
|
35
|
+
[ZodError: [
|
|
36
|
+
{
|
|
37
|
+
"code": "custom",
|
|
38
|
+
"path": [
|
|
39
|
+
"first"
|
|
40
|
+
],
|
|
41
|
+
"message": "bad key"
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
"code": "custom",
|
|
45
|
+
"path": [
|
|
46
|
+
"first"
|
|
47
|
+
],
|
|
48
|
+
"message": "bad value"
|
|
49
|
+
}
|
|
50
|
+
]]
|
|
53
51
|
`);
|
|
54
52
|
});
|
|
55
53
|
|
|
@@ -68,26 +66,24 @@ test("throws when the given map has invalid key and invalid input", () => {
|
|
|
68
66
|
if (result.success === false) {
|
|
69
67
|
expect(result.error.issues.length).toEqual(2);
|
|
70
68
|
expect(result.error).toMatchInlineSnapshot(`
|
|
71
|
-
ZodError
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
],
|
|
90
|
-
}
|
|
69
|
+
[ZodError: [
|
|
70
|
+
{
|
|
71
|
+
"expected": "string",
|
|
72
|
+
"code": "invalid_type",
|
|
73
|
+
"path": [
|
|
74
|
+
42
|
|
75
|
+
],
|
|
76
|
+
"message": "Invalid input: expected string, received number"
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
"expected": "string",
|
|
80
|
+
"code": "invalid_type",
|
|
81
|
+
"path": [
|
|
82
|
+
42
|
|
83
|
+
],
|
|
84
|
+
"message": "Invalid input: expected string, received symbol"
|
|
85
|
+
}
|
|
86
|
+
]]
|
|
91
87
|
`);
|
|
92
88
|
}
|
|
93
89
|
});
|
|
@@ -146,24 +142,22 @@ test("dirty", async () => {
|
|
|
146
142
|
if (!result.success) {
|
|
147
143
|
expect(result.error.issues.length).toEqual(2);
|
|
148
144
|
expect(result.error).toMatchInlineSnapshot(`
|
|
149
|
-
ZodError
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
"
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
"
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
],
|
|
166
|
-
}
|
|
145
|
+
[ZodError: [
|
|
146
|
+
{
|
|
147
|
+
"code": "custom",
|
|
148
|
+
"path": [
|
|
149
|
+
"first"
|
|
150
|
+
],
|
|
151
|
+
"message": "Keys must be uppercase"
|
|
152
|
+
},
|
|
153
|
+
{
|
|
154
|
+
"code": "custom",
|
|
155
|
+
"path": [
|
|
156
|
+
"second"
|
|
157
|
+
],
|
|
158
|
+
"message": "Keys must be uppercase"
|
|
159
|
+
}
|
|
160
|
+
]]
|
|
167
161
|
`);
|
|
168
162
|
}
|
|
169
163
|
});
|
|
@@ -188,17 +182,15 @@ test("map with object keys", () => {
|
|
|
188
182
|
const badResult = map.safeParse(badData);
|
|
189
183
|
expect(badResult.success).toEqual(false);
|
|
190
184
|
expect(badResult.error).toMatchInlineSnapshot(`
|
|
191
|
-
ZodError
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
"
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
],
|
|
202
|
-
}
|
|
185
|
+
[ZodError: [
|
|
186
|
+
{
|
|
187
|
+
"expected": "object",
|
|
188
|
+
"code": "invalid_type",
|
|
189
|
+
"path": [
|
|
190
|
+
"bad"
|
|
191
|
+
],
|
|
192
|
+
"message": "Invalid input: expected object, received string"
|
|
193
|
+
}
|
|
194
|
+
]]
|
|
203
195
|
`);
|
|
204
196
|
});
|
|
@@ -40,88 +40,84 @@ test("nested refinements", () => {
|
|
|
40
40
|
};
|
|
41
41
|
expect(zodSchema.safeParse(DATA)).toMatchInlineSnapshot(`
|
|
42
42
|
{
|
|
43
|
-
"error": ZodError
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
"nested",
|
|
52
|
-
"confirm",
|
|
53
|
-
],
|
|
54
|
-
},
|
|
55
|
-
{
|
|
56
|
-
"code": "custom",
|
|
57
|
-
"message": "Confirm length should be > 2",
|
|
58
|
-
"path": [
|
|
59
|
-
"nested",
|
|
60
|
-
"confirm",
|
|
61
|
-
],
|
|
62
|
-
},
|
|
63
|
-
{
|
|
64
|
-
"code": "custom",
|
|
65
|
-
"message": "Value must be "bar"",
|
|
66
|
-
"path": [
|
|
67
|
-
"nested",
|
|
68
|
-
"confirm",
|
|
69
|
-
],
|
|
70
|
-
},
|
|
71
|
-
{
|
|
72
|
-
"code": "custom",
|
|
73
|
-
"message": "Password and confirm must match",
|
|
74
|
-
"path": [
|
|
75
|
-
"nested",
|
|
76
|
-
"confirm",
|
|
77
|
-
],
|
|
78
|
-
},
|
|
43
|
+
"error": [ZodError: [
|
|
44
|
+
{
|
|
45
|
+
"origin": "string",
|
|
46
|
+
"code": "too_small",
|
|
47
|
+
"minimum": 1,
|
|
48
|
+
"path": [
|
|
49
|
+
"nested",
|
|
50
|
+
"confirm"
|
|
79
51
|
],
|
|
52
|
+
"message": "Too small: expected string to have >1 characters"
|
|
80
53
|
},
|
|
54
|
+
{
|
|
55
|
+
"code": "custom",
|
|
56
|
+
"path": [
|
|
57
|
+
"nested",
|
|
58
|
+
"confirm"
|
|
59
|
+
],
|
|
60
|
+
"message": "Confirm length should be > 2"
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
"code": "custom",
|
|
64
|
+
"path": [
|
|
65
|
+
"nested",
|
|
66
|
+
"confirm"
|
|
67
|
+
],
|
|
68
|
+
"message": "Value must be \\"bar\\""
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
"code": "custom",
|
|
72
|
+
"path": [
|
|
73
|
+
"nested",
|
|
74
|
+
"confirm"
|
|
75
|
+
],
|
|
76
|
+
"message": "Password and confirm must match"
|
|
77
|
+
}
|
|
78
|
+
]],
|
|
81
79
|
"success": false,
|
|
82
80
|
}
|
|
83
81
|
`);
|
|
84
82
|
|
|
85
83
|
expect(zodSchema.safeParse(DATA, { jitless: true })).toMatchInlineSnapshot(`
|
|
86
84
|
{
|
|
87
|
-
"error": ZodError
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
"nested",
|
|
96
|
-
"confirm",
|
|
97
|
-
],
|
|
98
|
-
},
|
|
99
|
-
{
|
|
100
|
-
"code": "custom",
|
|
101
|
-
"message": "Confirm length should be > 2",
|
|
102
|
-
"path": [
|
|
103
|
-
"nested",
|
|
104
|
-
"confirm",
|
|
105
|
-
],
|
|
106
|
-
},
|
|
107
|
-
{
|
|
108
|
-
"code": "custom",
|
|
109
|
-
"message": "Value must be "bar"",
|
|
110
|
-
"path": [
|
|
111
|
-
"nested",
|
|
112
|
-
"confirm",
|
|
113
|
-
],
|
|
114
|
-
},
|
|
115
|
-
{
|
|
116
|
-
"code": "custom",
|
|
117
|
-
"message": "Password and confirm must match",
|
|
118
|
-
"path": [
|
|
119
|
-
"nested",
|
|
120
|
-
"confirm",
|
|
121
|
-
],
|
|
122
|
-
},
|
|
85
|
+
"error": [ZodError: [
|
|
86
|
+
{
|
|
87
|
+
"origin": "string",
|
|
88
|
+
"code": "too_small",
|
|
89
|
+
"minimum": 1,
|
|
90
|
+
"path": [
|
|
91
|
+
"nested",
|
|
92
|
+
"confirm"
|
|
123
93
|
],
|
|
94
|
+
"message": "Too small: expected string to have >1 characters"
|
|
124
95
|
},
|
|
96
|
+
{
|
|
97
|
+
"code": "custom",
|
|
98
|
+
"path": [
|
|
99
|
+
"nested",
|
|
100
|
+
"confirm"
|
|
101
|
+
],
|
|
102
|
+
"message": "Confirm length should be > 2"
|
|
103
|
+
},
|
|
104
|
+
{
|
|
105
|
+
"code": "custom",
|
|
106
|
+
"path": [
|
|
107
|
+
"nested",
|
|
108
|
+
"confirm"
|
|
109
|
+
],
|
|
110
|
+
"message": "Value must be \\"bar\\""
|
|
111
|
+
},
|
|
112
|
+
{
|
|
113
|
+
"code": "custom",
|
|
114
|
+
"path": [
|
|
115
|
+
"nested",
|
|
116
|
+
"confirm"
|
|
117
|
+
],
|
|
118
|
+
"message": "Password and confirm must match"
|
|
119
|
+
}
|
|
120
|
+
]],
|
|
125
121
|
"success": false,
|
|
126
122
|
}
|
|
127
123
|
`);
|
|
@@ -10,16 +10,14 @@ test("nonoptional", () => {
|
|
|
10
10
|
expect(result.success).toBe(false);
|
|
11
11
|
expect(result).toMatchInlineSnapshot(`
|
|
12
12
|
{
|
|
13
|
-
"error": ZodError
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
],
|
|
22
|
-
},
|
|
13
|
+
"error": [ZodError: [
|
|
14
|
+
{
|
|
15
|
+
"expected": "string",
|
|
16
|
+
"code": "invalid_type",
|
|
17
|
+
"path": [],
|
|
18
|
+
"message": "Invalid input: expected string, received undefined"
|
|
19
|
+
}
|
|
20
|
+
]],
|
|
23
21
|
"success": false,
|
|
24
22
|
}
|
|
25
23
|
`);
|
|
@@ -34,16 +32,14 @@ test("nonoptional with default", () => {
|
|
|
34
32
|
expect(result.success).toBe(false);
|
|
35
33
|
expect(result).toMatchInlineSnapshot(`
|
|
36
34
|
{
|
|
37
|
-
"error": ZodError
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
],
|
|
46
|
-
},
|
|
35
|
+
"error": [ZodError: [
|
|
36
|
+
{
|
|
37
|
+
"code": "invalid_type",
|
|
38
|
+
"expected": "nonoptional",
|
|
39
|
+
"path": [],
|
|
40
|
+
"message": "Invalid input: expected nonoptional, received undefined"
|
|
41
|
+
}
|
|
42
|
+
]],
|
|
47
43
|
"success": false,
|
|
48
44
|
}
|
|
49
45
|
`);
|
|
@@ -61,34 +57,30 @@ test("nonoptional in object", () => {
|
|
|
61
57
|
// expect(schema.safeParse({ hi: undefined }).success).toEqual(false);
|
|
62
58
|
expect(r2.success).toEqual(false);
|
|
63
59
|
expect(r2.error).toMatchInlineSnapshot(`
|
|
64
|
-
ZodError
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
"
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
],
|
|
75
|
-
}
|
|
60
|
+
[ZodError: [
|
|
61
|
+
{
|
|
62
|
+
"code": "invalid_type",
|
|
63
|
+
"expected": "nonoptional",
|
|
64
|
+
"path": [
|
|
65
|
+
"hi"
|
|
66
|
+
],
|
|
67
|
+
"message": "Invalid input: expected nonoptional, received undefined"
|
|
68
|
+
}
|
|
69
|
+
]]
|
|
76
70
|
`);
|
|
77
71
|
|
|
78
72
|
const r3 = schema.safeParse({});
|
|
79
73
|
expect(r3.success).toEqual(false);
|
|
80
74
|
expect(r3.error).toMatchInlineSnapshot(`
|
|
81
|
-
ZodError
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
"
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
],
|
|
92
|
-
}
|
|
75
|
+
[ZodError: [
|
|
76
|
+
{
|
|
77
|
+
"code": "invalid_type",
|
|
78
|
+
"expected": "nonoptional",
|
|
79
|
+
"path": [
|
|
80
|
+
"hi"
|
|
81
|
+
],
|
|
82
|
+
"message": "Invalid input: expected nonoptional, received undefined"
|
|
83
|
+
}
|
|
84
|
+
]]
|
|
93
85
|
`);
|
|
94
86
|
});
|
|
@@ -16,33 +16,29 @@ test("Infinity validation", () => {
|
|
|
16
16
|
const schema = z.number();
|
|
17
17
|
expect(schema.safeParse(Number.POSITIVE_INFINITY)).toMatchInlineSnapshot(`
|
|
18
18
|
{
|
|
19
|
-
"error": ZodError
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
],
|
|
29
|
-
},
|
|
19
|
+
"error": [ZodError: [
|
|
20
|
+
{
|
|
21
|
+
"expected": "number",
|
|
22
|
+
"code": "invalid_type",
|
|
23
|
+
"received": "Infinity",
|
|
24
|
+
"path": [],
|
|
25
|
+
"message": "Invalid input: expected number, received number"
|
|
26
|
+
}
|
|
27
|
+
]],
|
|
30
28
|
"success": false,
|
|
31
29
|
}
|
|
32
30
|
`);
|
|
33
31
|
expect(schema.safeParse(Number.NEGATIVE_INFINITY)).toMatchInlineSnapshot(`
|
|
34
32
|
{
|
|
35
|
-
"error": ZodError
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
],
|
|
45
|
-
},
|
|
33
|
+
"error": [ZodError: [
|
|
34
|
+
{
|
|
35
|
+
"expected": "number",
|
|
36
|
+
"code": "invalid_type",
|
|
37
|
+
"received": "Infinity",
|
|
38
|
+
"path": [],
|
|
39
|
+
"message": "Invalid input: expected number, received number"
|
|
40
|
+
}
|
|
41
|
+
]],
|
|
46
42
|
"success": false,
|
|
47
43
|
}
|
|
48
44
|
`);
|
|
@@ -153,33 +149,29 @@ test(".finite() validation", () => {
|
|
|
153
149
|
expect(schema.parse(123)).toEqual(123);
|
|
154
150
|
expect(schema.safeParse(Number.POSITIVE_INFINITY)).toMatchInlineSnapshot(`
|
|
155
151
|
{
|
|
156
|
-
"error": ZodError
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
],
|
|
166
|
-
},
|
|
152
|
+
"error": [ZodError: [
|
|
153
|
+
{
|
|
154
|
+
"expected": "number",
|
|
155
|
+
"code": "invalid_type",
|
|
156
|
+
"received": "Infinity",
|
|
157
|
+
"path": [],
|
|
158
|
+
"message": "Invalid input: expected number, received number"
|
|
159
|
+
}
|
|
160
|
+
]],
|
|
167
161
|
"success": false,
|
|
168
162
|
}
|
|
169
163
|
`);
|
|
170
164
|
expect(schema.safeParse(Number.NEGATIVE_INFINITY)).toMatchInlineSnapshot(`
|
|
171
165
|
{
|
|
172
|
-
"error": ZodError
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
],
|
|
182
|
-
},
|
|
166
|
+
"error": [ZodError: [
|
|
167
|
+
{
|
|
168
|
+
"expected": "number",
|
|
169
|
+
"code": "invalid_type",
|
|
170
|
+
"received": "Infinity",
|
|
171
|
+
"path": [],
|
|
172
|
+
"message": "Invalid input: expected number, received number"
|
|
173
|
+
}
|
|
174
|
+
]],
|
|
183
175
|
"success": false,
|
|
184
176
|
}
|
|
185
177
|
`);
|
|
@@ -40,20 +40,18 @@ test("continue on non-fatal errors", () => {
|
|
|
40
40
|
|
|
41
41
|
expect(schema.safeParse("4321")).toMatchInlineSnapshot(`
|
|
42
42
|
{
|
|
43
|
-
"error": ZodError
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
"path": [],
|
|
49
|
-
},
|
|
50
|
-
{
|
|
51
|
-
"code": "custom",
|
|
52
|
-
"message": "B",
|
|
53
|
-
"path": [],
|
|
54
|
-
},
|
|
55
|
-
],
|
|
43
|
+
"error": [ZodError: [
|
|
44
|
+
{
|
|
45
|
+
"code": "custom",
|
|
46
|
+
"path": [],
|
|
47
|
+
"message": "A"
|
|
56
48
|
},
|
|
49
|
+
{
|
|
50
|
+
"code": "custom",
|
|
51
|
+
"path": [],
|
|
52
|
+
"message": "B"
|
|
53
|
+
}
|
|
54
|
+
]],
|
|
57
55
|
"success": false,
|
|
58
56
|
}
|
|
59
57
|
`);
|
|
@@ -70,15 +68,13 @@ test("break on fatal errors", () => {
|
|
|
70
68
|
|
|
71
69
|
expect(schema.safeParse("4321")).toMatchInlineSnapshot(`
|
|
72
70
|
{
|
|
73
|
-
"error": ZodError
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
],
|
|
81
|
-
},
|
|
71
|
+
"error": [ZodError: [
|
|
72
|
+
{
|
|
73
|
+
"code": "custom",
|
|
74
|
+
"path": [],
|
|
75
|
+
"message": "A"
|
|
76
|
+
}
|
|
77
|
+
]],
|
|
82
78
|
"success": false,
|
|
83
79
|
}
|
|
84
80
|
`);
|