zod 3.20.6 → 3.21.4
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 +211 -18
- package/lib/ZodError.d.ts +17 -13
- package/lib/ZodError.js +0 -8
- package/lib/benchmarks/index.js +33 -8
- package/lib/helpers/parseUtil.js +1 -1
- package/lib/helpers/partialUtil.d.ts +4 -4
- package/lib/helpers/util.d.ts +25 -4
- package/lib/helpers/util.js +13 -4
- package/lib/index.d.ts +3 -3
- package/lib/index.js +3 -3
- package/lib/index.mjs +366 -56
- package/lib/index.umd.js +367 -57
- package/lib/locales/en.js +15 -3
- package/lib/types.d.ts +123 -86
- package/lib/types.js +329 -221
- package/package.json +55 -54
package/lib/index.mjs
CHANGED
|
@@ -61,6 +61,15 @@ var util;
|
|
|
61
61
|
return value;
|
|
62
62
|
};
|
|
63
63
|
})(util || (util = {}));
|
|
64
|
+
var objectUtil;
|
|
65
|
+
(function (objectUtil) {
|
|
66
|
+
objectUtil.mergeShapes = (first, second) => {
|
|
67
|
+
return {
|
|
68
|
+
...first,
|
|
69
|
+
...second, // second overwrites first
|
|
70
|
+
};
|
|
71
|
+
};
|
|
72
|
+
})(objectUtil || (objectUtil = {}));
|
|
64
73
|
const ZodParsedType = util.arrayToEnum([
|
|
65
74
|
"string",
|
|
66
75
|
"nan",
|
|
@@ -292,7 +301,13 @@ const errorMap = (issue, _ctx) => {
|
|
|
292
301
|
break;
|
|
293
302
|
case ZodIssueCode.invalid_string:
|
|
294
303
|
if (typeof issue.validation === "object") {
|
|
295
|
-
if ("
|
|
304
|
+
if ("includes" in issue.validation) {
|
|
305
|
+
message = `Invalid input: must include "${issue.validation.includes}"`;
|
|
306
|
+
if (typeof issue.validation.position === "number") {
|
|
307
|
+
message = `${message} at one or more positions greater than or equal to ${issue.validation.position}`;
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
else if ("startsWith" in issue.validation) {
|
|
296
311
|
message = `Invalid input: must start with "${issue.validation.startsWith}"`;
|
|
297
312
|
}
|
|
298
313
|
else if ("endsWith" in issue.validation) {
|
|
@@ -325,7 +340,7 @@ const errorMap = (issue, _ctx) => {
|
|
|
325
340
|
? `exactly equal to `
|
|
326
341
|
: issue.inclusive
|
|
327
342
|
? `greater than or equal to `
|
|
328
|
-
: `greater than `}${new Date(issue.minimum)}`;
|
|
343
|
+
: `greater than `}${new Date(Number(issue.minimum))}`;
|
|
329
344
|
else
|
|
330
345
|
message = "Invalid input";
|
|
331
346
|
break;
|
|
@@ -340,12 +355,18 @@ const errorMap = (issue, _ctx) => {
|
|
|
340
355
|
: issue.inclusive
|
|
341
356
|
? `less than or equal to`
|
|
342
357
|
: `less than`} ${issue.maximum}`;
|
|
358
|
+
else if (issue.type === "bigint")
|
|
359
|
+
message = `BigInt must be ${issue.exact
|
|
360
|
+
? `exactly`
|
|
361
|
+
: issue.inclusive
|
|
362
|
+
? `less than or equal to`
|
|
363
|
+
: `less than`} ${issue.maximum}`;
|
|
343
364
|
else if (issue.type === "date")
|
|
344
365
|
message = `Date must be ${issue.exact
|
|
345
366
|
? `exactly`
|
|
346
367
|
: issue.inclusive
|
|
347
368
|
? `smaller than or equal to`
|
|
348
|
-
: `smaller than`} ${new Date(issue.maximum)}`;
|
|
369
|
+
: `smaller than`} ${new Date(Number(issue.maximum))}`;
|
|
349
370
|
else
|
|
350
371
|
message = "Invalid input";
|
|
351
372
|
break;
|
|
@@ -482,13 +503,22 @@ var errorUtil;
|
|
|
482
503
|
|
|
483
504
|
class ParseInputLazyPath {
|
|
484
505
|
constructor(parent, value, path, key) {
|
|
506
|
+
this._cachedPath = [];
|
|
485
507
|
this.parent = parent;
|
|
486
508
|
this.data = value;
|
|
487
509
|
this._path = path;
|
|
488
510
|
this._key = key;
|
|
489
511
|
}
|
|
490
512
|
get path() {
|
|
491
|
-
|
|
513
|
+
if (!this._cachedPath.length) {
|
|
514
|
+
if (this._key instanceof Array) {
|
|
515
|
+
this._cachedPath.push(...this._path, ...this._key);
|
|
516
|
+
}
|
|
517
|
+
else {
|
|
518
|
+
this._cachedPath.push(...this._path, this._key);
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
return this._cachedPath;
|
|
492
522
|
}
|
|
493
523
|
}
|
|
494
524
|
const handleResult = (ctx, result) => {
|
|
@@ -499,8 +529,16 @@ const handleResult = (ctx, result) => {
|
|
|
499
529
|
if (!ctx.common.issues.length) {
|
|
500
530
|
throw new Error("Validation failed but no issues detected.");
|
|
501
531
|
}
|
|
502
|
-
|
|
503
|
-
|
|
532
|
+
return {
|
|
533
|
+
success: false,
|
|
534
|
+
get error() {
|
|
535
|
+
if (this._error)
|
|
536
|
+
return this._error;
|
|
537
|
+
const error = new ZodError(ctx.common.issues);
|
|
538
|
+
this._error = error;
|
|
539
|
+
return this._error;
|
|
540
|
+
},
|
|
541
|
+
};
|
|
504
542
|
}
|
|
505
543
|
};
|
|
506
544
|
function processCreateParams(params) {
|
|
@@ -773,6 +811,7 @@ class ZodType {
|
|
|
773
811
|
}
|
|
774
812
|
const cuidRegex = /^c[^\s-]{8,}$/i;
|
|
775
813
|
const cuid2Regex = /^[a-z][a-z0-9]*$/;
|
|
814
|
+
const ulidRegex = /[0-9A-HJKMNP-TV-Z]{26}/;
|
|
776
815
|
const uuidRegex = /^([a-f0-9]{8}-[a-f0-9]{4}-[1-5][a-f0-9]{3}-[a-f0-9]{4}-[a-f0-9]{12}|00000000-0000-0000-0000-000000000000)$/i;
|
|
777
816
|
// from https://stackoverflow.com/a/46181/1550155
|
|
778
817
|
// old version: too slow, didn't support unicode
|
|
@@ -780,13 +819,11 @@ const uuidRegex = /^([a-f0-9]{8}-[a-f0-9]{4}-[1-5][a-f0-9]{3}-[a-f0-9]{4}-[a-f0-
|
|
|
780
819
|
//old email regex
|
|
781
820
|
// const emailRegex = /^(([^<>()[\].,;:\s@"]+(\.[^<>()[\].,;:\s@"]+)*)|(".+"))@((?!-)([^<>()[\].,;:\s@"]+\.)+[^<>()[\].,;:\s@"]{1,})[^-<>()[\].,;:\s@"]$/i;
|
|
782
821
|
// eslint-disable-next-line
|
|
783
|
-
const emailRegex = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}
|
|
784
|
-
//
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
// any?: boolean;
|
|
789
|
-
// }
|
|
822
|
+
const emailRegex = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[(((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2}))\.){3}((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2}))\])|(\[IPv6:(([a-f0-9]{1,4}:){7}|::([a-f0-9]{1,4}:){0,6}|([a-f0-9]{1,4}:){1}:([a-f0-9]{1,4}:){0,5}|([a-f0-9]{1,4}:){2}:([a-f0-9]{1,4}:){0,4}|([a-f0-9]{1,4}:){3}:([a-f0-9]{1,4}:){0,3}|([a-f0-9]{1,4}:){4}:([a-f0-9]{1,4}:){0,2}|([a-f0-9]{1,4}:){5}:([a-f0-9]{1,4}:){0,1})([a-f0-9]{1,4}|(((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2}))\.){3}((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2})))\])|([A-Za-z0-9]([A-Za-z0-9-]*[A-Za-z0-9])*(\.[A-Za-z]{2,})+))$/;
|
|
823
|
+
// from https://thekevinscott.com/emojis-in-javascript/#writing-a-regular-expression
|
|
824
|
+
const emojiRegex = /^(\p{Extended_Pictographic}|\p{Emoji_Component})+$/u;
|
|
825
|
+
const ipv4Regex = /^(((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2}))\.){3}((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2}))$/;
|
|
826
|
+
const ipv6Regex = /^(([a-f0-9]{1,4}:){7}|::([a-f0-9]{1,4}:){0,6}|([a-f0-9]{1,4}:){1}:([a-f0-9]{1,4}:){0,5}|([a-f0-9]{1,4}:){2}:([a-f0-9]{1,4}:){0,4}|([a-f0-9]{1,4}:){3}:([a-f0-9]{1,4}:){0,3}|([a-f0-9]{1,4}:){4}:([a-f0-9]{1,4}:){0,2}|([a-f0-9]{1,4}:){5}:([a-f0-9]{1,4}:){0,1})([a-f0-9]{1,4}|(((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2}))\.){3}((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2})))$/;
|
|
790
827
|
// Adapted from https://stackoverflow.com/a/3143231
|
|
791
828
|
const datetimeRegex = (args) => {
|
|
792
829
|
if (args.precision) {
|
|
@@ -814,6 +851,15 @@ const datetimeRegex = (args) => {
|
|
|
814
851
|
}
|
|
815
852
|
}
|
|
816
853
|
};
|
|
854
|
+
function isValidIP(ip, version) {
|
|
855
|
+
if ((version === "v4" || !version) && ipv4Regex.test(ip)) {
|
|
856
|
+
return true;
|
|
857
|
+
}
|
|
858
|
+
if ((version === "v6" || !version) && ipv6Regex.test(ip)) {
|
|
859
|
+
return true;
|
|
860
|
+
}
|
|
861
|
+
return false;
|
|
862
|
+
}
|
|
817
863
|
class ZodString extends ZodType {
|
|
818
864
|
constructor() {
|
|
819
865
|
super(...arguments);
|
|
@@ -831,6 +877,14 @@ class ZodString extends ZodType {
|
|
|
831
877
|
...this._def,
|
|
832
878
|
checks: [...this._def.checks, { kind: "trim" }],
|
|
833
879
|
});
|
|
880
|
+
this.toLowerCase = () => new ZodString({
|
|
881
|
+
...this._def,
|
|
882
|
+
checks: [...this._def.checks, { kind: "toLowerCase" }],
|
|
883
|
+
});
|
|
884
|
+
this.toUpperCase = () => new ZodString({
|
|
885
|
+
...this._def,
|
|
886
|
+
checks: [...this._def.checks, { kind: "toUpperCase" }],
|
|
887
|
+
});
|
|
834
888
|
}
|
|
835
889
|
_parse(input) {
|
|
836
890
|
if (this._def.coerce) {
|
|
@@ -918,6 +972,17 @@ class ZodString extends ZodType {
|
|
|
918
972
|
status.dirty();
|
|
919
973
|
}
|
|
920
974
|
}
|
|
975
|
+
else if (check.kind === "emoji") {
|
|
976
|
+
if (!emojiRegex.test(input.data)) {
|
|
977
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
978
|
+
addIssueToContext(ctx, {
|
|
979
|
+
validation: "emoji",
|
|
980
|
+
code: ZodIssueCode.invalid_string,
|
|
981
|
+
message: check.message,
|
|
982
|
+
});
|
|
983
|
+
status.dirty();
|
|
984
|
+
}
|
|
985
|
+
}
|
|
921
986
|
else if (check.kind === "uuid") {
|
|
922
987
|
if (!uuidRegex.test(input.data)) {
|
|
923
988
|
ctx = this._getOrReturnCtx(input, ctx);
|
|
@@ -951,6 +1016,17 @@ class ZodString extends ZodType {
|
|
|
951
1016
|
status.dirty();
|
|
952
1017
|
}
|
|
953
1018
|
}
|
|
1019
|
+
else if (check.kind === "ulid") {
|
|
1020
|
+
if (!ulidRegex.test(input.data)) {
|
|
1021
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1022
|
+
addIssueToContext(ctx, {
|
|
1023
|
+
validation: "ulid",
|
|
1024
|
+
code: ZodIssueCode.invalid_string,
|
|
1025
|
+
message: check.message,
|
|
1026
|
+
});
|
|
1027
|
+
status.dirty();
|
|
1028
|
+
}
|
|
1029
|
+
}
|
|
954
1030
|
else if (check.kind === "url") {
|
|
955
1031
|
try {
|
|
956
1032
|
new URL(input.data);
|
|
@@ -981,6 +1057,23 @@ class ZodString extends ZodType {
|
|
|
981
1057
|
else if (check.kind === "trim") {
|
|
982
1058
|
input.data = input.data.trim();
|
|
983
1059
|
}
|
|
1060
|
+
else if (check.kind === "includes") {
|
|
1061
|
+
if (!input.data.includes(check.value, check.position)) {
|
|
1062
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1063
|
+
addIssueToContext(ctx, {
|
|
1064
|
+
code: ZodIssueCode.invalid_string,
|
|
1065
|
+
validation: { includes: check.value, position: check.position },
|
|
1066
|
+
message: check.message,
|
|
1067
|
+
});
|
|
1068
|
+
status.dirty();
|
|
1069
|
+
}
|
|
1070
|
+
}
|
|
1071
|
+
else if (check.kind === "toLowerCase") {
|
|
1072
|
+
input.data = input.data.toLowerCase();
|
|
1073
|
+
}
|
|
1074
|
+
else if (check.kind === "toUpperCase") {
|
|
1075
|
+
input.data = input.data.toUpperCase();
|
|
1076
|
+
}
|
|
984
1077
|
else if (check.kind === "startsWith") {
|
|
985
1078
|
if (!input.data.startsWith(check.value)) {
|
|
986
1079
|
ctx = this._getOrReturnCtx(input, ctx);
|
|
@@ -1015,6 +1108,17 @@ class ZodString extends ZodType {
|
|
|
1015
1108
|
status.dirty();
|
|
1016
1109
|
}
|
|
1017
1110
|
}
|
|
1111
|
+
else if (check.kind === "ip") {
|
|
1112
|
+
if (!isValidIP(input.data, check.version)) {
|
|
1113
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1114
|
+
addIssueToContext(ctx, {
|
|
1115
|
+
validation: "ip",
|
|
1116
|
+
code: ZodIssueCode.invalid_string,
|
|
1117
|
+
message: check.message,
|
|
1118
|
+
});
|
|
1119
|
+
status.dirty();
|
|
1120
|
+
}
|
|
1121
|
+
}
|
|
1018
1122
|
else {
|
|
1019
1123
|
util.assertNever(check);
|
|
1020
1124
|
}
|
|
@@ -1033,6 +1137,9 @@ class ZodString extends ZodType {
|
|
|
1033
1137
|
url(message) {
|
|
1034
1138
|
return this._addCheck({ kind: "url", ...errorUtil.errToObj(message) });
|
|
1035
1139
|
}
|
|
1140
|
+
emoji(message) {
|
|
1141
|
+
return this._addCheck({ kind: "emoji", ...errorUtil.errToObj(message) });
|
|
1142
|
+
}
|
|
1036
1143
|
uuid(message) {
|
|
1037
1144
|
return this._addCheck({ kind: "uuid", ...errorUtil.errToObj(message) });
|
|
1038
1145
|
}
|
|
@@ -1042,6 +1149,12 @@ class ZodString extends ZodType {
|
|
|
1042
1149
|
cuid2(message) {
|
|
1043
1150
|
return this._addCheck({ kind: "cuid2", ...errorUtil.errToObj(message) });
|
|
1044
1151
|
}
|
|
1152
|
+
ulid(message) {
|
|
1153
|
+
return this._addCheck({ kind: "ulid", ...errorUtil.errToObj(message) });
|
|
1154
|
+
}
|
|
1155
|
+
ip(options) {
|
|
1156
|
+
return this._addCheck({ kind: "ip", ...errorUtil.errToObj(options) });
|
|
1157
|
+
}
|
|
1045
1158
|
datetime(options) {
|
|
1046
1159
|
var _a;
|
|
1047
1160
|
if (typeof options === "string") {
|
|
@@ -1066,6 +1179,14 @@ class ZodString extends ZodType {
|
|
|
1066
1179
|
...errorUtil.errToObj(message),
|
|
1067
1180
|
});
|
|
1068
1181
|
}
|
|
1182
|
+
includes(value, options) {
|
|
1183
|
+
return this._addCheck({
|
|
1184
|
+
kind: "includes",
|
|
1185
|
+
value: value,
|
|
1186
|
+
position: options === null || options === void 0 ? void 0 : options.position,
|
|
1187
|
+
...errorUtil.errToObj(options === null || options === void 0 ? void 0 : options.message),
|
|
1188
|
+
});
|
|
1189
|
+
}
|
|
1069
1190
|
startsWith(value, message) {
|
|
1070
1191
|
return this._addCheck({
|
|
1071
1192
|
kind: "startsWith",
|
|
@@ -1110,6 +1231,9 @@ class ZodString extends ZodType {
|
|
|
1110
1231
|
get isURL() {
|
|
1111
1232
|
return !!this._def.checks.find((ch) => ch.kind === "url");
|
|
1112
1233
|
}
|
|
1234
|
+
get isEmoji() {
|
|
1235
|
+
return !!this._def.checks.find((ch) => ch.kind === "emoji");
|
|
1236
|
+
}
|
|
1113
1237
|
get isUUID() {
|
|
1114
1238
|
return !!this._def.checks.find((ch) => ch.kind === "uuid");
|
|
1115
1239
|
}
|
|
@@ -1119,6 +1243,12 @@ class ZodString extends ZodType {
|
|
|
1119
1243
|
get isCUID2() {
|
|
1120
1244
|
return !!this._def.checks.find((ch) => ch.kind === "cuid2");
|
|
1121
1245
|
}
|
|
1246
|
+
get isULID() {
|
|
1247
|
+
return !!this._def.checks.find((ch) => ch.kind === "ulid");
|
|
1248
|
+
}
|
|
1249
|
+
get isIP() {
|
|
1250
|
+
return !!this._def.checks.find((ch) => ch.kind === "ip");
|
|
1251
|
+
}
|
|
1122
1252
|
get minLength() {
|
|
1123
1253
|
let min = null;
|
|
1124
1254
|
for (const ch of this._def.checks) {
|
|
@@ -1338,6 +1468,19 @@ class ZodNumber extends ZodType {
|
|
|
1338
1468
|
message: errorUtil.toString(message),
|
|
1339
1469
|
});
|
|
1340
1470
|
}
|
|
1471
|
+
safe(message) {
|
|
1472
|
+
return this._addCheck({
|
|
1473
|
+
kind: "min",
|
|
1474
|
+
inclusive: true,
|
|
1475
|
+
value: Number.MIN_SAFE_INTEGER,
|
|
1476
|
+
message: errorUtil.toString(message),
|
|
1477
|
+
})._addCheck({
|
|
1478
|
+
kind: "max",
|
|
1479
|
+
inclusive: true,
|
|
1480
|
+
value: Number.MAX_SAFE_INTEGER,
|
|
1481
|
+
message: errorUtil.toString(message),
|
|
1482
|
+
});
|
|
1483
|
+
}
|
|
1341
1484
|
get minValue() {
|
|
1342
1485
|
let min = null;
|
|
1343
1486
|
for (const ch of this._def.checks) {
|
|
@@ -1391,6 +1534,11 @@ ZodNumber.create = (params) => {
|
|
|
1391
1534
|
});
|
|
1392
1535
|
};
|
|
1393
1536
|
class ZodBigInt extends ZodType {
|
|
1537
|
+
constructor() {
|
|
1538
|
+
super(...arguments);
|
|
1539
|
+
this.min = this.gte;
|
|
1540
|
+
this.max = this.lte;
|
|
1541
|
+
}
|
|
1394
1542
|
_parse(input) {
|
|
1395
1543
|
if (this._def.coerce) {
|
|
1396
1544
|
input.data = BigInt(input.data);
|
|
@@ -1405,12 +1553,154 @@ class ZodBigInt extends ZodType {
|
|
|
1405
1553
|
});
|
|
1406
1554
|
return INVALID;
|
|
1407
1555
|
}
|
|
1408
|
-
|
|
1556
|
+
let ctx = undefined;
|
|
1557
|
+
const status = new ParseStatus();
|
|
1558
|
+
for (const check of this._def.checks) {
|
|
1559
|
+
if (check.kind === "min") {
|
|
1560
|
+
const tooSmall = check.inclusive
|
|
1561
|
+
? input.data < check.value
|
|
1562
|
+
: input.data <= check.value;
|
|
1563
|
+
if (tooSmall) {
|
|
1564
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1565
|
+
addIssueToContext(ctx, {
|
|
1566
|
+
code: ZodIssueCode.too_small,
|
|
1567
|
+
type: "bigint",
|
|
1568
|
+
minimum: check.value,
|
|
1569
|
+
inclusive: check.inclusive,
|
|
1570
|
+
message: check.message,
|
|
1571
|
+
});
|
|
1572
|
+
status.dirty();
|
|
1573
|
+
}
|
|
1574
|
+
}
|
|
1575
|
+
else if (check.kind === "max") {
|
|
1576
|
+
const tooBig = check.inclusive
|
|
1577
|
+
? input.data > check.value
|
|
1578
|
+
: input.data >= check.value;
|
|
1579
|
+
if (tooBig) {
|
|
1580
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1581
|
+
addIssueToContext(ctx, {
|
|
1582
|
+
code: ZodIssueCode.too_big,
|
|
1583
|
+
type: "bigint",
|
|
1584
|
+
maximum: check.value,
|
|
1585
|
+
inclusive: check.inclusive,
|
|
1586
|
+
message: check.message,
|
|
1587
|
+
});
|
|
1588
|
+
status.dirty();
|
|
1589
|
+
}
|
|
1590
|
+
}
|
|
1591
|
+
else if (check.kind === "multipleOf") {
|
|
1592
|
+
if (input.data % check.value !== BigInt(0)) {
|
|
1593
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1594
|
+
addIssueToContext(ctx, {
|
|
1595
|
+
code: ZodIssueCode.not_multiple_of,
|
|
1596
|
+
multipleOf: check.value,
|
|
1597
|
+
message: check.message,
|
|
1598
|
+
});
|
|
1599
|
+
status.dirty();
|
|
1600
|
+
}
|
|
1601
|
+
}
|
|
1602
|
+
else {
|
|
1603
|
+
util.assertNever(check);
|
|
1604
|
+
}
|
|
1605
|
+
}
|
|
1606
|
+
return { status: status.value, value: input.data };
|
|
1607
|
+
}
|
|
1608
|
+
gte(value, message) {
|
|
1609
|
+
return this.setLimit("min", value, true, errorUtil.toString(message));
|
|
1610
|
+
}
|
|
1611
|
+
gt(value, message) {
|
|
1612
|
+
return this.setLimit("min", value, false, errorUtil.toString(message));
|
|
1613
|
+
}
|
|
1614
|
+
lte(value, message) {
|
|
1615
|
+
return this.setLimit("max", value, true, errorUtil.toString(message));
|
|
1616
|
+
}
|
|
1617
|
+
lt(value, message) {
|
|
1618
|
+
return this.setLimit("max", value, false, errorUtil.toString(message));
|
|
1619
|
+
}
|
|
1620
|
+
setLimit(kind, value, inclusive, message) {
|
|
1621
|
+
return new ZodBigInt({
|
|
1622
|
+
...this._def,
|
|
1623
|
+
checks: [
|
|
1624
|
+
...this._def.checks,
|
|
1625
|
+
{
|
|
1626
|
+
kind,
|
|
1627
|
+
value,
|
|
1628
|
+
inclusive,
|
|
1629
|
+
message: errorUtil.toString(message),
|
|
1630
|
+
},
|
|
1631
|
+
],
|
|
1632
|
+
});
|
|
1633
|
+
}
|
|
1634
|
+
_addCheck(check) {
|
|
1635
|
+
return new ZodBigInt({
|
|
1636
|
+
...this._def,
|
|
1637
|
+
checks: [...this._def.checks, check],
|
|
1638
|
+
});
|
|
1639
|
+
}
|
|
1640
|
+
positive(message) {
|
|
1641
|
+
return this._addCheck({
|
|
1642
|
+
kind: "min",
|
|
1643
|
+
value: BigInt(0),
|
|
1644
|
+
inclusive: false,
|
|
1645
|
+
message: errorUtil.toString(message),
|
|
1646
|
+
});
|
|
1647
|
+
}
|
|
1648
|
+
negative(message) {
|
|
1649
|
+
return this._addCheck({
|
|
1650
|
+
kind: "max",
|
|
1651
|
+
value: BigInt(0),
|
|
1652
|
+
inclusive: false,
|
|
1653
|
+
message: errorUtil.toString(message),
|
|
1654
|
+
});
|
|
1655
|
+
}
|
|
1656
|
+
nonpositive(message) {
|
|
1657
|
+
return this._addCheck({
|
|
1658
|
+
kind: "max",
|
|
1659
|
+
value: BigInt(0),
|
|
1660
|
+
inclusive: true,
|
|
1661
|
+
message: errorUtil.toString(message),
|
|
1662
|
+
});
|
|
1663
|
+
}
|
|
1664
|
+
nonnegative(message) {
|
|
1665
|
+
return this._addCheck({
|
|
1666
|
+
kind: "min",
|
|
1667
|
+
value: BigInt(0),
|
|
1668
|
+
inclusive: true,
|
|
1669
|
+
message: errorUtil.toString(message),
|
|
1670
|
+
});
|
|
1671
|
+
}
|
|
1672
|
+
multipleOf(value, message) {
|
|
1673
|
+
return this._addCheck({
|
|
1674
|
+
kind: "multipleOf",
|
|
1675
|
+
value,
|
|
1676
|
+
message: errorUtil.toString(message),
|
|
1677
|
+
});
|
|
1678
|
+
}
|
|
1679
|
+
get minValue() {
|
|
1680
|
+
let min = null;
|
|
1681
|
+
for (const ch of this._def.checks) {
|
|
1682
|
+
if (ch.kind === "min") {
|
|
1683
|
+
if (min === null || ch.value > min)
|
|
1684
|
+
min = ch.value;
|
|
1685
|
+
}
|
|
1686
|
+
}
|
|
1687
|
+
return min;
|
|
1688
|
+
}
|
|
1689
|
+
get maxValue() {
|
|
1690
|
+
let max = null;
|
|
1691
|
+
for (const ch of this._def.checks) {
|
|
1692
|
+
if (ch.kind === "max") {
|
|
1693
|
+
if (max === null || ch.value < max)
|
|
1694
|
+
max = ch.value;
|
|
1695
|
+
}
|
|
1696
|
+
}
|
|
1697
|
+
return max;
|
|
1409
1698
|
}
|
|
1410
1699
|
}
|
|
1411
1700
|
ZodBigInt.create = (params) => {
|
|
1412
1701
|
var _a;
|
|
1413
1702
|
return new ZodBigInt({
|
|
1703
|
+
checks: [],
|
|
1414
1704
|
typeName: ZodFirstPartyTypeKind.ZodBigInt,
|
|
1415
1705
|
coerce: (_a = params === null || params === void 0 ? void 0 : params.coerce) !== null && _a !== void 0 ? _a : false,
|
|
1416
1706
|
...processCreateParams(params),
|
|
@@ -1786,22 +2076,6 @@ ZodArray.create = (schema, params) => {
|
|
|
1786
2076
|
...processCreateParams(params),
|
|
1787
2077
|
});
|
|
1788
2078
|
};
|
|
1789
|
-
/////////////////////////////////////////
|
|
1790
|
-
/////////////////////////////////////////
|
|
1791
|
-
////////// //////////
|
|
1792
|
-
////////// ZodObject //////////
|
|
1793
|
-
////////// //////////
|
|
1794
|
-
/////////////////////////////////////////
|
|
1795
|
-
/////////////////////////////////////////
|
|
1796
|
-
var objectUtil;
|
|
1797
|
-
(function (objectUtil) {
|
|
1798
|
-
objectUtil.mergeShapes = (first, second) => {
|
|
1799
|
-
return {
|
|
1800
|
-
...first,
|
|
1801
|
-
...second, // second overwrites first
|
|
1802
|
-
};
|
|
1803
|
-
};
|
|
1804
|
-
})(objectUtil || (objectUtil = {}));
|
|
1805
2079
|
function deepPartialify(schema) {
|
|
1806
2080
|
if (schema instanceof ZodObject) {
|
|
1807
2081
|
const newShape = {};
|
|
@@ -1815,7 +2089,10 @@ function deepPartialify(schema) {
|
|
|
1815
2089
|
});
|
|
1816
2090
|
}
|
|
1817
2091
|
else if (schema instanceof ZodArray) {
|
|
1818
|
-
return ZodArray
|
|
2092
|
+
return new ZodArray({
|
|
2093
|
+
...schema._def,
|
|
2094
|
+
type: deepPartialify(schema.element),
|
|
2095
|
+
});
|
|
1819
2096
|
}
|
|
1820
2097
|
else if (schema instanceof ZodOptional) {
|
|
1821
2098
|
return ZodOptional.create(deepPartialify(schema.unwrap()));
|
|
@@ -2047,7 +2324,10 @@ class ZodObject extends ZodType {
|
|
|
2047
2324
|
const merged = new ZodObject({
|
|
2048
2325
|
unknownKeys: merging._def.unknownKeys,
|
|
2049
2326
|
catchall: merging._def.catchall,
|
|
2050
|
-
shape: () =>
|
|
2327
|
+
shape: () => ({
|
|
2328
|
+
...this._def.shape(),
|
|
2329
|
+
...merging._def.shape(),
|
|
2330
|
+
}),
|
|
2051
2331
|
typeName: ZodFirstPartyTypeKind.ZodObject,
|
|
2052
2332
|
});
|
|
2053
2333
|
return merged;
|
|
@@ -2141,6 +2421,9 @@ class ZodObject extends ZodType {
|
|
|
2141
2421
|
shape: () => shape,
|
|
2142
2422
|
});
|
|
2143
2423
|
}
|
|
2424
|
+
/**
|
|
2425
|
+
* @deprecated
|
|
2426
|
+
*/
|
|
2144
2427
|
deepPartial() {
|
|
2145
2428
|
return deepPartialify(this);
|
|
2146
2429
|
}
|
|
@@ -3180,10 +3463,6 @@ class ZodEffects extends ZodType {
|
|
|
3180
3463
|
path: ctx.path,
|
|
3181
3464
|
parent: ctx,
|
|
3182
3465
|
});
|
|
3183
|
-
// if (base.status === "aborted") return INVALID;
|
|
3184
|
-
// if (base.status === "dirty") {
|
|
3185
|
-
// return { status: "dirty", value: base.value };
|
|
3186
|
-
// }
|
|
3187
3466
|
if (!isValid(base))
|
|
3188
3467
|
return base;
|
|
3189
3468
|
const result = effect.transform(base.value, checkCtx);
|
|
@@ -3198,10 +3477,6 @@ class ZodEffects extends ZodType {
|
|
|
3198
3477
|
.then((base) => {
|
|
3199
3478
|
if (!isValid(base))
|
|
3200
3479
|
return base;
|
|
3201
|
-
// if (base.status === "aborted") return INVALID;
|
|
3202
|
-
// if (base.status === "dirty") {
|
|
3203
|
-
// return { status: "dirty", value: base.value };
|
|
3204
|
-
// }
|
|
3205
3480
|
return Promise.resolve(effect.transform(base.value, checkCtx)).then((result) => ({ status: status.value, value: result }));
|
|
3206
3481
|
});
|
|
3207
3482
|
}
|
|
@@ -3293,29 +3568,47 @@ ZodDefault.create = (type, params) => {
|
|
|
3293
3568
|
class ZodCatch extends ZodType {
|
|
3294
3569
|
_parse(input) {
|
|
3295
3570
|
const { ctx } = this._processInputParams(input);
|
|
3571
|
+
// newCtx is used to not collect issues from inner types in ctx
|
|
3572
|
+
const newCtx = {
|
|
3573
|
+
...ctx,
|
|
3574
|
+
common: {
|
|
3575
|
+
...ctx.common,
|
|
3576
|
+
issues: [],
|
|
3577
|
+
},
|
|
3578
|
+
};
|
|
3296
3579
|
const result = this._def.innerType._parse({
|
|
3297
|
-
data:
|
|
3298
|
-
path:
|
|
3580
|
+
data: newCtx.data,
|
|
3581
|
+
path: newCtx.path,
|
|
3299
3582
|
parent: {
|
|
3300
|
-
...
|
|
3301
|
-
common: {
|
|
3302
|
-
...ctx.common,
|
|
3303
|
-
issues: [], // don't collect issues from inner type
|
|
3304
|
-
},
|
|
3583
|
+
...newCtx,
|
|
3305
3584
|
},
|
|
3306
3585
|
});
|
|
3307
3586
|
if (isAsync(result)) {
|
|
3308
3587
|
return result.then((result) => {
|
|
3309
3588
|
return {
|
|
3310
3589
|
status: "valid",
|
|
3311
|
-
value: result.status === "valid"
|
|
3590
|
+
value: result.status === "valid"
|
|
3591
|
+
? result.value
|
|
3592
|
+
: this._def.catchValue({
|
|
3593
|
+
get error() {
|
|
3594
|
+
return new ZodError(newCtx.common.issues);
|
|
3595
|
+
},
|
|
3596
|
+
input: newCtx.data,
|
|
3597
|
+
}),
|
|
3312
3598
|
};
|
|
3313
3599
|
});
|
|
3314
3600
|
}
|
|
3315
3601
|
else {
|
|
3316
3602
|
return {
|
|
3317
3603
|
status: "valid",
|
|
3318
|
-
value: result.status === "valid"
|
|
3604
|
+
value: result.status === "valid"
|
|
3605
|
+
? result.value
|
|
3606
|
+
: this._def.catchValue({
|
|
3607
|
+
get error() {
|
|
3608
|
+
return new ZodError(newCtx.common.issues);
|
|
3609
|
+
},
|
|
3610
|
+
input: newCtx.data,
|
|
3611
|
+
}),
|
|
3319
3612
|
};
|
|
3320
3613
|
}
|
|
3321
3614
|
}
|
|
@@ -3425,13 +3718,30 @@ class ZodPipeline extends ZodType {
|
|
|
3425
3718
|
});
|
|
3426
3719
|
}
|
|
3427
3720
|
}
|
|
3428
|
-
const custom = (check, params = {},
|
|
3721
|
+
const custom = (check, params = {},
|
|
3722
|
+
/*
|
|
3723
|
+
* @deprecated
|
|
3724
|
+
*
|
|
3725
|
+
* Pass `fatal` into the params object instead:
|
|
3726
|
+
*
|
|
3727
|
+
* ```ts
|
|
3728
|
+
* z.string().custom((val) => val.length > 5, { fatal: false })
|
|
3729
|
+
* ```
|
|
3730
|
+
*
|
|
3731
|
+
*/
|
|
3732
|
+
fatal) => {
|
|
3429
3733
|
if (check)
|
|
3430
3734
|
return ZodAny.create().superRefine((data, ctx) => {
|
|
3735
|
+
var _a, _b;
|
|
3431
3736
|
if (!check(data)) {
|
|
3432
|
-
const p = typeof params === "function"
|
|
3737
|
+
const p = typeof params === "function"
|
|
3738
|
+
? params(data)
|
|
3739
|
+
: typeof params === "string"
|
|
3740
|
+
? { message: params }
|
|
3741
|
+
: params;
|
|
3742
|
+
const _fatal = (_b = (_a = p.fatal) !== null && _a !== void 0 ? _a : fatal) !== null && _b !== void 0 ? _b : true;
|
|
3433
3743
|
const p2 = typeof p === "string" ? { message: p } : p;
|
|
3434
|
-
ctx.addIssue({ code: "custom", ...p2, fatal });
|
|
3744
|
+
ctx.addIssue({ code: "custom", ...p2, fatal: _fatal });
|
|
3435
3745
|
}
|
|
3436
3746
|
});
|
|
3437
3747
|
return ZodAny.create();
|
|
@@ -3481,7 +3791,7 @@ const instanceOfType = (
|
|
|
3481
3791
|
// const instanceOfType = <T extends new (...args: any[]) => any>(
|
|
3482
3792
|
cls, params = {
|
|
3483
3793
|
message: `Input not instance of ${cls.name}`,
|
|
3484
|
-
}) => custom((data) => data instanceof cls, params
|
|
3794
|
+
}) => custom((data) => data instanceof cls, params);
|
|
3485
3795
|
const stringType = ZodString.create;
|
|
3486
3796
|
const numberType = ZodNumber.create;
|
|
3487
3797
|
const nanType = ZodNaN.create;
|
|
@@ -3531,7 +3841,7 @@ const coerce = {
|
|
|
3531
3841
|
};
|
|
3532
3842
|
const NEVER = INVALID;
|
|
3533
3843
|
|
|
3534
|
-
var
|
|
3844
|
+
var z = /*#__PURE__*/Object.freeze({
|
|
3535
3845
|
__proto__: null,
|
|
3536
3846
|
defaultErrorMap: errorMap,
|
|
3537
3847
|
setErrorMap: setErrorMap,
|
|
@@ -3548,6 +3858,7 @@ var mod = /*#__PURE__*/Object.freeze({
|
|
|
3548
3858
|
isValid: isValid,
|
|
3549
3859
|
isAsync: isAsync,
|
|
3550
3860
|
get util () { return util; },
|
|
3861
|
+
get objectUtil () { return objectUtil; },
|
|
3551
3862
|
ZodParsedType: ZodParsedType,
|
|
3552
3863
|
getParsedType: getParsedType,
|
|
3553
3864
|
ZodType: ZodType,
|
|
@@ -3564,7 +3875,6 @@ var mod = /*#__PURE__*/Object.freeze({
|
|
|
3564
3875
|
ZodNever: ZodNever,
|
|
3565
3876
|
ZodVoid: ZodVoid,
|
|
3566
3877
|
ZodArray: ZodArray,
|
|
3567
|
-
get objectUtil () { return objectUtil; },
|
|
3568
3878
|
ZodObject: ZodObject,
|
|
3569
3879
|
ZodUnion: ZodUnion,
|
|
3570
3880
|
ZodDiscriminatedUnion: ZodDiscriminatedUnion,
|
|
@@ -3640,4 +3950,4 @@ var mod = /*#__PURE__*/Object.freeze({
|
|
|
3640
3950
|
ZodError: ZodError
|
|
3641
3951
|
});
|
|
3642
3952
|
|
|
3643
|
-
export { BRAND, DIRTY, EMPTY_PATH, INVALID, NEVER, OK, ParseStatus, ZodType as Schema, ZodAny, ZodArray, ZodBigInt, ZodBoolean, ZodBranded, ZodCatch, ZodDate, ZodDefault, ZodDiscriminatedUnion, ZodEffects, ZodEnum, ZodError, ZodFirstPartyTypeKind, ZodFunction, ZodIntersection, ZodIssueCode, ZodLazy, ZodLiteral, ZodMap, ZodNaN, ZodNativeEnum, ZodNever, ZodNull, ZodNullable, ZodNumber, ZodObject, ZodOptional, ZodParsedType, ZodPipeline, ZodPromise, ZodRecord, ZodType as ZodSchema, ZodSet, ZodString, ZodSymbol, ZodEffects as ZodTransformer, ZodTuple, ZodType, ZodUndefined, ZodUnion, ZodUnknown, ZodVoid, addIssueToContext, anyType as any, arrayType as array, bigIntType as bigint, booleanType as boolean, coerce, custom, dateType as date,
|
|
3953
|
+
export { BRAND, DIRTY, EMPTY_PATH, INVALID, NEVER, OK, ParseStatus, ZodType as Schema, ZodAny, ZodArray, ZodBigInt, ZodBoolean, ZodBranded, ZodCatch, ZodDate, ZodDefault, ZodDiscriminatedUnion, ZodEffects, ZodEnum, ZodError, ZodFirstPartyTypeKind, ZodFunction, ZodIntersection, ZodIssueCode, ZodLazy, ZodLiteral, ZodMap, ZodNaN, ZodNativeEnum, ZodNever, ZodNull, ZodNullable, ZodNumber, ZodObject, ZodOptional, ZodParsedType, ZodPipeline, ZodPromise, ZodRecord, ZodType as ZodSchema, ZodSet, ZodString, ZodSymbol, ZodEffects as ZodTransformer, ZodTuple, ZodType, ZodUndefined, ZodUnion, ZodUnknown, ZodVoid, addIssueToContext, anyType as any, arrayType as array, bigIntType as bigint, booleanType as boolean, coerce, custom, dateType as date, z as default, errorMap as defaultErrorMap, discriminatedUnionType as discriminatedUnion, effectsType as effect, enumType as enum, functionType as function, getErrorMap, getParsedType, instanceOfType as instanceof, intersectionType as intersection, isAborted, isAsync, isDirty, isValid, late, lazyType as lazy, literalType as literal, makeIssue, mapType as map, nanType as nan, nativeEnumType as nativeEnum, neverType as never, nullType as null, nullableType as nullable, numberType as number, objectType as object, objectUtil, oboolean, onumber, optionalType as optional, ostring, pipelineType as pipeline, preprocessType as preprocess, promiseType as promise, quotelessJson, recordType as record, setType as set, setErrorMap, strictObjectType as strictObject, stringType as string, symbolType as symbol, effectsType as transformer, tupleType as tuple, undefinedType as undefined, unionType as union, unknownType as unknown, util, voidType as void, z };
|