zod 3.22.3 → 3.22.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 +2 -0
- package/lib/ZodError.js +8 -0
- package/lib/helpers/parseUtil.js +1 -1
- package/lib/helpers/util.js +4 -4
- package/lib/index.mjs +39 -28
- package/lib/index.umd.js +39 -28
- package/lib/types.d.ts +33 -5
- package/lib/types.js +236 -29
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -489,6 +489,7 @@ There are a growing number of tools that are built atop or support Zod natively!
|
|
|
489
489
|
- [`prisma-trpc-generator`](https://github.com/omar-dulaimi/prisma-trpc-generator): Emit fully implemented tRPC routers and their validation schemas using Zod.
|
|
490
490
|
- [`zod-prisma-types`](https://github.com/chrishoermann/zod-prisma-types) Create Zod types from your Prisma models.
|
|
491
491
|
- [`quicktype`](https://app.quicktype.io/): Convert JSON objects and JSON schemas into Zod schemas.
|
|
492
|
+
- [`@sanity-typed/zod`](https://github.com/saiichihashimoto/sanity-typed/tree/main/packages/zod): Generate Zod Schemas from [Sanity Schemas](https://www.sanity.io/docs/schema-types).
|
|
492
493
|
|
|
493
494
|
#### Mocking
|
|
494
495
|
|
|
@@ -509,6 +510,7 @@ There are a growing number of tools that are built atop or support Zod natively!
|
|
|
509
510
|
#### Utilities for Zod
|
|
510
511
|
|
|
511
512
|
- [`zod_utilz`](https://github.com/JacobWeisenburger/zod_utilz): Framework agnostic utilities for Zod.
|
|
513
|
+
- [`zod-sandbox`](https://github.com/nereumelo/zod-sandbox): Controlled environment for testing zod schemas. [Live demo](https://zod-sandbox.vercel.app/).
|
|
512
514
|
|
|
513
515
|
## Installation
|
|
514
516
|
|
package/lib/ZodError.js
CHANGED
|
@@ -37,6 +37,7 @@ class ZodError extends Error {
|
|
|
37
37
|
};
|
|
38
38
|
const actualProto = new.target.prototype;
|
|
39
39
|
if (Object.setPrototypeOf) {
|
|
40
|
+
// eslint-disable-next-line ban/ban
|
|
40
41
|
Object.setPrototypeOf(this, actualProto);
|
|
41
42
|
}
|
|
42
43
|
else {
|
|
@@ -76,6 +77,13 @@ class ZodError extends Error {
|
|
|
76
77
|
const terminal = i === issue.path.length - 1;
|
|
77
78
|
if (!terminal) {
|
|
78
79
|
curr[el] = curr[el] || { _errors: [] };
|
|
80
|
+
// if (typeof el === "string") {
|
|
81
|
+
// curr[el] = curr[el] || { _errors: [] };
|
|
82
|
+
// } else if (typeof el === "number") {
|
|
83
|
+
// const errorArray: any = [];
|
|
84
|
+
// errorArray._errors = [];
|
|
85
|
+
// curr[el] = curr[el] || errorArray;
|
|
86
|
+
// }
|
|
79
87
|
}
|
|
80
88
|
else {
|
|
81
89
|
curr[el] = curr[el] || { _errors: [] };
|
package/lib/helpers/parseUtil.js
CHANGED
|
@@ -38,7 +38,7 @@ function addIssueToContext(ctx, issueData) {
|
|
|
38
38
|
ctx.common.contextualErrorMap,
|
|
39
39
|
ctx.schemaErrorMap,
|
|
40
40
|
(0, errors_1.getErrorMap)(),
|
|
41
|
-
en_1.default,
|
|
41
|
+
en_1.default, // then global default map
|
|
42
42
|
].filter((x) => !!x),
|
|
43
43
|
});
|
|
44
44
|
ctx.common.issues.push(issue);
|
package/lib/helpers/util.js
CHANGED
|
@@ -30,8 +30,8 @@ var util;
|
|
|
30
30
|
return obj[e];
|
|
31
31
|
});
|
|
32
32
|
};
|
|
33
|
-
util.objectKeys = typeof Object.keys === "function"
|
|
34
|
-
? (obj) => Object.keys(obj)
|
|
33
|
+
util.objectKeys = typeof Object.keys === "function" // eslint-disable-line ban/ban
|
|
34
|
+
? (obj) => Object.keys(obj) // eslint-disable-line ban/ban
|
|
35
35
|
: (object) => {
|
|
36
36
|
const keys = [];
|
|
37
37
|
for (const key in object) {
|
|
@@ -49,7 +49,7 @@ var util;
|
|
|
49
49
|
return undefined;
|
|
50
50
|
};
|
|
51
51
|
util.isInteger = typeof Number.isInteger === "function"
|
|
52
|
-
? (val) => Number.isInteger(val)
|
|
52
|
+
? (val) => Number.isInteger(val) // eslint-disable-line ban/ban
|
|
53
53
|
: (val) => typeof val === "number" && isFinite(val) && Math.floor(val) === val;
|
|
54
54
|
function joinValues(array, separator = " | ") {
|
|
55
55
|
return array
|
|
@@ -69,7 +69,7 @@ var objectUtil;
|
|
|
69
69
|
objectUtil.mergeShapes = (first, second) => {
|
|
70
70
|
return {
|
|
71
71
|
...first,
|
|
72
|
-
...second,
|
|
72
|
+
...second, // second overwrites first
|
|
73
73
|
};
|
|
74
74
|
};
|
|
75
75
|
})(objectUtil = exports.objectUtil || (exports.objectUtil = {}));
|
package/lib/index.mjs
CHANGED
|
@@ -816,7 +816,7 @@ class ZodType {
|
|
|
816
816
|
}
|
|
817
817
|
const cuidRegex = /^c[^\s-]{8,}$/i;
|
|
818
818
|
const cuid2Regex = /^[a-z][a-z0-9]*$/;
|
|
819
|
-
const ulidRegex =
|
|
819
|
+
const ulidRegex = /^[0-9A-HJKMNP-TV-Z]{26}$/;
|
|
820
820
|
// const uuidRegex =
|
|
821
821
|
// /^([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;
|
|
822
822
|
const uuidRegex = /^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/i;
|
|
@@ -836,7 +836,8 @@ const emailRegex = /^(?!\.)(?!.*\.\.)([A-Z0-9_+-\.]*)[A-Z0-9_+-]@([A-Z0-9][A-Z0-
|
|
|
836
836
|
// const emailRegex =
|
|
837
837
|
// /^[a-z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-z0-9-]+(?:\.[a-z0-9\-]+)*$/i;
|
|
838
838
|
// from https://thekevinscott.com/emojis-in-javascript/#writing-a-regular-expression
|
|
839
|
-
const
|
|
839
|
+
const _emojiRegex = `^(\\p{Extended_Pictographic}|\\p{Emoji_Component})+$`;
|
|
840
|
+
let emojiRegex;
|
|
840
841
|
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}))$/;
|
|
841
842
|
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})))$/;
|
|
842
843
|
// Adapted from https://stackoverflow.com/a/3143231
|
|
@@ -876,31 +877,6 @@ function isValidIP(ip, version) {
|
|
|
876
877
|
return false;
|
|
877
878
|
}
|
|
878
879
|
class ZodString extends ZodType {
|
|
879
|
-
constructor() {
|
|
880
|
-
super(...arguments);
|
|
881
|
-
this._regex = (regex, validation, message) => this.refinement((data) => regex.test(data), {
|
|
882
|
-
validation,
|
|
883
|
-
code: ZodIssueCode.invalid_string,
|
|
884
|
-
...errorUtil.errToObj(message),
|
|
885
|
-
});
|
|
886
|
-
/**
|
|
887
|
-
* @deprecated Use z.string().min(1) instead.
|
|
888
|
-
* @see {@link ZodString.min}
|
|
889
|
-
*/
|
|
890
|
-
this.nonempty = (message) => this.min(1, errorUtil.errToObj(message));
|
|
891
|
-
this.trim = () => new ZodString({
|
|
892
|
-
...this._def,
|
|
893
|
-
checks: [...this._def.checks, { kind: "trim" }],
|
|
894
|
-
});
|
|
895
|
-
this.toLowerCase = () => new ZodString({
|
|
896
|
-
...this._def,
|
|
897
|
-
checks: [...this._def.checks, { kind: "toLowerCase" }],
|
|
898
|
-
});
|
|
899
|
-
this.toUpperCase = () => new ZodString({
|
|
900
|
-
...this._def,
|
|
901
|
-
checks: [...this._def.checks, { kind: "toUpperCase" }],
|
|
902
|
-
});
|
|
903
|
-
}
|
|
904
880
|
_parse(input) {
|
|
905
881
|
if (this._def.coerce) {
|
|
906
882
|
input.data = String(input.data);
|
|
@@ -988,6 +964,9 @@ class ZodString extends ZodType {
|
|
|
988
964
|
}
|
|
989
965
|
}
|
|
990
966
|
else if (check.kind === "emoji") {
|
|
967
|
+
if (!emojiRegex) {
|
|
968
|
+
emojiRegex = new RegExp(_emojiRegex, "u");
|
|
969
|
+
}
|
|
991
970
|
if (!emojiRegex.test(input.data)) {
|
|
992
971
|
ctx = this._getOrReturnCtx(input, ctx);
|
|
993
972
|
addIssueToContext(ctx, {
|
|
@@ -1140,6 +1119,13 @@ class ZodString extends ZodType {
|
|
|
1140
1119
|
}
|
|
1141
1120
|
return { status: status.value, value: input.data };
|
|
1142
1121
|
}
|
|
1122
|
+
_regex(regex, validation, message) {
|
|
1123
|
+
return this.refinement((data) => regex.test(data), {
|
|
1124
|
+
validation,
|
|
1125
|
+
code: ZodIssueCode.invalid_string,
|
|
1126
|
+
...errorUtil.errToObj(message),
|
|
1127
|
+
});
|
|
1128
|
+
}
|
|
1143
1129
|
_addCheck(check) {
|
|
1144
1130
|
return new ZodString({
|
|
1145
1131
|
...this._def,
|
|
@@ -1237,6 +1223,31 @@ class ZodString extends ZodType {
|
|
|
1237
1223
|
...errorUtil.errToObj(message),
|
|
1238
1224
|
});
|
|
1239
1225
|
}
|
|
1226
|
+
/**
|
|
1227
|
+
* @deprecated Use z.string().min(1) instead.
|
|
1228
|
+
* @see {@link ZodString.min}
|
|
1229
|
+
*/
|
|
1230
|
+
nonempty(message) {
|
|
1231
|
+
return this.min(1, errorUtil.errToObj(message));
|
|
1232
|
+
}
|
|
1233
|
+
trim() {
|
|
1234
|
+
return new ZodString({
|
|
1235
|
+
...this._def,
|
|
1236
|
+
checks: [...this._def.checks, { kind: "trim" }],
|
|
1237
|
+
});
|
|
1238
|
+
}
|
|
1239
|
+
toLowerCase() {
|
|
1240
|
+
return new ZodString({
|
|
1241
|
+
...this._def,
|
|
1242
|
+
checks: [...this._def.checks, { kind: "toLowerCase" }],
|
|
1243
|
+
});
|
|
1244
|
+
}
|
|
1245
|
+
toUpperCase() {
|
|
1246
|
+
return new ZodString({
|
|
1247
|
+
...this._def,
|
|
1248
|
+
checks: [...this._def.checks, { kind: "toUpperCase" }],
|
|
1249
|
+
});
|
|
1250
|
+
}
|
|
1240
1251
|
get isDatetime() {
|
|
1241
1252
|
return !!this._def.checks.find((ch) => ch.kind === "datetime");
|
|
1242
1253
|
}
|
|
@@ -3770,7 +3781,7 @@ ZodReadonly.create = (type, params) => {
|
|
|
3770
3781
|
});
|
|
3771
3782
|
};
|
|
3772
3783
|
const custom = (check, params = {},
|
|
3773
|
-
|
|
3784
|
+
/**
|
|
3774
3785
|
* @deprecated
|
|
3775
3786
|
*
|
|
3776
3787
|
* Pass `fatal` into the params object instead:
|
package/lib/index.umd.js
CHANGED
|
@@ -822,7 +822,7 @@
|
|
|
822
822
|
}
|
|
823
823
|
const cuidRegex = /^c[^\s-]{8,}$/i;
|
|
824
824
|
const cuid2Regex = /^[a-z][a-z0-9]*$/;
|
|
825
|
-
const ulidRegex =
|
|
825
|
+
const ulidRegex = /^[0-9A-HJKMNP-TV-Z]{26}$/;
|
|
826
826
|
// const uuidRegex =
|
|
827
827
|
// /^([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;
|
|
828
828
|
const uuidRegex = /^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/i;
|
|
@@ -842,7 +842,8 @@
|
|
|
842
842
|
// const emailRegex =
|
|
843
843
|
// /^[a-z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-z0-9-]+(?:\.[a-z0-9\-]+)*$/i;
|
|
844
844
|
// from https://thekevinscott.com/emojis-in-javascript/#writing-a-regular-expression
|
|
845
|
-
const
|
|
845
|
+
const _emojiRegex = `^(\\p{Extended_Pictographic}|\\p{Emoji_Component})+$`;
|
|
846
|
+
let emojiRegex;
|
|
846
847
|
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}))$/;
|
|
847
848
|
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})))$/;
|
|
848
849
|
// Adapted from https://stackoverflow.com/a/3143231
|
|
@@ -882,31 +883,6 @@
|
|
|
882
883
|
return false;
|
|
883
884
|
}
|
|
884
885
|
class ZodString extends ZodType {
|
|
885
|
-
constructor() {
|
|
886
|
-
super(...arguments);
|
|
887
|
-
this._regex = (regex, validation, message) => this.refinement((data) => regex.test(data), {
|
|
888
|
-
validation,
|
|
889
|
-
code: ZodIssueCode.invalid_string,
|
|
890
|
-
...errorUtil.errToObj(message),
|
|
891
|
-
});
|
|
892
|
-
/**
|
|
893
|
-
* @deprecated Use z.string().min(1) instead.
|
|
894
|
-
* @see {@link ZodString.min}
|
|
895
|
-
*/
|
|
896
|
-
this.nonempty = (message) => this.min(1, errorUtil.errToObj(message));
|
|
897
|
-
this.trim = () => new ZodString({
|
|
898
|
-
...this._def,
|
|
899
|
-
checks: [...this._def.checks, { kind: "trim" }],
|
|
900
|
-
});
|
|
901
|
-
this.toLowerCase = () => new ZodString({
|
|
902
|
-
...this._def,
|
|
903
|
-
checks: [...this._def.checks, { kind: "toLowerCase" }],
|
|
904
|
-
});
|
|
905
|
-
this.toUpperCase = () => new ZodString({
|
|
906
|
-
...this._def,
|
|
907
|
-
checks: [...this._def.checks, { kind: "toUpperCase" }],
|
|
908
|
-
});
|
|
909
|
-
}
|
|
910
886
|
_parse(input) {
|
|
911
887
|
if (this._def.coerce) {
|
|
912
888
|
input.data = String(input.data);
|
|
@@ -994,6 +970,9 @@
|
|
|
994
970
|
}
|
|
995
971
|
}
|
|
996
972
|
else if (check.kind === "emoji") {
|
|
973
|
+
if (!emojiRegex) {
|
|
974
|
+
emojiRegex = new RegExp(_emojiRegex, "u");
|
|
975
|
+
}
|
|
997
976
|
if (!emojiRegex.test(input.data)) {
|
|
998
977
|
ctx = this._getOrReturnCtx(input, ctx);
|
|
999
978
|
addIssueToContext(ctx, {
|
|
@@ -1146,6 +1125,13 @@
|
|
|
1146
1125
|
}
|
|
1147
1126
|
return { status: status.value, value: input.data };
|
|
1148
1127
|
}
|
|
1128
|
+
_regex(regex, validation, message) {
|
|
1129
|
+
return this.refinement((data) => regex.test(data), {
|
|
1130
|
+
validation,
|
|
1131
|
+
code: ZodIssueCode.invalid_string,
|
|
1132
|
+
...errorUtil.errToObj(message),
|
|
1133
|
+
});
|
|
1134
|
+
}
|
|
1149
1135
|
_addCheck(check) {
|
|
1150
1136
|
return new ZodString({
|
|
1151
1137
|
...this._def,
|
|
@@ -1243,6 +1229,31 @@
|
|
|
1243
1229
|
...errorUtil.errToObj(message),
|
|
1244
1230
|
});
|
|
1245
1231
|
}
|
|
1232
|
+
/**
|
|
1233
|
+
* @deprecated Use z.string().min(1) instead.
|
|
1234
|
+
* @see {@link ZodString.min}
|
|
1235
|
+
*/
|
|
1236
|
+
nonempty(message) {
|
|
1237
|
+
return this.min(1, errorUtil.errToObj(message));
|
|
1238
|
+
}
|
|
1239
|
+
trim() {
|
|
1240
|
+
return new ZodString({
|
|
1241
|
+
...this._def,
|
|
1242
|
+
checks: [...this._def.checks, { kind: "trim" }],
|
|
1243
|
+
});
|
|
1244
|
+
}
|
|
1245
|
+
toLowerCase() {
|
|
1246
|
+
return new ZodString({
|
|
1247
|
+
...this._def,
|
|
1248
|
+
checks: [...this._def.checks, { kind: "toLowerCase" }],
|
|
1249
|
+
});
|
|
1250
|
+
}
|
|
1251
|
+
toUpperCase() {
|
|
1252
|
+
return new ZodString({
|
|
1253
|
+
...this._def,
|
|
1254
|
+
checks: [...this._def.checks, { kind: "toUpperCase" }],
|
|
1255
|
+
});
|
|
1256
|
+
}
|
|
1246
1257
|
get isDatetime() {
|
|
1247
1258
|
return !!this._def.checks.find((ch) => ch.kind === "datetime");
|
|
1248
1259
|
}
|
|
@@ -3776,7 +3787,7 @@
|
|
|
3776
3787
|
});
|
|
3777
3788
|
};
|
|
3778
3789
|
const custom = (check, params = {},
|
|
3779
|
-
|
|
3790
|
+
/**
|
|
3780
3791
|
* @deprecated
|
|
3781
3792
|
*
|
|
3782
3793
|
* Pass `fatal` into the params object instead:
|
package/lib/types.d.ts
CHANGED
|
@@ -60,6 +60,7 @@ export declare abstract class ZodType<Output = any, Def extends ZodTypeDef = Zod
|
|
|
60
60
|
safeParse(data: unknown, params?: Partial<ParseParams>): SafeParseReturnType<Input, Output>;
|
|
61
61
|
parseAsync(data: unknown, params?: Partial<ParseParams>): Promise<Output>;
|
|
62
62
|
safeParseAsync(data: unknown, params?: Partial<ParseParams>): Promise<SafeParseReturnType<Input, Output>>;
|
|
63
|
+
/** Alias of safeParseAsync */
|
|
63
64
|
spa: (data: unknown, params?: Partial<ParseParams> | undefined) => Promise<SafeParseReturnType<Input, Output>>;
|
|
64
65
|
refine<RefinedOutput extends Output>(check: (arg: Output) => arg is RefinedOutput, message?: string | CustomErrorParams | ((arg: Output) => CustomErrorParams)): ZodEffects<this, RefinedOutput, Input>;
|
|
65
66
|
refine(check: (arg: Output) => unknown | Promise<unknown>, message?: string | CustomErrorParams | ((arg: Output) => CustomErrorParams)): ZodEffects<this, Output, Input>;
|
|
@@ -169,7 +170,7 @@ export interface ZodStringDef extends ZodTypeDef {
|
|
|
169
170
|
}
|
|
170
171
|
export declare class ZodString extends ZodType<string, ZodStringDef> {
|
|
171
172
|
_parse(input: ParseInput): ParseReturnType<string>;
|
|
172
|
-
protected _regex
|
|
173
|
+
protected _regex(regex: RegExp, validation: StringValidation, message?: errorUtil.ErrMessage): ZodEffects<this, string, string>;
|
|
173
174
|
_addCheck(check: ZodStringCheck): ZodString;
|
|
174
175
|
email(message?: errorUtil.ErrMessage): ZodString;
|
|
175
176
|
url(message?: errorUtil.ErrMessage): ZodString;
|
|
@@ -197,10 +198,14 @@ export declare class ZodString extends ZodType<string, ZodStringDef> {
|
|
|
197
198
|
min(minLength: number, message?: errorUtil.ErrMessage): ZodString;
|
|
198
199
|
max(maxLength: number, message?: errorUtil.ErrMessage): ZodString;
|
|
199
200
|
length(len: number, message?: errorUtil.ErrMessage): ZodString;
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
201
|
+
/**
|
|
202
|
+
* @deprecated Use z.string().min(1) instead.
|
|
203
|
+
* @see {@link ZodString.min}
|
|
204
|
+
*/
|
|
205
|
+
nonempty(message?: errorUtil.ErrMessage): ZodString;
|
|
206
|
+
trim(): ZodString;
|
|
207
|
+
toLowerCase(): ZodString;
|
|
208
|
+
toUpperCase(): ZodString;
|
|
204
209
|
get isDatetime(): boolean;
|
|
205
210
|
get isEmail(): boolean;
|
|
206
211
|
get isURL(): boolean;
|
|
@@ -493,9 +498,21 @@ export declare class ZodObject<T extends ZodRawShape, UnknownKeys extends Unknow
|
|
|
493
498
|
strict(message?: errorUtil.ErrMessage): ZodObject<T, "strict", Catchall>;
|
|
494
499
|
strip(): ZodObject<T, "strip", Catchall>;
|
|
495
500
|
passthrough(): ZodObject<T, "passthrough", Catchall>;
|
|
501
|
+
/**
|
|
502
|
+
* @deprecated In most cases, this is no longer needed - unknown properties are now silently stripped.
|
|
503
|
+
* If you want to pass through unknown properties, use `.passthrough()` instead.
|
|
504
|
+
*/
|
|
496
505
|
nonstrict: () => ZodObject<T, "passthrough", Catchall>;
|
|
497
506
|
extend<Augmentation extends ZodRawShape>(augmentation: Augmentation): ZodObject<objectUtil.extendShape<T, Augmentation>, UnknownKeys, Catchall>;
|
|
507
|
+
/**
|
|
508
|
+
* @deprecated Use `.extend` instead
|
|
509
|
+
* */
|
|
498
510
|
augment: <Augmentation extends ZodRawShape>(augmentation: Augmentation) => ZodObject<{ [k in keyof (Omit<T, keyof Augmentation> & Augmentation)]: (Omit<T, keyof Augmentation> & Augmentation)[k]; }, UnknownKeys, Catchall, objectOutputType<{ [k in keyof (Omit<T, keyof Augmentation> & Augmentation)]: (Omit<T, keyof Augmentation> & Augmentation)[k]; }, Catchall, UnknownKeys>, objectInputType<{ [k in keyof (Omit<T, keyof Augmentation> & Augmentation)]: (Omit<T, keyof Augmentation> & Augmentation)[k]; }, Catchall, UnknownKeys>>;
|
|
511
|
+
/**
|
|
512
|
+
* Prior to zod@1.0.12 there was a bug in the
|
|
513
|
+
* inferred type of merged objects. Please
|
|
514
|
+
* upgrade if you are experiencing issues.
|
|
515
|
+
*/
|
|
499
516
|
merge<Incoming extends AnyZodObject, Augmentation extends Incoming["shape"]>(merging: Incoming): ZodObject<objectUtil.extendShape<T, Augmentation>, Incoming["_def"]["unknownKeys"], Incoming["_def"]["catchall"]>;
|
|
500
517
|
setKey<Key extends string, Schema extends ZodTypeAny>(key: Key, schema: Schema): ZodObject<T & {
|
|
501
518
|
[k in Key]: Schema;
|
|
@@ -507,6 +524,9 @@ export declare class ZodObject<T extends ZodRawShape, UnknownKeys extends Unknow
|
|
|
507
524
|
omit<Mask extends {
|
|
508
525
|
[k in keyof T]?: true;
|
|
509
526
|
}>(mask: Mask): ZodObject<Omit<T, keyof Mask>, UnknownKeys, Catchall>;
|
|
527
|
+
/**
|
|
528
|
+
* @deprecated
|
|
529
|
+
*/
|
|
510
530
|
deepPartial(): partialUtil.DeepPartial<this>;
|
|
511
531
|
partial(): ZodObject<{
|
|
512
532
|
[k in keyof T]: ZodOptional<T[k]>;
|
|
@@ -558,6 +578,14 @@ export declare class ZodDiscriminatedUnion<Discriminator extends string, Options
|
|
|
558
578
|
get discriminator(): Discriminator;
|
|
559
579
|
get options(): Options;
|
|
560
580
|
get optionsMap(): Map<Primitive, ZodDiscriminatedUnionOption<any>>;
|
|
581
|
+
/**
|
|
582
|
+
* The constructor of the discriminated union schema. Its behaviour is very similar to that of the normal z.union() constructor.
|
|
583
|
+
* However, it only allows a union of objects, all of which need to share a discriminator property. This property must
|
|
584
|
+
* have a different value for each object in the union.
|
|
585
|
+
* @param discriminator the name of the discriminator property
|
|
586
|
+
* @param types an array of object schemas
|
|
587
|
+
* @param params
|
|
588
|
+
*/
|
|
561
589
|
static create<Discriminator extends string, Types extends [
|
|
562
590
|
ZodDiscriminatedUnionOption<Discriminator>,
|
|
563
591
|
...ZodDiscriminatedUnionOption<Discriminator>[]
|
package/lib/types.js
CHANGED
|
@@ -68,6 +68,7 @@ function processCreateParams(params) {
|
|
|
68
68
|
}
|
|
69
69
|
class ZodType {
|
|
70
70
|
constructor(def) {
|
|
71
|
+
/** Alias of safeParseAsync */
|
|
71
72
|
this.spa = this.safeParseAsync;
|
|
72
73
|
this._def = def;
|
|
73
74
|
this.parse = this.parse.bind(this);
|
|
@@ -323,12 +324,31 @@ exports.Schema = ZodType;
|
|
|
323
324
|
exports.ZodSchema = ZodType;
|
|
324
325
|
const cuidRegex = /^c[^\s-]{8,}$/i;
|
|
325
326
|
const cuid2Regex = /^[a-z][a-z0-9]*$/;
|
|
326
|
-
const ulidRegex =
|
|
327
|
+
const ulidRegex = /^[0-9A-HJKMNP-TV-Z]{26}$/;
|
|
328
|
+
// const uuidRegex =
|
|
329
|
+
// /^([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;
|
|
327
330
|
const uuidRegex = /^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/i;
|
|
331
|
+
// from https://stackoverflow.com/a/46181/1550155
|
|
332
|
+
// old version: too slow, didn't support unicode
|
|
333
|
+
// const emailRegex = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$/i;
|
|
334
|
+
//old email regex
|
|
335
|
+
// const emailRegex = /^(([^<>()[\].,;:\s@"]+(\.[^<>()[\].,;:\s@"]+)*)|(".+"))@((?!-)([^<>()[\].,;:\s@"]+\.)+[^<>()[\].,;:\s@"]{1,})[^-<>()[\].,;:\s@"]$/i;
|
|
336
|
+
// eslint-disable-next-line
|
|
337
|
+
// const emailRegex =
|
|
338
|
+
// /^(([^<>()[\]\\.,;:\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,})+))$/;
|
|
339
|
+
// const emailRegex =
|
|
340
|
+
// /^[a-zA-Z0-9\.\!\#\$\%\&\'\*\+\/\=\?\^\_\`\{\|\}\~\-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
|
|
341
|
+
// const emailRegex =
|
|
342
|
+
// /^(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])$/i;
|
|
328
343
|
const emailRegex = /^(?!\.)(?!.*\.\.)([A-Z0-9_+-\.]*)[A-Z0-9_+-]@([A-Z0-9][A-Z0-9\-]*\.)+[A-Z]{2,}$/i;
|
|
329
|
-
const
|
|
344
|
+
// const emailRegex =
|
|
345
|
+
// /^[a-z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-z0-9-]+(?:\.[a-z0-9\-]+)*$/i;
|
|
346
|
+
// from https://thekevinscott.com/emojis-in-javascript/#writing-a-regular-expression
|
|
347
|
+
const _emojiRegex = `^(\\p{Extended_Pictographic}|\\p{Emoji_Component})+$`;
|
|
348
|
+
let emojiRegex;
|
|
330
349
|
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}))$/;
|
|
331
350
|
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})))$/;
|
|
351
|
+
// Adapted from https://stackoverflow.com/a/3143231
|
|
332
352
|
const datetimeRegex = (args) => {
|
|
333
353
|
if (args.precision) {
|
|
334
354
|
if (args.offset) {
|
|
@@ -365,27 +385,6 @@ function isValidIP(ip, version) {
|
|
|
365
385
|
return false;
|
|
366
386
|
}
|
|
367
387
|
class ZodString extends ZodType {
|
|
368
|
-
constructor() {
|
|
369
|
-
super(...arguments);
|
|
370
|
-
this._regex = (regex, validation, message) => this.refinement((data) => regex.test(data), {
|
|
371
|
-
validation,
|
|
372
|
-
code: ZodError_1.ZodIssueCode.invalid_string,
|
|
373
|
-
...errorUtil_1.errorUtil.errToObj(message),
|
|
374
|
-
});
|
|
375
|
-
this.nonempty = (message) => this.min(1, errorUtil_1.errorUtil.errToObj(message));
|
|
376
|
-
this.trim = () => new ZodString({
|
|
377
|
-
...this._def,
|
|
378
|
-
checks: [...this._def.checks, { kind: "trim" }],
|
|
379
|
-
});
|
|
380
|
-
this.toLowerCase = () => new ZodString({
|
|
381
|
-
...this._def,
|
|
382
|
-
checks: [...this._def.checks, { kind: "toLowerCase" }],
|
|
383
|
-
});
|
|
384
|
-
this.toUpperCase = () => new ZodString({
|
|
385
|
-
...this._def,
|
|
386
|
-
checks: [...this._def.checks, { kind: "toUpperCase" }],
|
|
387
|
-
});
|
|
388
|
-
}
|
|
389
388
|
_parse(input) {
|
|
390
389
|
if (this._def.coerce) {
|
|
391
390
|
input.data = String(input.data);
|
|
@@ -397,7 +396,9 @@ class ZodString extends ZodType {
|
|
|
397
396
|
code: ZodError_1.ZodIssueCode.invalid_type,
|
|
398
397
|
expected: util_1.ZodParsedType.string,
|
|
399
398
|
received: ctx.parsedType,
|
|
400
|
-
}
|
|
399
|
+
}
|
|
400
|
+
//
|
|
401
|
+
);
|
|
401
402
|
return parseUtil_1.INVALID;
|
|
402
403
|
}
|
|
403
404
|
const status = new parseUtil_1.ParseStatus();
|
|
@@ -471,6 +472,9 @@ class ZodString extends ZodType {
|
|
|
471
472
|
}
|
|
472
473
|
}
|
|
473
474
|
else if (check.kind === "emoji") {
|
|
475
|
+
if (!emojiRegex) {
|
|
476
|
+
emojiRegex = new RegExp(_emojiRegex, "u");
|
|
477
|
+
}
|
|
474
478
|
if (!emojiRegex.test(input.data)) {
|
|
475
479
|
ctx = this._getOrReturnCtx(input, ctx);
|
|
476
480
|
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
@@ -623,6 +627,13 @@ class ZodString extends ZodType {
|
|
|
623
627
|
}
|
|
624
628
|
return { status: status.value, value: input.data };
|
|
625
629
|
}
|
|
630
|
+
_regex(regex, validation, message) {
|
|
631
|
+
return this.refinement((data) => regex.test(data), {
|
|
632
|
+
validation,
|
|
633
|
+
code: ZodError_1.ZodIssueCode.invalid_string,
|
|
634
|
+
...errorUtil_1.errorUtil.errToObj(message),
|
|
635
|
+
});
|
|
636
|
+
}
|
|
626
637
|
_addCheck(check) {
|
|
627
638
|
return new ZodString({
|
|
628
639
|
...this._def,
|
|
@@ -720,6 +731,31 @@ class ZodString extends ZodType {
|
|
|
720
731
|
...errorUtil_1.errorUtil.errToObj(message),
|
|
721
732
|
});
|
|
722
733
|
}
|
|
734
|
+
/**
|
|
735
|
+
* @deprecated Use z.string().min(1) instead.
|
|
736
|
+
* @see {@link ZodString.min}
|
|
737
|
+
*/
|
|
738
|
+
nonempty(message) {
|
|
739
|
+
return this.min(1, errorUtil_1.errorUtil.errToObj(message));
|
|
740
|
+
}
|
|
741
|
+
trim() {
|
|
742
|
+
return new ZodString({
|
|
743
|
+
...this._def,
|
|
744
|
+
checks: [...this._def.checks, { kind: "trim" }],
|
|
745
|
+
});
|
|
746
|
+
}
|
|
747
|
+
toLowerCase() {
|
|
748
|
+
return new ZodString({
|
|
749
|
+
...this._def,
|
|
750
|
+
checks: [...this._def.checks, { kind: "toLowerCase" }],
|
|
751
|
+
});
|
|
752
|
+
}
|
|
753
|
+
toUpperCase() {
|
|
754
|
+
return new ZodString({
|
|
755
|
+
...this._def,
|
|
756
|
+
checks: [...this._def.checks, { kind: "toUpperCase" }],
|
|
757
|
+
});
|
|
758
|
+
}
|
|
723
759
|
get isDatetime() {
|
|
724
760
|
return !!this._def.checks.find((ch) => ch.kind === "datetime");
|
|
725
761
|
}
|
|
@@ -778,6 +814,7 @@ ZodString.create = (params) => {
|
|
|
778
814
|
...processCreateParams(params),
|
|
779
815
|
});
|
|
780
816
|
};
|
|
817
|
+
// https://stackoverflow.com/questions/3966484/why-does-modulus-operator-return-fractional-number-in-javascript/31711034#31711034
|
|
781
818
|
function floatSafeRemainder(val, step) {
|
|
782
819
|
const valDecCount = (val.toString().split(".")[1] || "").length;
|
|
783
820
|
const stepDecCount = (step.toString().split(".")[1] || "").length;
|
|
@@ -1413,6 +1450,7 @@ ZodNull.create = (params) => {
|
|
|
1413
1450
|
class ZodAny extends ZodType {
|
|
1414
1451
|
constructor() {
|
|
1415
1452
|
super(...arguments);
|
|
1453
|
+
// to prevent instances of other classes from extending ZodAny. this causes issues with catchall in ZodObject.
|
|
1416
1454
|
this._any = true;
|
|
1417
1455
|
}
|
|
1418
1456
|
_parse(input) {
|
|
@@ -1429,6 +1467,7 @@ ZodAny.create = (params) => {
|
|
|
1429
1467
|
class ZodUnknown extends ZodType {
|
|
1430
1468
|
constructor() {
|
|
1431
1469
|
super(...arguments);
|
|
1470
|
+
// required
|
|
1432
1471
|
this._unknown = true;
|
|
1433
1472
|
}
|
|
1434
1473
|
_parse(input) {
|
|
@@ -1619,7 +1658,47 @@ class ZodObject extends ZodType {
|
|
|
1619
1658
|
constructor() {
|
|
1620
1659
|
super(...arguments);
|
|
1621
1660
|
this._cached = null;
|
|
1661
|
+
/**
|
|
1662
|
+
* @deprecated In most cases, this is no longer needed - unknown properties are now silently stripped.
|
|
1663
|
+
* If you want to pass through unknown properties, use `.passthrough()` instead.
|
|
1664
|
+
*/
|
|
1622
1665
|
this.nonstrict = this.passthrough;
|
|
1666
|
+
// extend<
|
|
1667
|
+
// Augmentation extends ZodRawShape,
|
|
1668
|
+
// NewOutput extends util.flatten<{
|
|
1669
|
+
// [k in keyof Augmentation | keyof Output]: k extends keyof Augmentation
|
|
1670
|
+
// ? Augmentation[k]["_output"]
|
|
1671
|
+
// : k extends keyof Output
|
|
1672
|
+
// ? Output[k]
|
|
1673
|
+
// : never;
|
|
1674
|
+
// }>,
|
|
1675
|
+
// NewInput extends util.flatten<{
|
|
1676
|
+
// [k in keyof Augmentation | keyof Input]: k extends keyof Augmentation
|
|
1677
|
+
// ? Augmentation[k]["_input"]
|
|
1678
|
+
// : k extends keyof Input
|
|
1679
|
+
// ? Input[k]
|
|
1680
|
+
// : never;
|
|
1681
|
+
// }>
|
|
1682
|
+
// >(
|
|
1683
|
+
// augmentation: Augmentation
|
|
1684
|
+
// ): ZodObject<
|
|
1685
|
+
// extendShape<T, Augmentation>,
|
|
1686
|
+
// UnknownKeys,
|
|
1687
|
+
// Catchall,
|
|
1688
|
+
// NewOutput,
|
|
1689
|
+
// NewInput
|
|
1690
|
+
// > {
|
|
1691
|
+
// return new ZodObject({
|
|
1692
|
+
// ...this._def,
|
|
1693
|
+
// shape: () => ({
|
|
1694
|
+
// ...this._def.shape(),
|
|
1695
|
+
// ...augmentation,
|
|
1696
|
+
// }),
|
|
1697
|
+
// }) as any;
|
|
1698
|
+
// }
|
|
1699
|
+
/**
|
|
1700
|
+
* @deprecated Use `.extend` instead
|
|
1701
|
+
* */
|
|
1623
1702
|
this.augment = this.extend;
|
|
1624
1703
|
}
|
|
1625
1704
|
_getCached() {
|
|
@@ -1687,12 +1766,14 @@ class ZodObject extends ZodType {
|
|
|
1687
1766
|
}
|
|
1688
1767
|
}
|
|
1689
1768
|
else {
|
|
1769
|
+
// run catchall validation
|
|
1690
1770
|
const catchall = this._def.catchall;
|
|
1691
1771
|
for (const key of extraKeys) {
|
|
1692
1772
|
const value = ctx.data[key];
|
|
1693
1773
|
pairs.push({
|
|
1694
1774
|
key: { status: "valid", value: key },
|
|
1695
|
-
value: catchall._parse(new ParseInputLazyPath(ctx, value, ctx.path, key)),
|
|
1775
|
+
value: catchall._parse(new ParseInputLazyPath(ctx, value, ctx.path, key) //, ctx.child(key), value, getParsedType(value)
|
|
1776
|
+
),
|
|
1696
1777
|
alwaysSet: key in ctx.data,
|
|
1697
1778
|
});
|
|
1698
1779
|
}
|
|
@@ -1756,6 +1837,23 @@ class ZodObject extends ZodType {
|
|
|
1756
1837
|
unknownKeys: "passthrough",
|
|
1757
1838
|
});
|
|
1758
1839
|
}
|
|
1840
|
+
// const AugmentFactory =
|
|
1841
|
+
// <Def extends ZodObjectDef>(def: Def) =>
|
|
1842
|
+
// <Augmentation extends ZodRawShape>(
|
|
1843
|
+
// augmentation: Augmentation
|
|
1844
|
+
// ): ZodObject<
|
|
1845
|
+
// extendShape<ReturnType<Def["shape"]>, Augmentation>,
|
|
1846
|
+
// Def["unknownKeys"],
|
|
1847
|
+
// Def["catchall"]
|
|
1848
|
+
// > => {
|
|
1849
|
+
// return new ZodObject({
|
|
1850
|
+
// ...def,
|
|
1851
|
+
// shape: () => ({
|
|
1852
|
+
// ...def.shape(),
|
|
1853
|
+
// ...augmentation,
|
|
1854
|
+
// }),
|
|
1855
|
+
// }) as any;
|
|
1856
|
+
// };
|
|
1759
1857
|
extend(augmentation) {
|
|
1760
1858
|
return new ZodObject({
|
|
1761
1859
|
...this._def,
|
|
@@ -1765,6 +1863,11 @@ class ZodObject extends ZodType {
|
|
|
1765
1863
|
}),
|
|
1766
1864
|
});
|
|
1767
1865
|
}
|
|
1866
|
+
/**
|
|
1867
|
+
* Prior to zod@1.0.12 there was a bug in the
|
|
1868
|
+
* inferred type of merged objects. Please
|
|
1869
|
+
* upgrade if you are experiencing issues.
|
|
1870
|
+
*/
|
|
1768
1871
|
merge(merging) {
|
|
1769
1872
|
const merged = new ZodObject({
|
|
1770
1873
|
unknownKeys: merging._def.unknownKeys,
|
|
@@ -1777,9 +1880,65 @@ class ZodObject extends ZodType {
|
|
|
1777
1880
|
});
|
|
1778
1881
|
return merged;
|
|
1779
1882
|
}
|
|
1883
|
+
// merge<
|
|
1884
|
+
// Incoming extends AnyZodObject,
|
|
1885
|
+
// Augmentation extends Incoming["shape"],
|
|
1886
|
+
// NewOutput extends {
|
|
1887
|
+
// [k in keyof Augmentation | keyof Output]: k extends keyof Augmentation
|
|
1888
|
+
// ? Augmentation[k]["_output"]
|
|
1889
|
+
// : k extends keyof Output
|
|
1890
|
+
// ? Output[k]
|
|
1891
|
+
// : never;
|
|
1892
|
+
// },
|
|
1893
|
+
// NewInput extends {
|
|
1894
|
+
// [k in keyof Augmentation | keyof Input]: k extends keyof Augmentation
|
|
1895
|
+
// ? Augmentation[k]["_input"]
|
|
1896
|
+
// : k extends keyof Input
|
|
1897
|
+
// ? Input[k]
|
|
1898
|
+
// : never;
|
|
1899
|
+
// }
|
|
1900
|
+
// >(
|
|
1901
|
+
// merging: Incoming
|
|
1902
|
+
// ): ZodObject<
|
|
1903
|
+
// extendShape<T, ReturnType<Incoming["_def"]["shape"]>>,
|
|
1904
|
+
// Incoming["_def"]["unknownKeys"],
|
|
1905
|
+
// Incoming["_def"]["catchall"],
|
|
1906
|
+
// NewOutput,
|
|
1907
|
+
// NewInput
|
|
1908
|
+
// > {
|
|
1909
|
+
// const merged: any = new ZodObject({
|
|
1910
|
+
// unknownKeys: merging._def.unknownKeys,
|
|
1911
|
+
// catchall: merging._def.catchall,
|
|
1912
|
+
// shape: () =>
|
|
1913
|
+
// objectUtil.mergeShapes(this._def.shape(), merging._def.shape()),
|
|
1914
|
+
// typeName: ZodFirstPartyTypeKind.ZodObject,
|
|
1915
|
+
// }) as any;
|
|
1916
|
+
// return merged;
|
|
1917
|
+
// }
|
|
1780
1918
|
setKey(key, schema) {
|
|
1781
1919
|
return this.augment({ [key]: schema });
|
|
1782
1920
|
}
|
|
1921
|
+
// merge<Incoming extends AnyZodObject>(
|
|
1922
|
+
// merging: Incoming
|
|
1923
|
+
// ): //ZodObject<T & Incoming["_shape"], UnknownKeys, Catchall> = (merging) => {
|
|
1924
|
+
// ZodObject<
|
|
1925
|
+
// extendShape<T, ReturnType<Incoming["_def"]["shape"]>>,
|
|
1926
|
+
// Incoming["_def"]["unknownKeys"],
|
|
1927
|
+
// Incoming["_def"]["catchall"]
|
|
1928
|
+
// > {
|
|
1929
|
+
// // const mergedShape = objectUtil.mergeShapes(
|
|
1930
|
+
// // this._def.shape(),
|
|
1931
|
+
// // merging._def.shape()
|
|
1932
|
+
// // );
|
|
1933
|
+
// const merged: any = new ZodObject({
|
|
1934
|
+
// unknownKeys: merging._def.unknownKeys,
|
|
1935
|
+
// catchall: merging._def.catchall,
|
|
1936
|
+
// shape: () =>
|
|
1937
|
+
// objectUtil.mergeShapes(this._def.shape(), merging._def.shape()),
|
|
1938
|
+
// typeName: ZodFirstPartyTypeKind.ZodObject,
|
|
1939
|
+
// }) as any;
|
|
1940
|
+
// return merged;
|
|
1941
|
+
// }
|
|
1783
1942
|
catchall(index) {
|
|
1784
1943
|
return new ZodObject({
|
|
1785
1944
|
...this._def,
|
|
@@ -1810,6 +1969,9 @@ class ZodObject extends ZodType {
|
|
|
1810
1969
|
shape: () => shape,
|
|
1811
1970
|
});
|
|
1812
1971
|
}
|
|
1972
|
+
/**
|
|
1973
|
+
* @deprecated
|
|
1974
|
+
*/
|
|
1813
1975
|
deepPartial() {
|
|
1814
1976
|
return deepPartialify(this);
|
|
1815
1977
|
}
|
|
@@ -1886,6 +2048,7 @@ class ZodUnion extends ZodType {
|
|
|
1886
2048
|
const { ctx } = this._processInputParams(input);
|
|
1887
2049
|
const options = this._def.options;
|
|
1888
2050
|
function handleResults(results) {
|
|
2051
|
+
// return first issue-free validation if it exists
|
|
1889
2052
|
for (const result of results) {
|
|
1890
2053
|
if (result.result.status === "valid") {
|
|
1891
2054
|
return result.result;
|
|
@@ -1893,10 +2056,12 @@ class ZodUnion extends ZodType {
|
|
|
1893
2056
|
}
|
|
1894
2057
|
for (const result of results) {
|
|
1895
2058
|
if (result.result.status === "dirty") {
|
|
2059
|
+
// add issues from dirty option
|
|
1896
2060
|
ctx.common.issues.push(...result.ctx.common.issues);
|
|
1897
2061
|
return result.result;
|
|
1898
2062
|
}
|
|
1899
2063
|
}
|
|
2064
|
+
// return invalid
|
|
1900
2065
|
const unionErrors = results.map((result) => new ZodError_1.ZodError(result.ctx.common.issues));
|
|
1901
2066
|
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
1902
2067
|
code: ZodError_1.ZodIssueCode.invalid_union,
|
|
@@ -1975,6 +2140,13 @@ ZodUnion.create = (types, params) => {
|
|
|
1975
2140
|
...processCreateParams(params),
|
|
1976
2141
|
});
|
|
1977
2142
|
};
|
|
2143
|
+
/////////////////////////////////////////////////////
|
|
2144
|
+
/////////////////////////////////////////////////////
|
|
2145
|
+
////////// //////////
|
|
2146
|
+
////////// ZodDiscriminatedUnion //////////
|
|
2147
|
+
////////// //////////
|
|
2148
|
+
/////////////////////////////////////////////////////
|
|
2149
|
+
/////////////////////////////////////////////////////
|
|
1978
2150
|
const getDiscriminator = (type) => {
|
|
1979
2151
|
if (type instanceof ZodLazy) {
|
|
1980
2152
|
return getDiscriminator(type.schema);
|
|
@@ -1989,6 +2161,7 @@ const getDiscriminator = (type) => {
|
|
|
1989
2161
|
return type.options;
|
|
1990
2162
|
}
|
|
1991
2163
|
else if (type instanceof ZodNativeEnum) {
|
|
2164
|
+
// eslint-disable-next-line ban/ban
|
|
1992
2165
|
return Object.keys(type.enum);
|
|
1993
2166
|
}
|
|
1994
2167
|
else if (type instanceof ZodDefault) {
|
|
@@ -2050,8 +2223,18 @@ class ZodDiscriminatedUnion extends ZodType {
|
|
|
2050
2223
|
get optionsMap() {
|
|
2051
2224
|
return this._def.optionsMap;
|
|
2052
2225
|
}
|
|
2226
|
+
/**
|
|
2227
|
+
* The constructor of the discriminated union schema. Its behaviour is very similar to that of the normal z.union() constructor.
|
|
2228
|
+
* However, it only allows a union of objects, all of which need to share a discriminator property. This property must
|
|
2229
|
+
* have a different value for each object in the union.
|
|
2230
|
+
* @param discriminator the name of the discriminator property
|
|
2231
|
+
* @param types an array of object schemas
|
|
2232
|
+
* @param params
|
|
2233
|
+
*/
|
|
2053
2234
|
static create(discriminator, options, params) {
|
|
2235
|
+
// Get all the valid discriminator values
|
|
2054
2236
|
const optionsMap = new Map();
|
|
2237
|
+
// try {
|
|
2055
2238
|
for (const type of options) {
|
|
2056
2239
|
const discriminatorValues = getDiscriminator(type.shape[discriminator]);
|
|
2057
2240
|
if (!discriminatorValues) {
|
|
@@ -2214,7 +2397,7 @@ class ZodTuple extends ZodType {
|
|
|
2214
2397
|
return null;
|
|
2215
2398
|
return schema._parse(new ParseInputLazyPath(ctx, item, ctx.path, itemIndex));
|
|
2216
2399
|
})
|
|
2217
|
-
.filter((x) => !!x);
|
|
2400
|
+
.filter((x) => !!x); // filter nulls
|
|
2218
2401
|
if (ctx.common.async) {
|
|
2219
2402
|
return Promise.all(items).then((results) => {
|
|
2220
2403
|
return parseUtil_1.ParseStatus.mergeArray(status, results);
|
|
@@ -2505,6 +2688,9 @@ class ZodFunction extends ZodType {
|
|
|
2505
2688
|
const params = { errorMap: ctx.common.contextualErrorMap };
|
|
2506
2689
|
const fn = ctx.data;
|
|
2507
2690
|
if (this._def.returns instanceof ZodPromise) {
|
|
2691
|
+
// Would love a way to avoid disabling this rule, but we need
|
|
2692
|
+
// an alias (using an arrow function was what caused 2651).
|
|
2693
|
+
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
2508
2694
|
const me = this;
|
|
2509
2695
|
return (0, parseUtil_1.OK)(async function (...args) {
|
|
2510
2696
|
const error = new ZodError_1.ZodError([]);
|
|
@@ -2525,6 +2711,9 @@ class ZodFunction extends ZodType {
|
|
|
2525
2711
|
});
|
|
2526
2712
|
}
|
|
2527
2713
|
else {
|
|
2714
|
+
// Would love a way to avoid disabling this rule, but we need
|
|
2715
|
+
// an alias (using an arrow function was what caused 2651).
|
|
2716
|
+
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
2528
2717
|
const me = this;
|
|
2529
2718
|
return (0, parseUtil_1.OK)(function (...args) {
|
|
2530
2719
|
const parsedArgs = me._def.args.safeParse(args, params);
|
|
@@ -2809,7 +2998,9 @@ class ZodEffects extends ZodType {
|
|
|
2809
2998
|
}
|
|
2810
2999
|
}
|
|
2811
3000
|
if (effect.type === "refinement") {
|
|
2812
|
-
const executeRefinement = (acc
|
|
3001
|
+
const executeRefinement = (acc
|
|
3002
|
+
// effect: RefinementEffect<any>
|
|
3003
|
+
) => {
|
|
2813
3004
|
const result = effect.refinement(acc, checkCtx);
|
|
2814
3005
|
if (ctx.common.async) {
|
|
2815
3006
|
return Promise.resolve(result);
|
|
@@ -2829,6 +3020,7 @@ class ZodEffects extends ZodType {
|
|
|
2829
3020
|
return parseUtil_1.INVALID;
|
|
2830
3021
|
if (inner.status === "dirty")
|
|
2831
3022
|
status.dirty();
|
|
3023
|
+
// return value is ignored
|
|
2832
3024
|
executeRefinement(inner.value);
|
|
2833
3025
|
return { status: status.value, value: inner.value };
|
|
2834
3026
|
}
|
|
@@ -2963,6 +3155,7 @@ ZodDefault.create = (type, params) => {
|
|
|
2963
3155
|
class ZodCatch extends ZodType {
|
|
2964
3156
|
_parse(input) {
|
|
2965
3157
|
const { ctx } = this._processInputParams(input);
|
|
3158
|
+
// newCtx is used to not collect issues from inner types in ctx
|
|
2966
3159
|
const newCtx = {
|
|
2967
3160
|
...ctx,
|
|
2968
3161
|
common: {
|
|
@@ -3133,7 +3326,18 @@ ZodReadonly.create = (type, params) => {
|
|
|
3133
3326
|
...processCreateParams(params),
|
|
3134
3327
|
});
|
|
3135
3328
|
};
|
|
3136
|
-
const custom = (check, params = {},
|
|
3329
|
+
const custom = (check, params = {},
|
|
3330
|
+
/**
|
|
3331
|
+
* @deprecated
|
|
3332
|
+
*
|
|
3333
|
+
* Pass `fatal` into the params object instead:
|
|
3334
|
+
*
|
|
3335
|
+
* ```ts
|
|
3336
|
+
* z.string().custom((val) => val.length > 5, { fatal: false })
|
|
3337
|
+
* ```
|
|
3338
|
+
*
|
|
3339
|
+
*/
|
|
3340
|
+
fatal) => {
|
|
3137
3341
|
if (check)
|
|
3138
3342
|
return ZodAny.create().superRefine((data, ctx) => {
|
|
3139
3343
|
var _a, _b;
|
|
@@ -3193,10 +3397,13 @@ var ZodFirstPartyTypeKind;
|
|
|
3193
3397
|
ZodFirstPartyTypeKind["ZodPipeline"] = "ZodPipeline";
|
|
3194
3398
|
ZodFirstPartyTypeKind["ZodReadonly"] = "ZodReadonly";
|
|
3195
3399
|
})(ZodFirstPartyTypeKind = exports.ZodFirstPartyTypeKind || (exports.ZodFirstPartyTypeKind = {}));
|
|
3400
|
+
// requires TS 4.4+
|
|
3196
3401
|
class Class {
|
|
3197
3402
|
constructor(..._) { }
|
|
3198
3403
|
}
|
|
3199
|
-
const instanceOfType = (
|
|
3404
|
+
const instanceOfType = (
|
|
3405
|
+
// const instanceOfType = <T extends new (...args: any[]) => any>(
|
|
3406
|
+
cls, params = {
|
|
3200
3407
|
message: `Input not instance of ${cls.name}`,
|
|
3201
3408
|
}) => (0, exports.custom)((data) => data instanceof cls, params);
|
|
3202
3409
|
exports.instanceof = instanceOfType;
|