zod 3.16.0 → 3.17.2
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/README.md +218 -183
- package/lib/ZodError.d.ts +1 -2
- package/lib/ZodError.js +1 -2
- package/lib/external.d.ts +1 -0
- package/lib/external.js +4 -0
- package/lib/helpers/parseUtil.d.ts +2 -25
- package/lib/helpers/parseUtil.js +1 -67
- package/lib/helpers/util.d.ts +24 -0
- package/lib/helpers/util.js +66 -1
- package/lib/index.mjs +91 -72
- package/lib/index.umd.js +91 -72
- package/lib/types.d.ts +9 -5
- package/lib/types.js +80 -61
- package/package.json +2 -2
package/lib/types.js
CHANGED
|
@@ -80,13 +80,13 @@ class ZodType {
|
|
|
80
80
|
return this._def.description;
|
|
81
81
|
}
|
|
82
82
|
_getType(input) {
|
|
83
|
-
return (0,
|
|
83
|
+
return (0, util_1.getParsedType)(input.data);
|
|
84
84
|
}
|
|
85
85
|
_getOrReturnCtx(input, ctx) {
|
|
86
86
|
return (ctx || {
|
|
87
87
|
common: input.parent.common,
|
|
88
88
|
data: input.data,
|
|
89
|
-
parsedType: (0,
|
|
89
|
+
parsedType: (0, util_1.getParsedType)(input.data),
|
|
90
90
|
schemaErrorMap: this._def.errorMap,
|
|
91
91
|
path: input.path,
|
|
92
92
|
parent: input.parent,
|
|
@@ -98,7 +98,7 @@ class ZodType {
|
|
|
98
98
|
ctx: {
|
|
99
99
|
common: input.parent.common,
|
|
100
100
|
data: input.data,
|
|
101
|
-
parsedType: (0,
|
|
101
|
+
parsedType: (0, util_1.getParsedType)(input.data),
|
|
102
102
|
schemaErrorMap: this._def.errorMap,
|
|
103
103
|
path: input.path,
|
|
104
104
|
parent: input.parent,
|
|
@@ -134,7 +134,7 @@ class ZodType {
|
|
|
134
134
|
schemaErrorMap: this._def.errorMap,
|
|
135
135
|
parent: null,
|
|
136
136
|
data,
|
|
137
|
-
parsedType: (0,
|
|
137
|
+
parsedType: (0, util_1.getParsedType)(data),
|
|
138
138
|
};
|
|
139
139
|
const result = this._parseSync({ data, path: ctx.path, parent: ctx });
|
|
140
140
|
return handleResult(ctx, result);
|
|
@@ -156,7 +156,7 @@ class ZodType {
|
|
|
156
156
|
schemaErrorMap: this._def.errorMap,
|
|
157
157
|
parent: null,
|
|
158
158
|
data,
|
|
159
|
-
parsedType: (0,
|
|
159
|
+
parsedType: (0, util_1.getParsedType)(data),
|
|
160
160
|
};
|
|
161
161
|
const maybeAsyncResult = this._parse({ data, path: [], parent: ctx });
|
|
162
162
|
const result = await ((0, parseUtil_1.isAsync)(maybeAsyncResult)
|
|
@@ -291,18 +291,22 @@ class ZodString extends ZodType {
|
|
|
291
291
|
...errorUtil_1.errorUtil.errToObj(message),
|
|
292
292
|
});
|
|
293
293
|
/**
|
|
294
|
-
*
|
|
295
|
-
*
|
|
294
|
+
* @deprecated Use z.string().min(1) instead.
|
|
295
|
+
* @see {@link ZodString.min}
|
|
296
296
|
*/
|
|
297
297
|
this.nonempty = (message) => this.min(1, errorUtil_1.errorUtil.errToObj(message));
|
|
298
|
+
this.trim = () => new ZodString({
|
|
299
|
+
...this._def,
|
|
300
|
+
checks: [...this._def.checks, { kind: "trim" }],
|
|
301
|
+
});
|
|
298
302
|
}
|
|
299
303
|
_parse(input) {
|
|
300
304
|
const parsedType = this._getType(input);
|
|
301
|
-
if (parsedType !==
|
|
305
|
+
if (parsedType !== util_1.ZodParsedType.string) {
|
|
302
306
|
const ctx = this._getOrReturnCtx(input);
|
|
303
307
|
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
304
308
|
code: ZodError_1.ZodIssueCode.invalid_type,
|
|
305
|
-
expected:
|
|
309
|
+
expected: util_1.ZodParsedType.string,
|
|
306
310
|
received: ctx.parsedType,
|
|
307
311
|
}
|
|
308
312
|
//
|
|
@@ -398,6 +402,12 @@ class ZodString extends ZodType {
|
|
|
398
402
|
status.dirty();
|
|
399
403
|
}
|
|
400
404
|
}
|
|
405
|
+
else if (check.kind === "trim") {
|
|
406
|
+
input.data = input.data.trim();
|
|
407
|
+
}
|
|
408
|
+
else {
|
|
409
|
+
util_1.util.assertNever(check);
|
|
410
|
+
}
|
|
401
411
|
}
|
|
402
412
|
return { status: status.value, value: input.data };
|
|
403
413
|
}
|
|
@@ -504,11 +514,11 @@ class ZodNumber extends ZodType {
|
|
|
504
514
|
}
|
|
505
515
|
_parse(input) {
|
|
506
516
|
const parsedType = this._getType(input);
|
|
507
|
-
if (parsedType !==
|
|
517
|
+
if (parsedType !== util_1.ZodParsedType.number) {
|
|
508
518
|
const ctx = this._getOrReturnCtx(input);
|
|
509
519
|
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
510
520
|
code: ZodError_1.ZodIssueCode.invalid_type,
|
|
511
|
-
expected:
|
|
521
|
+
expected: util_1.ZodParsedType.number,
|
|
512
522
|
received: ctx.parsedType,
|
|
513
523
|
});
|
|
514
524
|
return parseUtil_1.INVALID;
|
|
@@ -689,11 +699,11 @@ ZodNumber.create = (params) => {
|
|
|
689
699
|
class ZodBigInt extends ZodType {
|
|
690
700
|
_parse(input) {
|
|
691
701
|
const parsedType = this._getType(input);
|
|
692
|
-
if (parsedType !==
|
|
702
|
+
if (parsedType !== util_1.ZodParsedType.bigint) {
|
|
693
703
|
const ctx = this._getOrReturnCtx(input);
|
|
694
704
|
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
695
705
|
code: ZodError_1.ZodIssueCode.invalid_type,
|
|
696
|
-
expected:
|
|
706
|
+
expected: util_1.ZodParsedType.bigint,
|
|
697
707
|
received: ctx.parsedType,
|
|
698
708
|
});
|
|
699
709
|
return parseUtil_1.INVALID;
|
|
@@ -711,11 +721,11 @@ ZodBigInt.create = (params) => {
|
|
|
711
721
|
class ZodBoolean extends ZodType {
|
|
712
722
|
_parse(input) {
|
|
713
723
|
const parsedType = this._getType(input);
|
|
714
|
-
if (parsedType !==
|
|
724
|
+
if (parsedType !== util_1.ZodParsedType.boolean) {
|
|
715
725
|
const ctx = this._getOrReturnCtx(input);
|
|
716
726
|
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
717
727
|
code: ZodError_1.ZodIssueCode.invalid_type,
|
|
718
|
-
expected:
|
|
728
|
+
expected: util_1.ZodParsedType.boolean,
|
|
719
729
|
received: ctx.parsedType,
|
|
720
730
|
});
|
|
721
731
|
return parseUtil_1.INVALID;
|
|
@@ -733,11 +743,11 @@ ZodBoolean.create = (params) => {
|
|
|
733
743
|
class ZodDate extends ZodType {
|
|
734
744
|
_parse(input) {
|
|
735
745
|
const parsedType = this._getType(input);
|
|
736
|
-
if (parsedType !==
|
|
746
|
+
if (parsedType !== util_1.ZodParsedType.date) {
|
|
737
747
|
const ctx = this._getOrReturnCtx(input);
|
|
738
748
|
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
739
749
|
code: ZodError_1.ZodIssueCode.invalid_type,
|
|
740
|
-
expected:
|
|
750
|
+
expected: util_1.ZodParsedType.date,
|
|
741
751
|
received: ctx.parsedType,
|
|
742
752
|
});
|
|
743
753
|
return parseUtil_1.INVALID;
|
|
@@ -765,11 +775,11 @@ ZodDate.create = (params) => {
|
|
|
765
775
|
class ZodUndefined extends ZodType {
|
|
766
776
|
_parse(input) {
|
|
767
777
|
const parsedType = this._getType(input);
|
|
768
|
-
if (parsedType !==
|
|
778
|
+
if (parsedType !== util_1.ZodParsedType.undefined) {
|
|
769
779
|
const ctx = this._getOrReturnCtx(input);
|
|
770
780
|
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
771
781
|
code: ZodError_1.ZodIssueCode.invalid_type,
|
|
772
|
-
expected:
|
|
782
|
+
expected: util_1.ZodParsedType.undefined,
|
|
773
783
|
received: ctx.parsedType,
|
|
774
784
|
});
|
|
775
785
|
return parseUtil_1.INVALID;
|
|
@@ -787,11 +797,11 @@ ZodUndefined.create = (params) => {
|
|
|
787
797
|
class ZodNull extends ZodType {
|
|
788
798
|
_parse(input) {
|
|
789
799
|
const parsedType = this._getType(input);
|
|
790
|
-
if (parsedType !==
|
|
800
|
+
if (parsedType !== util_1.ZodParsedType.null) {
|
|
791
801
|
const ctx = this._getOrReturnCtx(input);
|
|
792
802
|
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
793
803
|
code: ZodError_1.ZodIssueCode.invalid_type,
|
|
794
|
-
expected:
|
|
804
|
+
expected: util_1.ZodParsedType.null,
|
|
795
805
|
received: ctx.parsedType,
|
|
796
806
|
});
|
|
797
807
|
return parseUtil_1.INVALID;
|
|
@@ -845,7 +855,7 @@ class ZodNever extends ZodType {
|
|
|
845
855
|
const ctx = this._getOrReturnCtx(input);
|
|
846
856
|
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
847
857
|
code: ZodError_1.ZodIssueCode.invalid_type,
|
|
848
|
-
expected:
|
|
858
|
+
expected: util_1.ZodParsedType.never,
|
|
849
859
|
received: ctx.parsedType,
|
|
850
860
|
});
|
|
851
861
|
return parseUtil_1.INVALID;
|
|
@@ -861,11 +871,11 @@ ZodNever.create = (params) => {
|
|
|
861
871
|
class ZodVoid extends ZodType {
|
|
862
872
|
_parse(input) {
|
|
863
873
|
const parsedType = this._getType(input);
|
|
864
|
-
if (parsedType !==
|
|
874
|
+
if (parsedType !== util_1.ZodParsedType.undefined) {
|
|
865
875
|
const ctx = this._getOrReturnCtx(input);
|
|
866
876
|
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
867
877
|
code: ZodError_1.ZodIssueCode.invalid_type,
|
|
868
|
-
expected:
|
|
878
|
+
expected: util_1.ZodParsedType.void,
|
|
869
879
|
received: ctx.parsedType,
|
|
870
880
|
});
|
|
871
881
|
return parseUtil_1.INVALID;
|
|
@@ -884,10 +894,10 @@ class ZodArray extends ZodType {
|
|
|
884
894
|
_parse(input) {
|
|
885
895
|
const { ctx, status } = this._processInputParams(input);
|
|
886
896
|
const def = this._def;
|
|
887
|
-
if (ctx.parsedType !==
|
|
897
|
+
if (ctx.parsedType !== util_1.ZodParsedType.array) {
|
|
888
898
|
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
889
899
|
code: ZodError_1.ZodIssueCode.invalid_type,
|
|
890
|
-
expected:
|
|
900
|
+
expected: util_1.ZodParsedType.array,
|
|
891
901
|
received: ctx.parsedType,
|
|
892
902
|
});
|
|
893
903
|
return parseUtil_1.INVALID;
|
|
@@ -1034,11 +1044,11 @@ class ZodObject extends ZodType {
|
|
|
1034
1044
|
}
|
|
1035
1045
|
_parse(input) {
|
|
1036
1046
|
const parsedType = this._getType(input);
|
|
1037
|
-
if (parsedType !==
|
|
1047
|
+
if (parsedType !== util_1.ZodParsedType.object) {
|
|
1038
1048
|
const ctx = this._getOrReturnCtx(input);
|
|
1039
1049
|
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
1040
1050
|
code: ZodError_1.ZodIssueCode.invalid_type,
|
|
1041
|
-
expected:
|
|
1051
|
+
expected: util_1.ZodParsedType.object,
|
|
1042
1052
|
received: ctx.parsedType,
|
|
1043
1053
|
});
|
|
1044
1054
|
return parseUtil_1.INVALID;
|
|
@@ -1188,7 +1198,9 @@ class ZodObject extends ZodType {
|
|
|
1188
1198
|
pick(mask) {
|
|
1189
1199
|
const shape = {};
|
|
1190
1200
|
util_1.util.objectKeys(mask).map((key) => {
|
|
1191
|
-
shape
|
|
1201
|
+
// only add to shape if key corresponds to an element of the current shape
|
|
1202
|
+
if (this.shape[key])
|
|
1203
|
+
shape[key] = this.shape[key];
|
|
1192
1204
|
});
|
|
1193
1205
|
return new ZodObject({
|
|
1194
1206
|
...this._def,
|
|
@@ -1381,10 +1393,10 @@ ZodUnion.create = (types, params) => {
|
|
|
1381
1393
|
class ZodDiscriminatedUnion extends ZodType {
|
|
1382
1394
|
_parse(input) {
|
|
1383
1395
|
const { ctx } = this._processInputParams(input);
|
|
1384
|
-
if (ctx.parsedType !==
|
|
1396
|
+
if (ctx.parsedType !== util_1.ZodParsedType.object) {
|
|
1385
1397
|
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
1386
1398
|
code: ZodError_1.ZodIssueCode.invalid_type,
|
|
1387
|
-
expected:
|
|
1399
|
+
expected: util_1.ZodParsedType.object,
|
|
1388
1400
|
received: ctx.parsedType,
|
|
1389
1401
|
});
|
|
1390
1402
|
return parseUtil_1.INVALID;
|
|
@@ -1458,12 +1470,12 @@ class ZodDiscriminatedUnion extends ZodType {
|
|
|
1458
1470
|
}
|
|
1459
1471
|
exports.ZodDiscriminatedUnion = ZodDiscriminatedUnion;
|
|
1460
1472
|
function mergeValues(a, b) {
|
|
1461
|
-
const aType = (0,
|
|
1462
|
-
const bType = (0,
|
|
1473
|
+
const aType = (0, util_1.getParsedType)(a);
|
|
1474
|
+
const bType = (0, util_1.getParsedType)(b);
|
|
1463
1475
|
if (a === b) {
|
|
1464
1476
|
return { valid: true, data: a };
|
|
1465
1477
|
}
|
|
1466
|
-
else if (aType ===
|
|
1478
|
+
else if (aType === util_1.ZodParsedType.object && bType === util_1.ZodParsedType.object) {
|
|
1467
1479
|
const bKeys = util_1.util.objectKeys(b);
|
|
1468
1480
|
const sharedKeys = util_1.util
|
|
1469
1481
|
.objectKeys(a)
|
|
@@ -1478,7 +1490,7 @@ function mergeValues(a, b) {
|
|
|
1478
1490
|
}
|
|
1479
1491
|
return { valid: true, data: newObj };
|
|
1480
1492
|
}
|
|
1481
|
-
else if (aType ===
|
|
1493
|
+
else if (aType === util_1.ZodParsedType.array && bType === util_1.ZodParsedType.array) {
|
|
1482
1494
|
if (a.length !== b.length) {
|
|
1483
1495
|
return { valid: false };
|
|
1484
1496
|
}
|
|
@@ -1494,8 +1506,8 @@ function mergeValues(a, b) {
|
|
|
1494
1506
|
}
|
|
1495
1507
|
return { valid: true, data: newArray };
|
|
1496
1508
|
}
|
|
1497
|
-
else if (aType ===
|
|
1498
|
-
bType ===
|
|
1509
|
+
else if (aType === util_1.ZodParsedType.date &&
|
|
1510
|
+
bType === util_1.ZodParsedType.date &&
|
|
1499
1511
|
+a === +b) {
|
|
1500
1512
|
return { valid: true, data: a };
|
|
1501
1513
|
}
|
|
@@ -1561,10 +1573,10 @@ ZodIntersection.create = (left, right, params) => {
|
|
|
1561
1573
|
class ZodTuple extends ZodType {
|
|
1562
1574
|
_parse(input) {
|
|
1563
1575
|
const { status, ctx } = this._processInputParams(input);
|
|
1564
|
-
if (ctx.parsedType !==
|
|
1576
|
+
if (ctx.parsedType !== util_1.ZodParsedType.array) {
|
|
1565
1577
|
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
1566
1578
|
code: ZodError_1.ZodIssueCode.invalid_type,
|
|
1567
|
-
expected:
|
|
1579
|
+
expected: util_1.ZodParsedType.array,
|
|
1568
1580
|
received: ctx.parsedType,
|
|
1569
1581
|
});
|
|
1570
1582
|
return parseUtil_1.INVALID;
|
|
@@ -1633,10 +1645,10 @@ class ZodRecord extends ZodType {
|
|
|
1633
1645
|
}
|
|
1634
1646
|
_parse(input) {
|
|
1635
1647
|
const { status, ctx } = this._processInputParams(input);
|
|
1636
|
-
if (ctx.parsedType !==
|
|
1648
|
+
if (ctx.parsedType !== util_1.ZodParsedType.object) {
|
|
1637
1649
|
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
1638
1650
|
code: ZodError_1.ZodIssueCode.invalid_type,
|
|
1639
|
-
expected:
|
|
1651
|
+
expected: util_1.ZodParsedType.object,
|
|
1640
1652
|
received: ctx.parsedType,
|
|
1641
1653
|
});
|
|
1642
1654
|
return parseUtil_1.INVALID;
|
|
@@ -1681,10 +1693,10 @@ exports.ZodRecord = ZodRecord;
|
|
|
1681
1693
|
class ZodMap extends ZodType {
|
|
1682
1694
|
_parse(input) {
|
|
1683
1695
|
const { status, ctx } = this._processInputParams(input);
|
|
1684
|
-
if (ctx.parsedType !==
|
|
1696
|
+
if (ctx.parsedType !== util_1.ZodParsedType.map) {
|
|
1685
1697
|
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
1686
1698
|
code: ZodError_1.ZodIssueCode.invalid_type,
|
|
1687
|
-
expected:
|
|
1699
|
+
expected: util_1.ZodParsedType.map,
|
|
1688
1700
|
received: ctx.parsedType,
|
|
1689
1701
|
});
|
|
1690
1702
|
return parseUtil_1.INVALID;
|
|
@@ -1743,10 +1755,10 @@ ZodMap.create = (keyType, valueType, params) => {
|
|
|
1743
1755
|
class ZodSet extends ZodType {
|
|
1744
1756
|
_parse(input) {
|
|
1745
1757
|
const { status, ctx } = this._processInputParams(input);
|
|
1746
|
-
if (ctx.parsedType !==
|
|
1758
|
+
if (ctx.parsedType !== util_1.ZodParsedType.set) {
|
|
1747
1759
|
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
1748
1760
|
code: ZodError_1.ZodIssueCode.invalid_type,
|
|
1749
|
-
expected:
|
|
1761
|
+
expected: util_1.ZodParsedType.set,
|
|
1750
1762
|
received: ctx.parsedType,
|
|
1751
1763
|
});
|
|
1752
1764
|
return parseUtil_1.INVALID;
|
|
@@ -1832,10 +1844,10 @@ class ZodFunction extends ZodType {
|
|
|
1832
1844
|
}
|
|
1833
1845
|
_parse(input) {
|
|
1834
1846
|
const { ctx } = this._processInputParams(input);
|
|
1835
|
-
if (ctx.parsedType !==
|
|
1847
|
+
if (ctx.parsedType !== util_1.ZodParsedType.function) {
|
|
1836
1848
|
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
1837
1849
|
code: ZodError_1.ZodIssueCode.invalid_type,
|
|
1838
|
-
expected:
|
|
1850
|
+
expected: util_1.ZodParsedType.function,
|
|
1839
1851
|
received: ctx.parsedType,
|
|
1840
1852
|
});
|
|
1841
1853
|
return parseUtil_1.INVALID;
|
|
@@ -1988,10 +2000,11 @@ ZodLiteral.create = (value, params) => {
|
|
|
1988
2000
|
...processCreateParams(params),
|
|
1989
2001
|
});
|
|
1990
2002
|
};
|
|
1991
|
-
function createZodEnum(values) {
|
|
2003
|
+
function createZodEnum(values, params) {
|
|
1992
2004
|
return new ZodEnum({
|
|
1993
2005
|
values: values,
|
|
1994
2006
|
typeName: ZodFirstPartyTypeKind.ZodEnum,
|
|
2007
|
+
...processCreateParams(params),
|
|
1995
2008
|
});
|
|
1996
2009
|
}
|
|
1997
2010
|
class ZodEnum extends ZodType {
|
|
@@ -2049,8 +2062,8 @@ class ZodNativeEnum extends ZodType {
|
|
|
2049
2062
|
_parse(input) {
|
|
2050
2063
|
const nativeEnumValues = util_1.util.getValidEnumValues(this._def.values);
|
|
2051
2064
|
const ctx = this._getOrReturnCtx(input);
|
|
2052
|
-
if (ctx.parsedType !==
|
|
2053
|
-
ctx.parsedType !==
|
|
2065
|
+
if (ctx.parsedType !== util_1.ZodParsedType.string &&
|
|
2066
|
+
ctx.parsedType !== util_1.ZodParsedType.number) {
|
|
2054
2067
|
const expectedValues = util_1.util.objectValues(nativeEnumValues);
|
|
2055
2068
|
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
2056
2069
|
expected: util_1.util.joinValues(expectedValues),
|
|
@@ -2085,16 +2098,16 @@ ZodNativeEnum.create = (values, params) => {
|
|
|
2085
2098
|
class ZodPromise extends ZodType {
|
|
2086
2099
|
_parse(input) {
|
|
2087
2100
|
const { ctx } = this._processInputParams(input);
|
|
2088
|
-
if (ctx.parsedType !==
|
|
2101
|
+
if (ctx.parsedType !== util_1.ZodParsedType.promise &&
|
|
2089
2102
|
ctx.common.async === false) {
|
|
2090
2103
|
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
2091
2104
|
code: ZodError_1.ZodIssueCode.invalid_type,
|
|
2092
|
-
expected:
|
|
2105
|
+
expected: util_1.ZodParsedType.promise,
|
|
2093
2106
|
received: ctx.parsedType,
|
|
2094
2107
|
});
|
|
2095
2108
|
return parseUtil_1.INVALID;
|
|
2096
2109
|
}
|
|
2097
|
-
const promisified = ctx.parsedType ===
|
|
2110
|
+
const promisified = ctx.parsedType === util_1.ZodParsedType.promise
|
|
2098
2111
|
? ctx.data
|
|
2099
2112
|
: Promise.resolve(ctx.data);
|
|
2100
2113
|
return (0, parseUtil_1.OK)(promisified.then((data) => {
|
|
@@ -2252,7 +2265,7 @@ ZodEffects.createWithPreprocess = (preprocess, schema, params) => {
|
|
|
2252
2265
|
class ZodOptional extends ZodType {
|
|
2253
2266
|
_parse(input) {
|
|
2254
2267
|
const parsedType = this._getType(input);
|
|
2255
|
-
if (parsedType ===
|
|
2268
|
+
if (parsedType === util_1.ZodParsedType.undefined) {
|
|
2256
2269
|
return (0, parseUtil_1.OK)(undefined);
|
|
2257
2270
|
}
|
|
2258
2271
|
return this._def.innerType._parse(input);
|
|
@@ -2272,7 +2285,7 @@ ZodOptional.create = (type, params) => {
|
|
|
2272
2285
|
class ZodNullable extends ZodType {
|
|
2273
2286
|
_parse(input) {
|
|
2274
2287
|
const parsedType = this._getType(input);
|
|
2275
|
-
if (parsedType ===
|
|
2288
|
+
if (parsedType === util_1.ZodParsedType.null) {
|
|
2276
2289
|
return (0, parseUtil_1.OK)(null);
|
|
2277
2290
|
}
|
|
2278
2291
|
return this._def.innerType._parse(input);
|
|
@@ -2293,7 +2306,7 @@ class ZodDefault extends ZodType {
|
|
|
2293
2306
|
_parse(input) {
|
|
2294
2307
|
const { ctx } = this._processInputParams(input);
|
|
2295
2308
|
let data = ctx.data;
|
|
2296
|
-
if (ctx.parsedType ===
|
|
2309
|
+
if (ctx.parsedType === util_1.ZodParsedType.undefined) {
|
|
2297
2310
|
data = this._def.defaultValue();
|
|
2298
2311
|
}
|
|
2299
2312
|
return this._def.innerType._parse({
|
|
@@ -2317,11 +2330,11 @@ ZodDefault.create = (type, params) => {
|
|
|
2317
2330
|
class ZodNaN extends ZodType {
|
|
2318
2331
|
_parse(input) {
|
|
2319
2332
|
const parsedType = this._getType(input);
|
|
2320
|
-
if (parsedType !==
|
|
2333
|
+
if (parsedType !== util_1.ZodParsedType.nan) {
|
|
2321
2334
|
const ctx = this._getOrReturnCtx(input);
|
|
2322
2335
|
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
2323
2336
|
code: ZodError_1.ZodIssueCode.invalid_type,
|
|
2324
|
-
expected:
|
|
2337
|
+
expected: util_1.ZodParsedType.nan,
|
|
2325
2338
|
received: ctx.parsedType,
|
|
2326
2339
|
});
|
|
2327
2340
|
return parseUtil_1.INVALID;
|
|
@@ -2336,9 +2349,15 @@ ZodNaN.create = (params) => {
|
|
|
2336
2349
|
...processCreateParams(params),
|
|
2337
2350
|
});
|
|
2338
2351
|
};
|
|
2339
|
-
const custom = (check, params) => {
|
|
2352
|
+
const custom = (check, params = {}, fatal) => {
|
|
2340
2353
|
if (check)
|
|
2341
|
-
return ZodAny.create().
|
|
2354
|
+
return ZodAny.create().superRefine((data, ctx) => {
|
|
2355
|
+
if (!check(data)) {
|
|
2356
|
+
const p = typeof params === "function" ? params(data) : params;
|
|
2357
|
+
const p2 = typeof p === "string" ? { message: p } : p;
|
|
2358
|
+
ctx.addIssue({ code: "custom", ...p2, fatal });
|
|
2359
|
+
}
|
|
2360
|
+
});
|
|
2342
2361
|
return ZodAny.create();
|
|
2343
2362
|
};
|
|
2344
2363
|
exports.custom = custom;
|
|
@@ -2381,7 +2400,7 @@ var ZodFirstPartyTypeKind;
|
|
|
2381
2400
|
})(ZodFirstPartyTypeKind = exports.ZodFirstPartyTypeKind || (exports.ZodFirstPartyTypeKind = {}));
|
|
2382
2401
|
const instanceOfType = (cls, params = {
|
|
2383
2402
|
message: `Input not instance of ${cls.name}`,
|
|
2384
|
-
}) => (0, exports.custom)((data) => data instanceof cls, params);
|
|
2403
|
+
}) => (0, exports.custom)((data) => data instanceof cls, params, true);
|
|
2385
2404
|
exports.instanceof = instanceOfType;
|
|
2386
2405
|
const stringType = ZodString.create;
|
|
2387
2406
|
exports.string = stringType;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "zod",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.17.2",
|
|
4
4
|
"description": "TypeScript-first schema declaration and validation library with static type inference",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"types": "./index.d.ts",
|
|
@@ -51,7 +51,7 @@
|
|
|
51
51
|
"fix": "yarn lint:fix && yarn prettier:fix",
|
|
52
52
|
"clean": "rm -rf lib/* deno/lib/*",
|
|
53
53
|
"build": "yarn run clean && npm run build:cjs && npm run build:esm && npm run build:deno",
|
|
54
|
-
"build:deno": "node ./deno/build.mjs",
|
|
54
|
+
"build:deno": "node ./deno/build.mjs && cp ./README.md ./deno/lib",
|
|
55
55
|
"build:esm": "rollup --config rollup.config.js",
|
|
56
56
|
"build:cjs": "tsc --p tsconfig.cjs.json",
|
|
57
57
|
"build:types": "tsc --p tsconfig.types.json",
|