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
|
@@ -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
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
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
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
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
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
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
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
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
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
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
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
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
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
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
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
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
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
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
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
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
|
`);
|
|
@@ -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
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
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
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
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
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
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
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
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
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
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
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
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
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
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
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
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
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
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
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
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
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
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
|
|
package/src/v4/core/errors.ts
CHANGED
|
@@ -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
|
|
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() {
|
package/src/v4/core/parse.ts
CHANGED
|
@@ -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.$
|
|
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.$
|
|
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);
|