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.umd.js
CHANGED
|
@@ -67,6 +67,15 @@
|
|
|
67
67
|
return value;
|
|
68
68
|
};
|
|
69
69
|
})(exports.util || (exports.util = {}));
|
|
70
|
+
exports.objectUtil = void 0;
|
|
71
|
+
(function (objectUtil) {
|
|
72
|
+
objectUtil.mergeShapes = (first, second) => {
|
|
73
|
+
return {
|
|
74
|
+
...first,
|
|
75
|
+
...second, // second overwrites first
|
|
76
|
+
};
|
|
77
|
+
};
|
|
78
|
+
})(exports.objectUtil || (exports.objectUtil = {}));
|
|
70
79
|
const ZodParsedType = exports.util.arrayToEnum([
|
|
71
80
|
"string",
|
|
72
81
|
"nan",
|
|
@@ -298,7 +307,13 @@
|
|
|
298
307
|
break;
|
|
299
308
|
case ZodIssueCode.invalid_string:
|
|
300
309
|
if (typeof issue.validation === "object") {
|
|
301
|
-
if ("
|
|
310
|
+
if ("includes" in issue.validation) {
|
|
311
|
+
message = `Invalid input: must include "${issue.validation.includes}"`;
|
|
312
|
+
if (typeof issue.validation.position === "number") {
|
|
313
|
+
message = `${message} at one or more positions greater than or equal to ${issue.validation.position}`;
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
else if ("startsWith" in issue.validation) {
|
|
302
317
|
message = `Invalid input: must start with "${issue.validation.startsWith}"`;
|
|
303
318
|
}
|
|
304
319
|
else if ("endsWith" in issue.validation) {
|
|
@@ -331,7 +346,7 @@
|
|
|
331
346
|
? `exactly equal to `
|
|
332
347
|
: issue.inclusive
|
|
333
348
|
? `greater than or equal to `
|
|
334
|
-
: `greater than `}${new Date(issue.minimum)}`;
|
|
349
|
+
: `greater than `}${new Date(Number(issue.minimum))}`;
|
|
335
350
|
else
|
|
336
351
|
message = "Invalid input";
|
|
337
352
|
break;
|
|
@@ -346,12 +361,18 @@
|
|
|
346
361
|
: issue.inclusive
|
|
347
362
|
? `less than or equal to`
|
|
348
363
|
: `less than`} ${issue.maximum}`;
|
|
364
|
+
else if (issue.type === "bigint")
|
|
365
|
+
message = `BigInt must be ${issue.exact
|
|
366
|
+
? `exactly`
|
|
367
|
+
: issue.inclusive
|
|
368
|
+
? `less than or equal to`
|
|
369
|
+
: `less than`} ${issue.maximum}`;
|
|
349
370
|
else if (issue.type === "date")
|
|
350
371
|
message = `Date must be ${issue.exact
|
|
351
372
|
? `exactly`
|
|
352
373
|
: issue.inclusive
|
|
353
374
|
? `smaller than or equal to`
|
|
354
|
-
: `smaller than`} ${new Date(issue.maximum)}`;
|
|
375
|
+
: `smaller than`} ${new Date(Number(issue.maximum))}`;
|
|
355
376
|
else
|
|
356
377
|
message = "Invalid input";
|
|
357
378
|
break;
|
|
@@ -488,13 +509,22 @@
|
|
|
488
509
|
|
|
489
510
|
class ParseInputLazyPath {
|
|
490
511
|
constructor(parent, value, path, key) {
|
|
512
|
+
this._cachedPath = [];
|
|
491
513
|
this.parent = parent;
|
|
492
514
|
this.data = value;
|
|
493
515
|
this._path = path;
|
|
494
516
|
this._key = key;
|
|
495
517
|
}
|
|
496
518
|
get path() {
|
|
497
|
-
|
|
519
|
+
if (!this._cachedPath.length) {
|
|
520
|
+
if (this._key instanceof Array) {
|
|
521
|
+
this._cachedPath.push(...this._path, ...this._key);
|
|
522
|
+
}
|
|
523
|
+
else {
|
|
524
|
+
this._cachedPath.push(...this._path, this._key);
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
return this._cachedPath;
|
|
498
528
|
}
|
|
499
529
|
}
|
|
500
530
|
const handleResult = (ctx, result) => {
|
|
@@ -505,8 +535,16 @@
|
|
|
505
535
|
if (!ctx.common.issues.length) {
|
|
506
536
|
throw new Error("Validation failed but no issues detected.");
|
|
507
537
|
}
|
|
508
|
-
|
|
509
|
-
|
|
538
|
+
return {
|
|
539
|
+
success: false,
|
|
540
|
+
get error() {
|
|
541
|
+
if (this._error)
|
|
542
|
+
return this._error;
|
|
543
|
+
const error = new ZodError(ctx.common.issues);
|
|
544
|
+
this._error = error;
|
|
545
|
+
return this._error;
|
|
546
|
+
},
|
|
547
|
+
};
|
|
510
548
|
}
|
|
511
549
|
};
|
|
512
550
|
function processCreateParams(params) {
|
|
@@ -779,6 +817,7 @@
|
|
|
779
817
|
}
|
|
780
818
|
const cuidRegex = /^c[^\s-]{8,}$/i;
|
|
781
819
|
const cuid2Regex = /^[a-z][a-z0-9]*$/;
|
|
820
|
+
const ulidRegex = /[0-9A-HJKMNP-TV-Z]{26}/;
|
|
782
821
|
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;
|
|
783
822
|
// from https://stackoverflow.com/a/46181/1550155
|
|
784
823
|
// old version: too slow, didn't support unicode
|
|
@@ -786,13 +825,11 @@
|
|
|
786
825
|
//old email regex
|
|
787
826
|
// const emailRegex = /^(([^<>()[\].,;:\s@"]+(\.[^<>()[\].,;:\s@"]+)*)|(".+"))@((?!-)([^<>()[\].,;:\s@"]+\.)+[^<>()[\].,;:\s@"]{1,})[^-<>()[\].,;:\s@"]$/i;
|
|
788
827
|
// eslint-disable-next-line
|
|
789
|
-
const emailRegex = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}
|
|
790
|
-
//
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
// any?: boolean;
|
|
795
|
-
// }
|
|
828
|
+
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,})+))$/;
|
|
829
|
+
// from https://thekevinscott.com/emojis-in-javascript/#writing-a-regular-expression
|
|
830
|
+
const emojiRegex = /^(\p{Extended_Pictographic}|\p{Emoji_Component})+$/u;
|
|
831
|
+
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}))$/;
|
|
832
|
+
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})))$/;
|
|
796
833
|
// Adapted from https://stackoverflow.com/a/3143231
|
|
797
834
|
const datetimeRegex = (args) => {
|
|
798
835
|
if (args.precision) {
|
|
@@ -820,6 +857,15 @@
|
|
|
820
857
|
}
|
|
821
858
|
}
|
|
822
859
|
};
|
|
860
|
+
function isValidIP(ip, version) {
|
|
861
|
+
if ((version === "v4" || !version) && ipv4Regex.test(ip)) {
|
|
862
|
+
return true;
|
|
863
|
+
}
|
|
864
|
+
if ((version === "v6" || !version) && ipv6Regex.test(ip)) {
|
|
865
|
+
return true;
|
|
866
|
+
}
|
|
867
|
+
return false;
|
|
868
|
+
}
|
|
823
869
|
class ZodString extends ZodType {
|
|
824
870
|
constructor() {
|
|
825
871
|
super(...arguments);
|
|
@@ -837,6 +883,14 @@
|
|
|
837
883
|
...this._def,
|
|
838
884
|
checks: [...this._def.checks, { kind: "trim" }],
|
|
839
885
|
});
|
|
886
|
+
this.toLowerCase = () => new ZodString({
|
|
887
|
+
...this._def,
|
|
888
|
+
checks: [...this._def.checks, { kind: "toLowerCase" }],
|
|
889
|
+
});
|
|
890
|
+
this.toUpperCase = () => new ZodString({
|
|
891
|
+
...this._def,
|
|
892
|
+
checks: [...this._def.checks, { kind: "toUpperCase" }],
|
|
893
|
+
});
|
|
840
894
|
}
|
|
841
895
|
_parse(input) {
|
|
842
896
|
if (this._def.coerce) {
|
|
@@ -924,6 +978,17 @@
|
|
|
924
978
|
status.dirty();
|
|
925
979
|
}
|
|
926
980
|
}
|
|
981
|
+
else if (check.kind === "emoji") {
|
|
982
|
+
if (!emojiRegex.test(input.data)) {
|
|
983
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
984
|
+
addIssueToContext(ctx, {
|
|
985
|
+
validation: "emoji",
|
|
986
|
+
code: ZodIssueCode.invalid_string,
|
|
987
|
+
message: check.message,
|
|
988
|
+
});
|
|
989
|
+
status.dirty();
|
|
990
|
+
}
|
|
991
|
+
}
|
|
927
992
|
else if (check.kind === "uuid") {
|
|
928
993
|
if (!uuidRegex.test(input.data)) {
|
|
929
994
|
ctx = this._getOrReturnCtx(input, ctx);
|
|
@@ -957,6 +1022,17 @@
|
|
|
957
1022
|
status.dirty();
|
|
958
1023
|
}
|
|
959
1024
|
}
|
|
1025
|
+
else if (check.kind === "ulid") {
|
|
1026
|
+
if (!ulidRegex.test(input.data)) {
|
|
1027
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1028
|
+
addIssueToContext(ctx, {
|
|
1029
|
+
validation: "ulid",
|
|
1030
|
+
code: ZodIssueCode.invalid_string,
|
|
1031
|
+
message: check.message,
|
|
1032
|
+
});
|
|
1033
|
+
status.dirty();
|
|
1034
|
+
}
|
|
1035
|
+
}
|
|
960
1036
|
else if (check.kind === "url") {
|
|
961
1037
|
try {
|
|
962
1038
|
new URL(input.data);
|
|
@@ -987,6 +1063,23 @@
|
|
|
987
1063
|
else if (check.kind === "trim") {
|
|
988
1064
|
input.data = input.data.trim();
|
|
989
1065
|
}
|
|
1066
|
+
else if (check.kind === "includes") {
|
|
1067
|
+
if (!input.data.includes(check.value, check.position)) {
|
|
1068
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1069
|
+
addIssueToContext(ctx, {
|
|
1070
|
+
code: ZodIssueCode.invalid_string,
|
|
1071
|
+
validation: { includes: check.value, position: check.position },
|
|
1072
|
+
message: check.message,
|
|
1073
|
+
});
|
|
1074
|
+
status.dirty();
|
|
1075
|
+
}
|
|
1076
|
+
}
|
|
1077
|
+
else if (check.kind === "toLowerCase") {
|
|
1078
|
+
input.data = input.data.toLowerCase();
|
|
1079
|
+
}
|
|
1080
|
+
else if (check.kind === "toUpperCase") {
|
|
1081
|
+
input.data = input.data.toUpperCase();
|
|
1082
|
+
}
|
|
990
1083
|
else if (check.kind === "startsWith") {
|
|
991
1084
|
if (!input.data.startsWith(check.value)) {
|
|
992
1085
|
ctx = this._getOrReturnCtx(input, ctx);
|
|
@@ -1021,6 +1114,17 @@
|
|
|
1021
1114
|
status.dirty();
|
|
1022
1115
|
}
|
|
1023
1116
|
}
|
|
1117
|
+
else if (check.kind === "ip") {
|
|
1118
|
+
if (!isValidIP(input.data, check.version)) {
|
|
1119
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1120
|
+
addIssueToContext(ctx, {
|
|
1121
|
+
validation: "ip",
|
|
1122
|
+
code: ZodIssueCode.invalid_string,
|
|
1123
|
+
message: check.message,
|
|
1124
|
+
});
|
|
1125
|
+
status.dirty();
|
|
1126
|
+
}
|
|
1127
|
+
}
|
|
1024
1128
|
else {
|
|
1025
1129
|
exports.util.assertNever(check);
|
|
1026
1130
|
}
|
|
@@ -1039,6 +1143,9 @@
|
|
|
1039
1143
|
url(message) {
|
|
1040
1144
|
return this._addCheck({ kind: "url", ...errorUtil.errToObj(message) });
|
|
1041
1145
|
}
|
|
1146
|
+
emoji(message) {
|
|
1147
|
+
return this._addCheck({ kind: "emoji", ...errorUtil.errToObj(message) });
|
|
1148
|
+
}
|
|
1042
1149
|
uuid(message) {
|
|
1043
1150
|
return this._addCheck({ kind: "uuid", ...errorUtil.errToObj(message) });
|
|
1044
1151
|
}
|
|
@@ -1048,6 +1155,12 @@
|
|
|
1048
1155
|
cuid2(message) {
|
|
1049
1156
|
return this._addCheck({ kind: "cuid2", ...errorUtil.errToObj(message) });
|
|
1050
1157
|
}
|
|
1158
|
+
ulid(message) {
|
|
1159
|
+
return this._addCheck({ kind: "ulid", ...errorUtil.errToObj(message) });
|
|
1160
|
+
}
|
|
1161
|
+
ip(options) {
|
|
1162
|
+
return this._addCheck({ kind: "ip", ...errorUtil.errToObj(options) });
|
|
1163
|
+
}
|
|
1051
1164
|
datetime(options) {
|
|
1052
1165
|
var _a;
|
|
1053
1166
|
if (typeof options === "string") {
|
|
@@ -1072,6 +1185,14 @@
|
|
|
1072
1185
|
...errorUtil.errToObj(message),
|
|
1073
1186
|
});
|
|
1074
1187
|
}
|
|
1188
|
+
includes(value, options) {
|
|
1189
|
+
return this._addCheck({
|
|
1190
|
+
kind: "includes",
|
|
1191
|
+
value: value,
|
|
1192
|
+
position: options === null || options === void 0 ? void 0 : options.position,
|
|
1193
|
+
...errorUtil.errToObj(options === null || options === void 0 ? void 0 : options.message),
|
|
1194
|
+
});
|
|
1195
|
+
}
|
|
1075
1196
|
startsWith(value, message) {
|
|
1076
1197
|
return this._addCheck({
|
|
1077
1198
|
kind: "startsWith",
|
|
@@ -1116,6 +1237,9 @@
|
|
|
1116
1237
|
get isURL() {
|
|
1117
1238
|
return !!this._def.checks.find((ch) => ch.kind === "url");
|
|
1118
1239
|
}
|
|
1240
|
+
get isEmoji() {
|
|
1241
|
+
return !!this._def.checks.find((ch) => ch.kind === "emoji");
|
|
1242
|
+
}
|
|
1119
1243
|
get isUUID() {
|
|
1120
1244
|
return !!this._def.checks.find((ch) => ch.kind === "uuid");
|
|
1121
1245
|
}
|
|
@@ -1125,6 +1249,12 @@
|
|
|
1125
1249
|
get isCUID2() {
|
|
1126
1250
|
return !!this._def.checks.find((ch) => ch.kind === "cuid2");
|
|
1127
1251
|
}
|
|
1252
|
+
get isULID() {
|
|
1253
|
+
return !!this._def.checks.find((ch) => ch.kind === "ulid");
|
|
1254
|
+
}
|
|
1255
|
+
get isIP() {
|
|
1256
|
+
return !!this._def.checks.find((ch) => ch.kind === "ip");
|
|
1257
|
+
}
|
|
1128
1258
|
get minLength() {
|
|
1129
1259
|
let min = null;
|
|
1130
1260
|
for (const ch of this._def.checks) {
|
|
@@ -1344,6 +1474,19 @@
|
|
|
1344
1474
|
message: errorUtil.toString(message),
|
|
1345
1475
|
});
|
|
1346
1476
|
}
|
|
1477
|
+
safe(message) {
|
|
1478
|
+
return this._addCheck({
|
|
1479
|
+
kind: "min",
|
|
1480
|
+
inclusive: true,
|
|
1481
|
+
value: Number.MIN_SAFE_INTEGER,
|
|
1482
|
+
message: errorUtil.toString(message),
|
|
1483
|
+
})._addCheck({
|
|
1484
|
+
kind: "max",
|
|
1485
|
+
inclusive: true,
|
|
1486
|
+
value: Number.MAX_SAFE_INTEGER,
|
|
1487
|
+
message: errorUtil.toString(message),
|
|
1488
|
+
});
|
|
1489
|
+
}
|
|
1347
1490
|
get minValue() {
|
|
1348
1491
|
let min = null;
|
|
1349
1492
|
for (const ch of this._def.checks) {
|
|
@@ -1397,6 +1540,11 @@
|
|
|
1397
1540
|
});
|
|
1398
1541
|
};
|
|
1399
1542
|
class ZodBigInt extends ZodType {
|
|
1543
|
+
constructor() {
|
|
1544
|
+
super(...arguments);
|
|
1545
|
+
this.min = this.gte;
|
|
1546
|
+
this.max = this.lte;
|
|
1547
|
+
}
|
|
1400
1548
|
_parse(input) {
|
|
1401
1549
|
if (this._def.coerce) {
|
|
1402
1550
|
input.data = BigInt(input.data);
|
|
@@ -1411,12 +1559,154 @@
|
|
|
1411
1559
|
});
|
|
1412
1560
|
return INVALID;
|
|
1413
1561
|
}
|
|
1414
|
-
|
|
1562
|
+
let ctx = undefined;
|
|
1563
|
+
const status = new ParseStatus();
|
|
1564
|
+
for (const check of this._def.checks) {
|
|
1565
|
+
if (check.kind === "min") {
|
|
1566
|
+
const tooSmall = check.inclusive
|
|
1567
|
+
? input.data < check.value
|
|
1568
|
+
: input.data <= check.value;
|
|
1569
|
+
if (tooSmall) {
|
|
1570
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1571
|
+
addIssueToContext(ctx, {
|
|
1572
|
+
code: ZodIssueCode.too_small,
|
|
1573
|
+
type: "bigint",
|
|
1574
|
+
minimum: check.value,
|
|
1575
|
+
inclusive: check.inclusive,
|
|
1576
|
+
message: check.message,
|
|
1577
|
+
});
|
|
1578
|
+
status.dirty();
|
|
1579
|
+
}
|
|
1580
|
+
}
|
|
1581
|
+
else if (check.kind === "max") {
|
|
1582
|
+
const tooBig = check.inclusive
|
|
1583
|
+
? input.data > check.value
|
|
1584
|
+
: input.data >= check.value;
|
|
1585
|
+
if (tooBig) {
|
|
1586
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1587
|
+
addIssueToContext(ctx, {
|
|
1588
|
+
code: ZodIssueCode.too_big,
|
|
1589
|
+
type: "bigint",
|
|
1590
|
+
maximum: check.value,
|
|
1591
|
+
inclusive: check.inclusive,
|
|
1592
|
+
message: check.message,
|
|
1593
|
+
});
|
|
1594
|
+
status.dirty();
|
|
1595
|
+
}
|
|
1596
|
+
}
|
|
1597
|
+
else if (check.kind === "multipleOf") {
|
|
1598
|
+
if (input.data % check.value !== BigInt(0)) {
|
|
1599
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1600
|
+
addIssueToContext(ctx, {
|
|
1601
|
+
code: ZodIssueCode.not_multiple_of,
|
|
1602
|
+
multipleOf: check.value,
|
|
1603
|
+
message: check.message,
|
|
1604
|
+
});
|
|
1605
|
+
status.dirty();
|
|
1606
|
+
}
|
|
1607
|
+
}
|
|
1608
|
+
else {
|
|
1609
|
+
exports.util.assertNever(check);
|
|
1610
|
+
}
|
|
1611
|
+
}
|
|
1612
|
+
return { status: status.value, value: input.data };
|
|
1613
|
+
}
|
|
1614
|
+
gte(value, message) {
|
|
1615
|
+
return this.setLimit("min", value, true, errorUtil.toString(message));
|
|
1616
|
+
}
|
|
1617
|
+
gt(value, message) {
|
|
1618
|
+
return this.setLimit("min", value, false, errorUtil.toString(message));
|
|
1619
|
+
}
|
|
1620
|
+
lte(value, message) {
|
|
1621
|
+
return this.setLimit("max", value, true, errorUtil.toString(message));
|
|
1622
|
+
}
|
|
1623
|
+
lt(value, message) {
|
|
1624
|
+
return this.setLimit("max", value, false, errorUtil.toString(message));
|
|
1625
|
+
}
|
|
1626
|
+
setLimit(kind, value, inclusive, message) {
|
|
1627
|
+
return new ZodBigInt({
|
|
1628
|
+
...this._def,
|
|
1629
|
+
checks: [
|
|
1630
|
+
...this._def.checks,
|
|
1631
|
+
{
|
|
1632
|
+
kind,
|
|
1633
|
+
value,
|
|
1634
|
+
inclusive,
|
|
1635
|
+
message: errorUtil.toString(message),
|
|
1636
|
+
},
|
|
1637
|
+
],
|
|
1638
|
+
});
|
|
1639
|
+
}
|
|
1640
|
+
_addCheck(check) {
|
|
1641
|
+
return new ZodBigInt({
|
|
1642
|
+
...this._def,
|
|
1643
|
+
checks: [...this._def.checks, check],
|
|
1644
|
+
});
|
|
1645
|
+
}
|
|
1646
|
+
positive(message) {
|
|
1647
|
+
return this._addCheck({
|
|
1648
|
+
kind: "min",
|
|
1649
|
+
value: BigInt(0),
|
|
1650
|
+
inclusive: false,
|
|
1651
|
+
message: errorUtil.toString(message),
|
|
1652
|
+
});
|
|
1653
|
+
}
|
|
1654
|
+
negative(message) {
|
|
1655
|
+
return this._addCheck({
|
|
1656
|
+
kind: "max",
|
|
1657
|
+
value: BigInt(0),
|
|
1658
|
+
inclusive: false,
|
|
1659
|
+
message: errorUtil.toString(message),
|
|
1660
|
+
});
|
|
1661
|
+
}
|
|
1662
|
+
nonpositive(message) {
|
|
1663
|
+
return this._addCheck({
|
|
1664
|
+
kind: "max",
|
|
1665
|
+
value: BigInt(0),
|
|
1666
|
+
inclusive: true,
|
|
1667
|
+
message: errorUtil.toString(message),
|
|
1668
|
+
});
|
|
1669
|
+
}
|
|
1670
|
+
nonnegative(message) {
|
|
1671
|
+
return this._addCheck({
|
|
1672
|
+
kind: "min",
|
|
1673
|
+
value: BigInt(0),
|
|
1674
|
+
inclusive: true,
|
|
1675
|
+
message: errorUtil.toString(message),
|
|
1676
|
+
});
|
|
1677
|
+
}
|
|
1678
|
+
multipleOf(value, message) {
|
|
1679
|
+
return this._addCheck({
|
|
1680
|
+
kind: "multipleOf",
|
|
1681
|
+
value,
|
|
1682
|
+
message: errorUtil.toString(message),
|
|
1683
|
+
});
|
|
1684
|
+
}
|
|
1685
|
+
get minValue() {
|
|
1686
|
+
let min = null;
|
|
1687
|
+
for (const ch of this._def.checks) {
|
|
1688
|
+
if (ch.kind === "min") {
|
|
1689
|
+
if (min === null || ch.value > min)
|
|
1690
|
+
min = ch.value;
|
|
1691
|
+
}
|
|
1692
|
+
}
|
|
1693
|
+
return min;
|
|
1694
|
+
}
|
|
1695
|
+
get maxValue() {
|
|
1696
|
+
let max = null;
|
|
1697
|
+
for (const ch of this._def.checks) {
|
|
1698
|
+
if (ch.kind === "max") {
|
|
1699
|
+
if (max === null || ch.value < max)
|
|
1700
|
+
max = ch.value;
|
|
1701
|
+
}
|
|
1702
|
+
}
|
|
1703
|
+
return max;
|
|
1415
1704
|
}
|
|
1416
1705
|
}
|
|
1417
1706
|
ZodBigInt.create = (params) => {
|
|
1418
1707
|
var _a;
|
|
1419
1708
|
return new ZodBigInt({
|
|
1709
|
+
checks: [],
|
|
1420
1710
|
typeName: exports.ZodFirstPartyTypeKind.ZodBigInt,
|
|
1421
1711
|
coerce: (_a = params === null || params === void 0 ? void 0 : params.coerce) !== null && _a !== void 0 ? _a : false,
|
|
1422
1712
|
...processCreateParams(params),
|
|
@@ -1792,22 +2082,6 @@
|
|
|
1792
2082
|
...processCreateParams(params),
|
|
1793
2083
|
});
|
|
1794
2084
|
};
|
|
1795
|
-
/////////////////////////////////////////
|
|
1796
|
-
/////////////////////////////////////////
|
|
1797
|
-
////////// //////////
|
|
1798
|
-
////////// ZodObject //////////
|
|
1799
|
-
////////// //////////
|
|
1800
|
-
/////////////////////////////////////////
|
|
1801
|
-
/////////////////////////////////////////
|
|
1802
|
-
exports.objectUtil = void 0;
|
|
1803
|
-
(function (objectUtil) {
|
|
1804
|
-
objectUtil.mergeShapes = (first, second) => {
|
|
1805
|
-
return {
|
|
1806
|
-
...first,
|
|
1807
|
-
...second, // second overwrites first
|
|
1808
|
-
};
|
|
1809
|
-
};
|
|
1810
|
-
})(exports.objectUtil || (exports.objectUtil = {}));
|
|
1811
2085
|
function deepPartialify(schema) {
|
|
1812
2086
|
if (schema instanceof ZodObject) {
|
|
1813
2087
|
const newShape = {};
|
|
@@ -1821,7 +2095,10 @@
|
|
|
1821
2095
|
});
|
|
1822
2096
|
}
|
|
1823
2097
|
else if (schema instanceof ZodArray) {
|
|
1824
|
-
return ZodArray
|
|
2098
|
+
return new ZodArray({
|
|
2099
|
+
...schema._def,
|
|
2100
|
+
type: deepPartialify(schema.element),
|
|
2101
|
+
});
|
|
1825
2102
|
}
|
|
1826
2103
|
else if (schema instanceof ZodOptional) {
|
|
1827
2104
|
return ZodOptional.create(deepPartialify(schema.unwrap()));
|
|
@@ -2053,7 +2330,10 @@
|
|
|
2053
2330
|
const merged = new ZodObject({
|
|
2054
2331
|
unknownKeys: merging._def.unknownKeys,
|
|
2055
2332
|
catchall: merging._def.catchall,
|
|
2056
|
-
shape: () =>
|
|
2333
|
+
shape: () => ({
|
|
2334
|
+
...this._def.shape(),
|
|
2335
|
+
...merging._def.shape(),
|
|
2336
|
+
}),
|
|
2057
2337
|
typeName: exports.ZodFirstPartyTypeKind.ZodObject,
|
|
2058
2338
|
});
|
|
2059
2339
|
return merged;
|
|
@@ -2147,6 +2427,9 @@
|
|
|
2147
2427
|
shape: () => shape,
|
|
2148
2428
|
});
|
|
2149
2429
|
}
|
|
2430
|
+
/**
|
|
2431
|
+
* @deprecated
|
|
2432
|
+
*/
|
|
2150
2433
|
deepPartial() {
|
|
2151
2434
|
return deepPartialify(this);
|
|
2152
2435
|
}
|
|
@@ -3186,10 +3469,6 @@
|
|
|
3186
3469
|
path: ctx.path,
|
|
3187
3470
|
parent: ctx,
|
|
3188
3471
|
});
|
|
3189
|
-
// if (base.status === "aborted") return INVALID;
|
|
3190
|
-
// if (base.status === "dirty") {
|
|
3191
|
-
// return { status: "dirty", value: base.value };
|
|
3192
|
-
// }
|
|
3193
3472
|
if (!isValid(base))
|
|
3194
3473
|
return base;
|
|
3195
3474
|
const result = effect.transform(base.value, checkCtx);
|
|
@@ -3204,10 +3483,6 @@
|
|
|
3204
3483
|
.then((base) => {
|
|
3205
3484
|
if (!isValid(base))
|
|
3206
3485
|
return base;
|
|
3207
|
-
// if (base.status === "aborted") return INVALID;
|
|
3208
|
-
// if (base.status === "dirty") {
|
|
3209
|
-
// return { status: "dirty", value: base.value };
|
|
3210
|
-
// }
|
|
3211
3486
|
return Promise.resolve(effect.transform(base.value, checkCtx)).then((result) => ({ status: status.value, value: result }));
|
|
3212
3487
|
});
|
|
3213
3488
|
}
|
|
@@ -3299,29 +3574,47 @@
|
|
|
3299
3574
|
class ZodCatch extends ZodType {
|
|
3300
3575
|
_parse(input) {
|
|
3301
3576
|
const { ctx } = this._processInputParams(input);
|
|
3577
|
+
// newCtx is used to not collect issues from inner types in ctx
|
|
3578
|
+
const newCtx = {
|
|
3579
|
+
...ctx,
|
|
3580
|
+
common: {
|
|
3581
|
+
...ctx.common,
|
|
3582
|
+
issues: [],
|
|
3583
|
+
},
|
|
3584
|
+
};
|
|
3302
3585
|
const result = this._def.innerType._parse({
|
|
3303
|
-
data:
|
|
3304
|
-
path:
|
|
3586
|
+
data: newCtx.data,
|
|
3587
|
+
path: newCtx.path,
|
|
3305
3588
|
parent: {
|
|
3306
|
-
...
|
|
3307
|
-
common: {
|
|
3308
|
-
...ctx.common,
|
|
3309
|
-
issues: [], // don't collect issues from inner type
|
|
3310
|
-
},
|
|
3589
|
+
...newCtx,
|
|
3311
3590
|
},
|
|
3312
3591
|
});
|
|
3313
3592
|
if (isAsync(result)) {
|
|
3314
3593
|
return result.then((result) => {
|
|
3315
3594
|
return {
|
|
3316
3595
|
status: "valid",
|
|
3317
|
-
value: result.status === "valid"
|
|
3596
|
+
value: result.status === "valid"
|
|
3597
|
+
? result.value
|
|
3598
|
+
: this._def.catchValue({
|
|
3599
|
+
get error() {
|
|
3600
|
+
return new ZodError(newCtx.common.issues);
|
|
3601
|
+
},
|
|
3602
|
+
input: newCtx.data,
|
|
3603
|
+
}),
|
|
3318
3604
|
};
|
|
3319
3605
|
});
|
|
3320
3606
|
}
|
|
3321
3607
|
else {
|
|
3322
3608
|
return {
|
|
3323
3609
|
status: "valid",
|
|
3324
|
-
value: result.status === "valid"
|
|
3610
|
+
value: result.status === "valid"
|
|
3611
|
+
? result.value
|
|
3612
|
+
: this._def.catchValue({
|
|
3613
|
+
get error() {
|
|
3614
|
+
return new ZodError(newCtx.common.issues);
|
|
3615
|
+
},
|
|
3616
|
+
input: newCtx.data,
|
|
3617
|
+
}),
|
|
3325
3618
|
};
|
|
3326
3619
|
}
|
|
3327
3620
|
}
|
|
@@ -3431,13 +3724,30 @@
|
|
|
3431
3724
|
});
|
|
3432
3725
|
}
|
|
3433
3726
|
}
|
|
3434
|
-
const custom = (check, params = {},
|
|
3727
|
+
const custom = (check, params = {},
|
|
3728
|
+
/*
|
|
3729
|
+
* @deprecated
|
|
3730
|
+
*
|
|
3731
|
+
* Pass `fatal` into the params object instead:
|
|
3732
|
+
*
|
|
3733
|
+
* ```ts
|
|
3734
|
+
* z.string().custom((val) => val.length > 5, { fatal: false })
|
|
3735
|
+
* ```
|
|
3736
|
+
*
|
|
3737
|
+
*/
|
|
3738
|
+
fatal) => {
|
|
3435
3739
|
if (check)
|
|
3436
3740
|
return ZodAny.create().superRefine((data, ctx) => {
|
|
3741
|
+
var _a, _b;
|
|
3437
3742
|
if (!check(data)) {
|
|
3438
|
-
const p = typeof params === "function"
|
|
3743
|
+
const p = typeof params === "function"
|
|
3744
|
+
? params(data)
|
|
3745
|
+
: typeof params === "string"
|
|
3746
|
+
? { message: params }
|
|
3747
|
+
: params;
|
|
3748
|
+
const _fatal = (_b = (_a = p.fatal) !== null && _a !== void 0 ? _a : fatal) !== null && _b !== void 0 ? _b : true;
|
|
3439
3749
|
const p2 = typeof p === "string" ? { message: p } : p;
|
|
3440
|
-
ctx.addIssue({ code: "custom", ...p2, fatal });
|
|
3750
|
+
ctx.addIssue({ code: "custom", ...p2, fatal: _fatal });
|
|
3441
3751
|
}
|
|
3442
3752
|
});
|
|
3443
3753
|
return ZodAny.create();
|
|
@@ -3487,7 +3797,7 @@
|
|
|
3487
3797
|
// const instanceOfType = <T extends new (...args: any[]) => any>(
|
|
3488
3798
|
cls, params = {
|
|
3489
3799
|
message: `Input not instance of ${cls.name}`,
|
|
3490
|
-
}) => custom((data) => data instanceof cls, params
|
|
3800
|
+
}) => custom((data) => data instanceof cls, params);
|
|
3491
3801
|
const stringType = ZodString.create;
|
|
3492
3802
|
const numberType = ZodNumber.create;
|
|
3493
3803
|
const nanType = ZodNaN.create;
|
|
@@ -3537,7 +3847,7 @@
|
|
|
3537
3847
|
};
|
|
3538
3848
|
const NEVER = INVALID;
|
|
3539
3849
|
|
|
3540
|
-
var
|
|
3850
|
+
var z = /*#__PURE__*/Object.freeze({
|
|
3541
3851
|
__proto__: null,
|
|
3542
3852
|
defaultErrorMap: errorMap,
|
|
3543
3853
|
setErrorMap: setErrorMap,
|
|
@@ -3554,6 +3864,7 @@
|
|
|
3554
3864
|
isValid: isValid,
|
|
3555
3865
|
isAsync: isAsync,
|
|
3556
3866
|
get util () { return exports.util; },
|
|
3867
|
+
get objectUtil () { return exports.objectUtil; },
|
|
3557
3868
|
ZodParsedType: ZodParsedType,
|
|
3558
3869
|
getParsedType: getParsedType,
|
|
3559
3870
|
ZodType: ZodType,
|
|
@@ -3570,7 +3881,6 @@
|
|
|
3570
3881
|
ZodNever: ZodNever,
|
|
3571
3882
|
ZodVoid: ZodVoid,
|
|
3572
3883
|
ZodArray: ZodArray,
|
|
3573
|
-
get objectUtil () { return exports.objectUtil; },
|
|
3574
3884
|
ZodObject: ZodObject,
|
|
3575
3885
|
ZodUnion: ZodUnion,
|
|
3576
3886
|
ZodDiscriminatedUnion: ZodDiscriminatedUnion,
|
|
@@ -3703,7 +4013,7 @@
|
|
|
3703
4013
|
exports.coerce = coerce;
|
|
3704
4014
|
exports.custom = custom;
|
|
3705
4015
|
exports.date = dateType;
|
|
3706
|
-
exports["default"] =
|
|
4016
|
+
exports["default"] = z;
|
|
3707
4017
|
exports.defaultErrorMap = errorMap;
|
|
3708
4018
|
exports.discriminatedUnion = discriminatedUnionType;
|
|
3709
4019
|
exports.effect = effectsType;
|
|
@@ -3749,7 +4059,7 @@
|
|
|
3749
4059
|
exports.union = unionType;
|
|
3750
4060
|
exports.unknown = unknownType;
|
|
3751
4061
|
exports["void"] = voidType;
|
|
3752
|
-
exports.z =
|
|
4062
|
+
exports.z = z;
|
|
3753
4063
|
|
|
3754
4064
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
3755
4065
|
|