zod 3.14.0 → 3.14.1
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/lib/benchmarks/index.js +2 -0
- package/lib/benchmarks/primitives.d.ts +5 -0
- package/lib/benchmarks/primitives.js +78 -0
- package/lib/helpers/parseUtil.d.ts +6 -3
- package/lib/helpers/parseUtil.js +2 -2
- package/lib/index.mjs +152 -99
- package/lib/types.d.ts +2 -0
- package/lib/types.js +150 -97
- package/package.json +1 -1
package/lib/index.mjs
CHANGED
|
@@ -343,13 +343,13 @@ function addIssueToContext(ctx, issueData) {
|
|
|
343
343
|
data: ctx.data,
|
|
344
344
|
path: ctx.path,
|
|
345
345
|
errorMaps: [
|
|
346
|
-
ctx.contextualErrorMap,
|
|
346
|
+
ctx.common.contextualErrorMap,
|
|
347
347
|
ctx.schemaErrorMap,
|
|
348
348
|
overrideErrorMap,
|
|
349
349
|
defaultErrorMap, // then global default map
|
|
350
350
|
].filter((x) => !!x),
|
|
351
351
|
});
|
|
352
|
-
ctx.issues.push(issue);
|
|
352
|
+
ctx.common.issues.push(issue);
|
|
353
353
|
}
|
|
354
354
|
class ParseStatus {
|
|
355
355
|
constructor() {
|
|
@@ -424,10 +424,10 @@ const handleResult = (ctx, result) => {
|
|
|
424
424
|
return { success: true, data: result.value };
|
|
425
425
|
}
|
|
426
426
|
else {
|
|
427
|
-
if (!ctx.issues.length) {
|
|
427
|
+
if (!ctx.common.issues.length) {
|
|
428
428
|
throw new Error("Validation failed but no issues detected.");
|
|
429
429
|
}
|
|
430
|
-
const error = new ZodError(ctx.issues);
|
|
430
|
+
const error = new ZodError(ctx.common.issues);
|
|
431
431
|
return { success: false, error };
|
|
432
432
|
}
|
|
433
433
|
};
|
|
@@ -481,11 +481,24 @@ class ZodType {
|
|
|
481
481
|
get description() {
|
|
482
482
|
return this._def.description;
|
|
483
483
|
}
|
|
484
|
+
_getType(input) {
|
|
485
|
+
return getParsedType(input.data);
|
|
486
|
+
}
|
|
487
|
+
_getOrReturnCtx(input, ctx) {
|
|
488
|
+
return (ctx || {
|
|
489
|
+
common: input.parent.common,
|
|
490
|
+
data: input.data,
|
|
491
|
+
parsedType: getParsedType(input.data),
|
|
492
|
+
schemaErrorMap: this._def.errorMap,
|
|
493
|
+
path: input.path,
|
|
494
|
+
parent: input.parent,
|
|
495
|
+
});
|
|
496
|
+
}
|
|
484
497
|
_processInputParams(input) {
|
|
485
498
|
return {
|
|
486
499
|
status: new ParseStatus(),
|
|
487
500
|
ctx: {
|
|
488
|
-
|
|
501
|
+
common: input.parent.common,
|
|
489
502
|
data: input.data,
|
|
490
503
|
parsedType: getParsedType(input.data),
|
|
491
504
|
schemaErrorMap: this._def.errorMap,
|
|
@@ -514,11 +527,14 @@ class ZodType {
|
|
|
514
527
|
safeParse(data, params) {
|
|
515
528
|
var _a;
|
|
516
529
|
const ctx = {
|
|
530
|
+
common: {
|
|
531
|
+
issues: [],
|
|
532
|
+
async: (_a = params === null || params === void 0 ? void 0 : params.async) !== null && _a !== void 0 ? _a : false,
|
|
533
|
+
typeCache: typeof Map !== "undefined" ? new Map() : undefined,
|
|
534
|
+
contextualErrorMap: params === null || params === void 0 ? void 0 : params.errorMap,
|
|
535
|
+
},
|
|
517
536
|
path: (params === null || params === void 0 ? void 0 : params.path) || [],
|
|
518
|
-
issues: [],
|
|
519
|
-
contextualErrorMap: params === null || params === void 0 ? void 0 : params.errorMap,
|
|
520
537
|
schemaErrorMap: this._def.errorMap,
|
|
521
|
-
async: (_a = params === null || params === void 0 ? void 0 : params.async) !== null && _a !== void 0 ? _a : false,
|
|
522
538
|
parent: null,
|
|
523
539
|
data,
|
|
524
540
|
parsedType: getParsedType(data),
|
|
@@ -534,11 +550,14 @@ class ZodType {
|
|
|
534
550
|
}
|
|
535
551
|
async safeParseAsync(data, params) {
|
|
536
552
|
const ctx = {
|
|
553
|
+
common: {
|
|
554
|
+
issues: [],
|
|
555
|
+
contextualErrorMap: params === null || params === void 0 ? void 0 : params.errorMap,
|
|
556
|
+
async: true,
|
|
557
|
+
typeCache: typeof Map !== "undefined" ? new Map() : undefined,
|
|
558
|
+
},
|
|
537
559
|
path: (params === null || params === void 0 ? void 0 : params.path) || [],
|
|
538
|
-
issues: [],
|
|
539
|
-
contextualErrorMap: params === null || params === void 0 ? void 0 : params.errorMap,
|
|
540
560
|
schemaErrorMap: this._def.errorMap,
|
|
541
|
-
async: true,
|
|
542
561
|
parent: null,
|
|
543
562
|
data,
|
|
544
563
|
parsedType: getParsedType(data),
|
|
@@ -679,8 +698,9 @@ class ZodString extends ZodType {
|
|
|
679
698
|
this.nonempty = (message) => this.min(1, errorUtil.errToObj(message));
|
|
680
699
|
}
|
|
681
700
|
_parse(input) {
|
|
682
|
-
const
|
|
683
|
-
if (
|
|
701
|
+
const parsedType = this._getType(input);
|
|
702
|
+
if (parsedType !== ZodParsedType.string) {
|
|
703
|
+
const ctx = this._getOrReturnCtx(input);
|
|
684
704
|
addIssueToContext(ctx, {
|
|
685
705
|
code: ZodIssueCode.invalid_type,
|
|
686
706
|
expected: ZodParsedType.string,
|
|
@@ -690,9 +710,12 @@ class ZodString extends ZodType {
|
|
|
690
710
|
);
|
|
691
711
|
return INVALID;
|
|
692
712
|
}
|
|
713
|
+
const status = new ParseStatus();
|
|
714
|
+
let ctx = undefined;
|
|
693
715
|
for (const check of this._def.checks) {
|
|
694
716
|
if (check.kind === "min") {
|
|
695
|
-
if (
|
|
717
|
+
if (input.data.length < check.value) {
|
|
718
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
696
719
|
addIssueToContext(ctx, {
|
|
697
720
|
code: ZodIssueCode.too_small,
|
|
698
721
|
minimum: check.value,
|
|
@@ -704,7 +727,8 @@ class ZodString extends ZodType {
|
|
|
704
727
|
}
|
|
705
728
|
}
|
|
706
729
|
else if (check.kind === "max") {
|
|
707
|
-
if (
|
|
730
|
+
if (input.data.length > check.value) {
|
|
731
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
708
732
|
addIssueToContext(ctx, {
|
|
709
733
|
code: ZodIssueCode.too_big,
|
|
710
734
|
maximum: check.value,
|
|
@@ -716,7 +740,8 @@ class ZodString extends ZodType {
|
|
|
716
740
|
}
|
|
717
741
|
}
|
|
718
742
|
else if (check.kind === "email") {
|
|
719
|
-
if (!emailRegex.test(
|
|
743
|
+
if (!emailRegex.test(input.data)) {
|
|
744
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
720
745
|
addIssueToContext(ctx, {
|
|
721
746
|
validation: "email",
|
|
722
747
|
code: ZodIssueCode.invalid_string,
|
|
@@ -726,7 +751,8 @@ class ZodString extends ZodType {
|
|
|
726
751
|
}
|
|
727
752
|
}
|
|
728
753
|
else if (check.kind === "uuid") {
|
|
729
|
-
if (!uuidRegex.test(
|
|
754
|
+
if (!uuidRegex.test(input.data)) {
|
|
755
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
730
756
|
addIssueToContext(ctx, {
|
|
731
757
|
validation: "uuid",
|
|
732
758
|
code: ZodIssueCode.invalid_string,
|
|
@@ -736,7 +762,8 @@ class ZodString extends ZodType {
|
|
|
736
762
|
}
|
|
737
763
|
}
|
|
738
764
|
else if (check.kind === "cuid") {
|
|
739
|
-
if (!cuidRegex.test(
|
|
765
|
+
if (!cuidRegex.test(input.data)) {
|
|
766
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
740
767
|
addIssueToContext(ctx, {
|
|
741
768
|
validation: "cuid",
|
|
742
769
|
code: ZodIssueCode.invalid_string,
|
|
@@ -747,9 +774,10 @@ class ZodString extends ZodType {
|
|
|
747
774
|
}
|
|
748
775
|
else if (check.kind === "url") {
|
|
749
776
|
try {
|
|
750
|
-
new URL(
|
|
777
|
+
new URL(input.data);
|
|
751
778
|
}
|
|
752
779
|
catch (_a) {
|
|
780
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
753
781
|
addIssueToContext(ctx, {
|
|
754
782
|
validation: "url",
|
|
755
783
|
code: ZodIssueCode.invalid_string,
|
|
@@ -760,8 +788,9 @@ class ZodString extends ZodType {
|
|
|
760
788
|
}
|
|
761
789
|
else if (check.kind === "regex") {
|
|
762
790
|
check.regex.lastIndex = 0;
|
|
763
|
-
const testResult = check.regex.test(
|
|
791
|
+
const testResult = check.regex.test(input.data);
|
|
764
792
|
if (!testResult) {
|
|
793
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
765
794
|
addIssueToContext(ctx, {
|
|
766
795
|
validation: "regex",
|
|
767
796
|
code: ZodIssueCode.invalid_string,
|
|
@@ -771,7 +800,7 @@ class ZodString extends ZodType {
|
|
|
771
800
|
}
|
|
772
801
|
}
|
|
773
802
|
}
|
|
774
|
-
return { status: status.value, value:
|
|
803
|
+
return { status: status.value, value: input.data };
|
|
775
804
|
}
|
|
776
805
|
_addCheck(check) {
|
|
777
806
|
return new ZodString({
|
|
@@ -874,8 +903,9 @@ class ZodNumber extends ZodType {
|
|
|
874
903
|
this.step = this.multipleOf;
|
|
875
904
|
}
|
|
876
905
|
_parse(input) {
|
|
877
|
-
const
|
|
878
|
-
if (
|
|
906
|
+
const parsedType = this._getType(input);
|
|
907
|
+
if (parsedType !== ZodParsedType.number) {
|
|
908
|
+
const ctx = this._getOrReturnCtx(input);
|
|
879
909
|
addIssueToContext(ctx, {
|
|
880
910
|
code: ZodIssueCode.invalid_type,
|
|
881
911
|
expected: ZodParsedType.number,
|
|
@@ -883,9 +913,12 @@ class ZodNumber extends ZodType {
|
|
|
883
913
|
});
|
|
884
914
|
return INVALID;
|
|
885
915
|
}
|
|
916
|
+
let ctx = undefined;
|
|
917
|
+
const status = new ParseStatus();
|
|
886
918
|
for (const check of this._def.checks) {
|
|
887
919
|
if (check.kind === "int") {
|
|
888
|
-
if (!util.isInteger(
|
|
920
|
+
if (!util.isInteger(input.data)) {
|
|
921
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
889
922
|
addIssueToContext(ctx, {
|
|
890
923
|
code: ZodIssueCode.invalid_type,
|
|
891
924
|
expected: "integer",
|
|
@@ -897,9 +930,10 @@ class ZodNumber extends ZodType {
|
|
|
897
930
|
}
|
|
898
931
|
else if (check.kind === "min") {
|
|
899
932
|
const tooSmall = check.inclusive
|
|
900
|
-
?
|
|
901
|
-
:
|
|
933
|
+
? input.data < check.value
|
|
934
|
+
: input.data <= check.value;
|
|
902
935
|
if (tooSmall) {
|
|
936
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
903
937
|
addIssueToContext(ctx, {
|
|
904
938
|
code: ZodIssueCode.too_small,
|
|
905
939
|
minimum: check.value,
|
|
@@ -912,9 +946,10 @@ class ZodNumber extends ZodType {
|
|
|
912
946
|
}
|
|
913
947
|
else if (check.kind === "max") {
|
|
914
948
|
const tooBig = check.inclusive
|
|
915
|
-
?
|
|
916
|
-
:
|
|
949
|
+
? input.data > check.value
|
|
950
|
+
: input.data >= check.value;
|
|
917
951
|
if (tooBig) {
|
|
952
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
918
953
|
addIssueToContext(ctx, {
|
|
919
954
|
code: ZodIssueCode.too_big,
|
|
920
955
|
maximum: check.value,
|
|
@@ -926,7 +961,8 @@ class ZodNumber extends ZodType {
|
|
|
926
961
|
}
|
|
927
962
|
}
|
|
928
963
|
else if (check.kind === "multipleOf") {
|
|
929
|
-
if (floatSafeRemainder(
|
|
964
|
+
if (floatSafeRemainder(input.data, check.value) !== 0) {
|
|
965
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
930
966
|
addIssueToContext(ctx, {
|
|
931
967
|
code: ZodIssueCode.not_multiple_of,
|
|
932
968
|
multipleOf: check.value,
|
|
@@ -939,7 +975,7 @@ class ZodNumber extends ZodType {
|
|
|
939
975
|
util.assertNever(check);
|
|
940
976
|
}
|
|
941
977
|
}
|
|
942
|
-
return { status: status.value, value:
|
|
978
|
+
return { status: status.value, value: input.data };
|
|
943
979
|
}
|
|
944
980
|
gte(value, message) {
|
|
945
981
|
return this.setLimit("min", value, true, errorUtil.toString(message));
|
|
@@ -1051,8 +1087,9 @@ ZodNumber.create = (params) => {
|
|
|
1051
1087
|
};
|
|
1052
1088
|
class ZodBigInt extends ZodType {
|
|
1053
1089
|
_parse(input) {
|
|
1054
|
-
const
|
|
1055
|
-
if (
|
|
1090
|
+
const parsedType = this._getType(input);
|
|
1091
|
+
if (parsedType !== ZodParsedType.bigint) {
|
|
1092
|
+
const ctx = this._getOrReturnCtx(input);
|
|
1056
1093
|
addIssueToContext(ctx, {
|
|
1057
1094
|
code: ZodIssueCode.invalid_type,
|
|
1058
1095
|
expected: ZodParsedType.bigint,
|
|
@@ -1060,7 +1097,7 @@ class ZodBigInt extends ZodType {
|
|
|
1060
1097
|
});
|
|
1061
1098
|
return INVALID;
|
|
1062
1099
|
}
|
|
1063
|
-
return OK(
|
|
1100
|
+
return OK(input.data);
|
|
1064
1101
|
}
|
|
1065
1102
|
}
|
|
1066
1103
|
ZodBigInt.create = (params) => {
|
|
@@ -1071,8 +1108,9 @@ ZodBigInt.create = (params) => {
|
|
|
1071
1108
|
};
|
|
1072
1109
|
class ZodBoolean extends ZodType {
|
|
1073
1110
|
_parse(input) {
|
|
1074
|
-
const
|
|
1075
|
-
if (
|
|
1111
|
+
const parsedType = this._getType(input);
|
|
1112
|
+
if (parsedType !== ZodParsedType.boolean) {
|
|
1113
|
+
const ctx = this._getOrReturnCtx(input);
|
|
1076
1114
|
addIssueToContext(ctx, {
|
|
1077
1115
|
code: ZodIssueCode.invalid_type,
|
|
1078
1116
|
expected: ZodParsedType.boolean,
|
|
@@ -1080,7 +1118,7 @@ class ZodBoolean extends ZodType {
|
|
|
1080
1118
|
});
|
|
1081
1119
|
return INVALID;
|
|
1082
1120
|
}
|
|
1083
|
-
return OK(
|
|
1121
|
+
return OK(input.data);
|
|
1084
1122
|
}
|
|
1085
1123
|
}
|
|
1086
1124
|
ZodBoolean.create = (params) => {
|
|
@@ -1091,8 +1129,9 @@ ZodBoolean.create = (params) => {
|
|
|
1091
1129
|
};
|
|
1092
1130
|
class ZodDate extends ZodType {
|
|
1093
1131
|
_parse(input) {
|
|
1094
|
-
const
|
|
1095
|
-
if (
|
|
1132
|
+
const parsedType = this._getType(input);
|
|
1133
|
+
if (parsedType !== ZodParsedType.date) {
|
|
1134
|
+
const ctx = this._getOrReturnCtx(input);
|
|
1096
1135
|
addIssueToContext(ctx, {
|
|
1097
1136
|
code: ZodIssueCode.invalid_type,
|
|
1098
1137
|
expected: ZodParsedType.date,
|
|
@@ -1100,15 +1139,16 @@ class ZodDate extends ZodType {
|
|
|
1100
1139
|
});
|
|
1101
1140
|
return INVALID;
|
|
1102
1141
|
}
|
|
1103
|
-
if (isNaN(
|
|
1142
|
+
if (isNaN(input.data.getTime())) {
|
|
1143
|
+
const ctx = this._getOrReturnCtx(input);
|
|
1104
1144
|
addIssueToContext(ctx, {
|
|
1105
1145
|
code: ZodIssueCode.invalid_date,
|
|
1106
1146
|
});
|
|
1107
1147
|
return INVALID;
|
|
1108
1148
|
}
|
|
1109
1149
|
return {
|
|
1110
|
-
status:
|
|
1111
|
-
value: new Date(
|
|
1150
|
+
status: "valid",
|
|
1151
|
+
value: new Date(input.data.getTime()),
|
|
1112
1152
|
};
|
|
1113
1153
|
}
|
|
1114
1154
|
}
|
|
@@ -1120,8 +1160,9 @@ ZodDate.create = (params) => {
|
|
|
1120
1160
|
};
|
|
1121
1161
|
class ZodUndefined extends ZodType {
|
|
1122
1162
|
_parse(input) {
|
|
1123
|
-
const
|
|
1124
|
-
if (
|
|
1163
|
+
const parsedType = this._getType(input);
|
|
1164
|
+
if (parsedType !== ZodParsedType.undefined) {
|
|
1165
|
+
const ctx = this._getOrReturnCtx(input);
|
|
1125
1166
|
addIssueToContext(ctx, {
|
|
1126
1167
|
code: ZodIssueCode.invalid_type,
|
|
1127
1168
|
expected: ZodParsedType.undefined,
|
|
@@ -1129,7 +1170,7 @@ class ZodUndefined extends ZodType {
|
|
|
1129
1170
|
});
|
|
1130
1171
|
return INVALID;
|
|
1131
1172
|
}
|
|
1132
|
-
return OK(
|
|
1173
|
+
return OK(input.data);
|
|
1133
1174
|
}
|
|
1134
1175
|
}
|
|
1135
1176
|
ZodUndefined.create = (params) => {
|
|
@@ -1140,8 +1181,9 @@ ZodUndefined.create = (params) => {
|
|
|
1140
1181
|
};
|
|
1141
1182
|
class ZodNull extends ZodType {
|
|
1142
1183
|
_parse(input) {
|
|
1143
|
-
const
|
|
1144
|
-
if (
|
|
1184
|
+
const parsedType = this._getType(input);
|
|
1185
|
+
if (parsedType !== ZodParsedType.null) {
|
|
1186
|
+
const ctx = this._getOrReturnCtx(input);
|
|
1145
1187
|
addIssueToContext(ctx, {
|
|
1146
1188
|
code: ZodIssueCode.invalid_type,
|
|
1147
1189
|
expected: ZodParsedType.null,
|
|
@@ -1149,7 +1191,7 @@ class ZodNull extends ZodType {
|
|
|
1149
1191
|
});
|
|
1150
1192
|
return INVALID;
|
|
1151
1193
|
}
|
|
1152
|
-
return OK(
|
|
1194
|
+
return OK(input.data);
|
|
1153
1195
|
}
|
|
1154
1196
|
}
|
|
1155
1197
|
ZodNull.create = (params) => {
|
|
@@ -1165,8 +1207,7 @@ class ZodAny extends ZodType {
|
|
|
1165
1207
|
this._any = true;
|
|
1166
1208
|
}
|
|
1167
1209
|
_parse(input) {
|
|
1168
|
-
|
|
1169
|
-
return OK(ctx.data);
|
|
1210
|
+
return OK(input.data);
|
|
1170
1211
|
}
|
|
1171
1212
|
}
|
|
1172
1213
|
ZodAny.create = (params) => {
|
|
@@ -1182,8 +1223,7 @@ class ZodUnknown extends ZodType {
|
|
|
1182
1223
|
this._unknown = true;
|
|
1183
1224
|
}
|
|
1184
1225
|
_parse(input) {
|
|
1185
|
-
|
|
1186
|
-
return OK(ctx.data);
|
|
1226
|
+
return OK(input.data);
|
|
1187
1227
|
}
|
|
1188
1228
|
}
|
|
1189
1229
|
ZodUnknown.create = (params) => {
|
|
@@ -1194,7 +1234,7 @@ ZodUnknown.create = (params) => {
|
|
|
1194
1234
|
};
|
|
1195
1235
|
class ZodNever extends ZodType {
|
|
1196
1236
|
_parse(input) {
|
|
1197
|
-
const
|
|
1237
|
+
const ctx = this._getOrReturnCtx(input);
|
|
1198
1238
|
addIssueToContext(ctx, {
|
|
1199
1239
|
code: ZodIssueCode.invalid_type,
|
|
1200
1240
|
expected: ZodParsedType.never,
|
|
@@ -1211,8 +1251,9 @@ ZodNever.create = (params) => {
|
|
|
1211
1251
|
};
|
|
1212
1252
|
class ZodVoid extends ZodType {
|
|
1213
1253
|
_parse(input) {
|
|
1214
|
-
const
|
|
1215
|
-
if (
|
|
1254
|
+
const parsedType = this._getType(input);
|
|
1255
|
+
if (parsedType !== ZodParsedType.undefined) {
|
|
1256
|
+
const ctx = this._getOrReturnCtx(input);
|
|
1216
1257
|
addIssueToContext(ctx, {
|
|
1217
1258
|
code: ZodIssueCode.invalid_type,
|
|
1218
1259
|
expected: ZodParsedType.void,
|
|
@@ -1220,7 +1261,7 @@ class ZodVoid extends ZodType {
|
|
|
1220
1261
|
});
|
|
1221
1262
|
return INVALID;
|
|
1222
1263
|
}
|
|
1223
|
-
return OK(
|
|
1264
|
+
return OK(input.data);
|
|
1224
1265
|
}
|
|
1225
1266
|
}
|
|
1226
1267
|
ZodVoid.create = (params) => {
|
|
@@ -1231,7 +1272,7 @@ ZodVoid.create = (params) => {
|
|
|
1231
1272
|
};
|
|
1232
1273
|
class ZodArray extends ZodType {
|
|
1233
1274
|
_parse(input) {
|
|
1234
|
-
const {
|
|
1275
|
+
const { ctx, status } = this._processInputParams(input);
|
|
1235
1276
|
const def = this._def;
|
|
1236
1277
|
if (ctx.parsedType !== ZodParsedType.array) {
|
|
1237
1278
|
addIssueToContext(ctx, {
|
|
@@ -1265,7 +1306,7 @@ class ZodArray extends ZodType {
|
|
|
1265
1306
|
status.dirty();
|
|
1266
1307
|
}
|
|
1267
1308
|
}
|
|
1268
|
-
if (ctx.async) {
|
|
1309
|
+
if (ctx.common.async) {
|
|
1269
1310
|
return Promise.all(ctx.data.map((item, i) => {
|
|
1270
1311
|
return def.type._parseAsync({
|
|
1271
1312
|
parent: ctx,
|
|
@@ -1389,8 +1430,9 @@ class ZodObject extends ZodType {
|
|
|
1389
1430
|
return (this._cached = { shape, keys });
|
|
1390
1431
|
}
|
|
1391
1432
|
_parse(input) {
|
|
1392
|
-
const
|
|
1393
|
-
if (
|
|
1433
|
+
const parsedType = this._getType(input);
|
|
1434
|
+
if (parsedType !== ZodParsedType.object) {
|
|
1435
|
+
const ctx = this._getOrReturnCtx(input);
|
|
1394
1436
|
addIssueToContext(ctx, {
|
|
1395
1437
|
code: ZodIssueCode.invalid_type,
|
|
1396
1438
|
expected: ZodParsedType.object,
|
|
@@ -1398,6 +1440,7 @@ class ZodObject extends ZodType {
|
|
|
1398
1440
|
});
|
|
1399
1441
|
return INVALID;
|
|
1400
1442
|
}
|
|
1443
|
+
const { status, ctx } = this._processInputParams(input);
|
|
1401
1444
|
const { shape, keys: shapeKeys } = this._getCached();
|
|
1402
1445
|
const dataKeys = util.objectKeys(ctx.data);
|
|
1403
1446
|
const extraKeys = dataKeys.filter((k) => !shapeKeys.includes(k));
|
|
@@ -1452,7 +1495,7 @@ class ZodObject extends ZodType {
|
|
|
1452
1495
|
});
|
|
1453
1496
|
}
|
|
1454
1497
|
}
|
|
1455
|
-
if (ctx.async) {
|
|
1498
|
+
if (ctx.common.async) {
|
|
1456
1499
|
return Promise.resolve()
|
|
1457
1500
|
.then(async () => {
|
|
1458
1501
|
const syncPairs = [];
|
|
@@ -1647,23 +1690,26 @@ class ZodUnion extends ZodType {
|
|
|
1647
1690
|
for (const result of results) {
|
|
1648
1691
|
if (result.result.status === "dirty") {
|
|
1649
1692
|
// add issues from dirty option
|
|
1650
|
-
ctx.issues.push(...result.ctx.issues);
|
|
1693
|
+
ctx.common.issues.push(...result.ctx.common.issues);
|
|
1651
1694
|
return result.result;
|
|
1652
1695
|
}
|
|
1653
1696
|
}
|
|
1654
1697
|
// return invalid
|
|
1655
|
-
const unionErrors = results.map((result) => new ZodError(result.ctx.issues));
|
|
1698
|
+
const unionErrors = results.map((result) => new ZodError(result.ctx.common.issues));
|
|
1656
1699
|
addIssueToContext(ctx, {
|
|
1657
1700
|
code: ZodIssueCode.invalid_union,
|
|
1658
1701
|
unionErrors,
|
|
1659
1702
|
});
|
|
1660
1703
|
return INVALID;
|
|
1661
1704
|
}
|
|
1662
|
-
if (ctx.async) {
|
|
1705
|
+
if (ctx.common.async) {
|
|
1663
1706
|
return Promise.all(options.map(async (option) => {
|
|
1664
1707
|
const childCtx = {
|
|
1665
1708
|
...ctx,
|
|
1666
|
-
|
|
1709
|
+
common: {
|
|
1710
|
+
...ctx.common,
|
|
1711
|
+
issues: [],
|
|
1712
|
+
},
|
|
1667
1713
|
parent: null,
|
|
1668
1714
|
};
|
|
1669
1715
|
return {
|
|
@@ -1682,7 +1728,10 @@ class ZodUnion extends ZodType {
|
|
|
1682
1728
|
for (const option of options) {
|
|
1683
1729
|
const childCtx = {
|
|
1684
1730
|
...ctx,
|
|
1685
|
-
|
|
1731
|
+
common: {
|
|
1732
|
+
...ctx.common,
|
|
1733
|
+
issues: [],
|
|
1734
|
+
},
|
|
1686
1735
|
parent: null,
|
|
1687
1736
|
};
|
|
1688
1737
|
const result = option._parseSync({
|
|
@@ -1696,12 +1745,12 @@ class ZodUnion extends ZodType {
|
|
|
1696
1745
|
else if (result.status === "dirty" && !dirty) {
|
|
1697
1746
|
dirty = { result, ctx: childCtx };
|
|
1698
1747
|
}
|
|
1699
|
-
if (childCtx.issues.length) {
|
|
1700
|
-
issues.push(childCtx.issues);
|
|
1748
|
+
if (childCtx.common.issues.length) {
|
|
1749
|
+
issues.push(childCtx.common.issues);
|
|
1701
1750
|
}
|
|
1702
1751
|
}
|
|
1703
1752
|
if (dirty) {
|
|
1704
|
-
ctx.issues.push(...dirty.ctx.issues);
|
|
1753
|
+
ctx.common.issues.push(...dirty.ctx.common.issues);
|
|
1705
1754
|
return dirty.result;
|
|
1706
1755
|
}
|
|
1707
1756
|
const unionErrors = issues.map((issues) => new ZodError(issues));
|
|
@@ -1745,7 +1794,7 @@ class ZodDiscriminatedUnion extends ZodType {
|
|
|
1745
1794
|
});
|
|
1746
1795
|
return INVALID;
|
|
1747
1796
|
}
|
|
1748
|
-
if (ctx.async) {
|
|
1797
|
+
if (ctx.common.async) {
|
|
1749
1798
|
return option._parseAsync({
|
|
1750
1799
|
data: ctx.data,
|
|
1751
1800
|
path: ctx.path,
|
|
@@ -1866,7 +1915,7 @@ class ZodIntersection extends ZodType {
|
|
|
1866
1915
|
}
|
|
1867
1916
|
return { status: status.value, value: merged.data };
|
|
1868
1917
|
};
|
|
1869
|
-
if (ctx.async) {
|
|
1918
|
+
if (ctx.common.async) {
|
|
1870
1919
|
return Promise.all([
|
|
1871
1920
|
this._def.left._parseAsync({
|
|
1872
1921
|
data: ctx.data,
|
|
@@ -1943,7 +1992,7 @@ class ZodTuple extends ZodType {
|
|
|
1943
1992
|
});
|
|
1944
1993
|
})
|
|
1945
1994
|
.filter((x) => !!x); // filter nulls
|
|
1946
|
-
if (ctx.async) {
|
|
1995
|
+
if (ctx.common.async) {
|
|
1947
1996
|
return Promise.all(items).then((results) => {
|
|
1948
1997
|
return ParseStatus.mergeArray(status, results);
|
|
1949
1998
|
});
|
|
@@ -2004,7 +2053,7 @@ class ZodRecord extends ZodType {
|
|
|
2004
2053
|
}),
|
|
2005
2054
|
});
|
|
2006
2055
|
}
|
|
2007
|
-
if (ctx.async) {
|
|
2056
|
+
if (ctx.common.async) {
|
|
2008
2057
|
return ParseStatus.mergeObjectAsync(status, pairs);
|
|
2009
2058
|
}
|
|
2010
2059
|
else {
|
|
@@ -2058,7 +2107,7 @@ class ZodMap extends ZodType {
|
|
|
2058
2107
|
}),
|
|
2059
2108
|
};
|
|
2060
2109
|
});
|
|
2061
|
-
if (ctx.async) {
|
|
2110
|
+
if (ctx.common.async) {
|
|
2062
2111
|
const finalMap = new Map();
|
|
2063
2112
|
return Promise.resolve().then(async () => {
|
|
2064
2113
|
for (const pair of pairs) {
|
|
@@ -2149,7 +2198,7 @@ class ZodSet extends ZodType {
|
|
|
2149
2198
|
return { status: status.value, value: parsedSet };
|
|
2150
2199
|
}
|
|
2151
2200
|
const elements = [...ctx.data.values()].map((item, i) => valueType._parse({ data: item, path: [...ctx.path, i], parent: ctx }));
|
|
2152
|
-
if (ctx.async) {
|
|
2201
|
+
if (ctx.common.async) {
|
|
2153
2202
|
return Promise.all(elements).then((elements) => finalizeSet(elements));
|
|
2154
2203
|
}
|
|
2155
2204
|
else {
|
|
@@ -2204,7 +2253,7 @@ class ZodFunction extends ZodType {
|
|
|
2204
2253
|
data: args,
|
|
2205
2254
|
path: ctx.path,
|
|
2206
2255
|
errorMaps: [
|
|
2207
|
-
ctx.contextualErrorMap,
|
|
2256
|
+
ctx.common.contextualErrorMap,
|
|
2208
2257
|
ctx.schemaErrorMap,
|
|
2209
2258
|
overrideErrorMap,
|
|
2210
2259
|
defaultErrorMap,
|
|
@@ -2220,7 +2269,7 @@ class ZodFunction extends ZodType {
|
|
|
2220
2269
|
data: returns,
|
|
2221
2270
|
path: ctx.path,
|
|
2222
2271
|
errorMaps: [
|
|
2223
|
-
ctx.contextualErrorMap,
|
|
2272
|
+
ctx.common.contextualErrorMap,
|
|
2224
2273
|
ctx.schemaErrorMap,
|
|
2225
2274
|
overrideErrorMap,
|
|
2226
2275
|
defaultErrorMap,
|
|
@@ -2231,7 +2280,7 @@ class ZodFunction extends ZodType {
|
|
|
2231
2280
|
},
|
|
2232
2281
|
});
|
|
2233
2282
|
}
|
|
2234
|
-
const params = { errorMap: ctx.contextualErrorMap };
|
|
2283
|
+
const params = { errorMap: ctx.common.contextualErrorMap };
|
|
2235
2284
|
const fn = ctx.data;
|
|
2236
2285
|
if (this._def.returns instanceof ZodPromise) {
|
|
2237
2286
|
return OK(async (...args) => {
|
|
@@ -2323,8 +2372,8 @@ ZodLazy.create = (getter, params) => {
|
|
|
2323
2372
|
};
|
|
2324
2373
|
class ZodLiteral extends ZodType {
|
|
2325
2374
|
_parse(input) {
|
|
2326
|
-
|
|
2327
|
-
|
|
2375
|
+
if (input.data !== this._def.value) {
|
|
2376
|
+
const ctx = this._getOrReturnCtx(input);
|
|
2328
2377
|
addIssueToContext(ctx, {
|
|
2329
2378
|
code: ZodIssueCode.invalid_type,
|
|
2330
2379
|
expected: getParsedType(this._def.value),
|
|
@@ -2332,7 +2381,7 @@ class ZodLiteral extends ZodType {
|
|
|
2332
2381
|
});
|
|
2333
2382
|
return INVALID;
|
|
2334
2383
|
}
|
|
2335
|
-
return { status:
|
|
2384
|
+
return { status: "valid", value: input.data };
|
|
2336
2385
|
}
|
|
2337
2386
|
get value() {
|
|
2338
2387
|
return this._def.value;
|
|
@@ -2353,15 +2402,15 @@ function createZodEnum(values) {
|
|
|
2353
2402
|
}
|
|
2354
2403
|
class ZodEnum extends ZodType {
|
|
2355
2404
|
_parse(input) {
|
|
2356
|
-
|
|
2357
|
-
|
|
2405
|
+
if (this._def.values.indexOf(input.data) === -1) {
|
|
2406
|
+
const ctx = this._getOrReturnCtx(input);
|
|
2358
2407
|
addIssueToContext(ctx, {
|
|
2359
2408
|
code: ZodIssueCode.invalid_enum_value,
|
|
2360
2409
|
options: this._def.values,
|
|
2361
2410
|
});
|
|
2362
2411
|
return INVALID;
|
|
2363
2412
|
}
|
|
2364
|
-
return OK(
|
|
2413
|
+
return OK(input.data);
|
|
2365
2414
|
}
|
|
2366
2415
|
get options() {
|
|
2367
2416
|
return this._def.values;
|
|
@@ -2391,16 +2440,16 @@ class ZodEnum extends ZodType {
|
|
|
2391
2440
|
ZodEnum.create = createZodEnum;
|
|
2392
2441
|
class ZodNativeEnum extends ZodType {
|
|
2393
2442
|
_parse(input) {
|
|
2394
|
-
const { ctx } = this._processInputParams(input);
|
|
2395
2443
|
const nativeEnumValues = util.getValidEnumValues(this._def.values);
|
|
2396
|
-
if (nativeEnumValues.indexOf(
|
|
2444
|
+
if (nativeEnumValues.indexOf(input.data) === -1) {
|
|
2445
|
+
const ctx = this._getOrReturnCtx(input);
|
|
2397
2446
|
addIssueToContext(ctx, {
|
|
2398
2447
|
code: ZodIssueCode.invalid_enum_value,
|
|
2399
2448
|
options: util.objectValues(nativeEnumValues),
|
|
2400
2449
|
});
|
|
2401
2450
|
return INVALID;
|
|
2402
2451
|
}
|
|
2403
|
-
return OK(
|
|
2452
|
+
return OK(input.data);
|
|
2404
2453
|
}
|
|
2405
2454
|
get enum() {
|
|
2406
2455
|
return this._def.values;
|
|
@@ -2416,7 +2465,8 @@ ZodNativeEnum.create = (values, params) => {
|
|
|
2416
2465
|
class ZodPromise extends ZodType {
|
|
2417
2466
|
_parse(input) {
|
|
2418
2467
|
const { ctx } = this._processInputParams(input);
|
|
2419
|
-
if (ctx.parsedType !== ZodParsedType.promise &&
|
|
2468
|
+
if (ctx.parsedType !== ZodParsedType.promise &&
|
|
2469
|
+
ctx.common.async === false) {
|
|
2420
2470
|
addIssueToContext(ctx, {
|
|
2421
2471
|
code: ZodIssueCode.invalid_type,
|
|
2422
2472
|
expected: ZodParsedType.promise,
|
|
@@ -2430,7 +2480,7 @@ class ZodPromise extends ZodType {
|
|
|
2430
2480
|
return OK(promisified.then((data) => {
|
|
2431
2481
|
return this._def.type.parseAsync(data, {
|
|
2432
2482
|
path: ctx.path,
|
|
2433
|
-
errorMap: ctx.contextualErrorMap,
|
|
2483
|
+
errorMap: ctx.common.contextualErrorMap,
|
|
2434
2484
|
});
|
|
2435
2485
|
}));
|
|
2436
2486
|
}
|
|
@@ -2451,7 +2501,7 @@ class ZodEffects extends ZodType {
|
|
|
2451
2501
|
const effect = this._def.effect || null;
|
|
2452
2502
|
if (effect.type === "preprocess") {
|
|
2453
2503
|
const processed = effect.transform(ctx.data);
|
|
2454
|
-
if (ctx.async) {
|
|
2504
|
+
if (ctx.common.async) {
|
|
2455
2505
|
return Promise.resolve(processed).then((processed) => {
|
|
2456
2506
|
return this._def.schema._parseAsync({
|
|
2457
2507
|
data: processed,
|
|
@@ -2488,7 +2538,7 @@ class ZodEffects extends ZodType {
|
|
|
2488
2538
|
// effect: RefinementEffect<any>
|
|
2489
2539
|
) => {
|
|
2490
2540
|
const result = effect.refinement(acc, checkCtx);
|
|
2491
|
-
if (ctx.async) {
|
|
2541
|
+
if (ctx.common.async) {
|
|
2492
2542
|
return Promise.resolve(result);
|
|
2493
2543
|
}
|
|
2494
2544
|
if (result instanceof Promise) {
|
|
@@ -2496,7 +2546,7 @@ class ZodEffects extends ZodType {
|
|
|
2496
2546
|
}
|
|
2497
2547
|
return acc;
|
|
2498
2548
|
};
|
|
2499
|
-
if (ctx.async === false) {
|
|
2549
|
+
if (ctx.common.async === false) {
|
|
2500
2550
|
const inner = this._def.schema._parseSync({
|
|
2501
2551
|
data: ctx.data,
|
|
2502
2552
|
path: ctx.path,
|
|
@@ -2525,7 +2575,7 @@ class ZodEffects extends ZodType {
|
|
|
2525
2575
|
}
|
|
2526
2576
|
}
|
|
2527
2577
|
if (effect.type === "transform") {
|
|
2528
|
-
if (ctx.async === false) {
|
|
2578
|
+
if (ctx.common.async === false) {
|
|
2529
2579
|
const base = this._def.schema._parseSync({
|
|
2530
2580
|
data: ctx.data,
|
|
2531
2581
|
path: ctx.path,
|
|
@@ -2578,10 +2628,11 @@ ZodEffects.createWithPreprocess = (preprocess, schema, params) => {
|
|
|
2578
2628
|
};
|
|
2579
2629
|
class ZodOptional extends ZodType {
|
|
2580
2630
|
_parse(input) {
|
|
2581
|
-
const
|
|
2582
|
-
if (
|
|
2631
|
+
const parsedType = this._getType(input);
|
|
2632
|
+
if (parsedType === ZodParsedType.undefined) {
|
|
2583
2633
|
return OK(undefined);
|
|
2584
2634
|
}
|
|
2635
|
+
const { ctx } = this._processInputParams(input);
|
|
2585
2636
|
return this._def.innerType._parse({
|
|
2586
2637
|
data: ctx.data,
|
|
2587
2638
|
path: ctx.path,
|
|
@@ -2601,10 +2652,11 @@ ZodOptional.create = (type, params) => {
|
|
|
2601
2652
|
};
|
|
2602
2653
|
class ZodNullable extends ZodType {
|
|
2603
2654
|
_parse(input) {
|
|
2604
|
-
const
|
|
2605
|
-
if (
|
|
2655
|
+
const parsedType = this._getType(input);
|
|
2656
|
+
if (parsedType === ZodParsedType.null) {
|
|
2606
2657
|
return OK(null);
|
|
2607
2658
|
}
|
|
2659
|
+
const { ctx } = this._processInputParams(input);
|
|
2608
2660
|
return this._def.innerType._parse({
|
|
2609
2661
|
data: ctx.data,
|
|
2610
2662
|
path: ctx.path,
|
|
@@ -2648,8 +2700,9 @@ ZodDefault.create = (type, params) => {
|
|
|
2648
2700
|
};
|
|
2649
2701
|
class ZodNaN extends ZodType {
|
|
2650
2702
|
_parse(input) {
|
|
2651
|
-
const
|
|
2652
|
-
if (
|
|
2703
|
+
const parsedType = this._getType(input);
|
|
2704
|
+
if (parsedType !== ZodParsedType.nan) {
|
|
2705
|
+
const ctx = this._getOrReturnCtx(input);
|
|
2653
2706
|
addIssueToContext(ctx, {
|
|
2654
2707
|
code: ZodIssueCode.invalid_type,
|
|
2655
2708
|
expected: ZodParsedType.nan,
|
|
@@ -2657,7 +2710,7 @@ class ZodNaN extends ZodType {
|
|
|
2657
2710
|
});
|
|
2658
2711
|
return INVALID;
|
|
2659
2712
|
}
|
|
2660
|
-
return { status:
|
|
2713
|
+
return { status: "valid", value: input.data };
|
|
2661
2714
|
}
|
|
2662
2715
|
}
|
|
2663
2716
|
ZodNaN.create = (params) => {
|