@dodopayments/convex 0.2.0 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +62 -37
- package/dist/component/lib.js +1 -1
- package/dist/component/lib.js.map +1 -1
- package/dist/index.cjs +4192 -232
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +4192 -232
- package/dist/index.js.map +1 -1
- package/dist/node_modules/zod/v3/ZodError.js +133 -0
- package/dist/node_modules/zod/v3/ZodError.js.map +1 -0
- package/dist/node_modules/zod/v3/errors.js +9 -0
- package/dist/node_modules/zod/v3/errors.js.map +1 -0
- package/dist/node_modules/zod/v3/helpers/errorUtil.js +9 -0
- package/dist/node_modules/zod/v3/helpers/errorUtil.js.map +1 -0
- package/dist/node_modules/zod/v3/helpers/parseUtil.js +112 -0
- package/dist/node_modules/zod/v3/helpers/parseUtil.js.map +1 -0
- package/dist/node_modules/zod/v3/helpers/util.js +136 -0
- package/dist/node_modules/zod/v3/helpers/util.js.map +1 -0
- package/dist/node_modules/zod/v3/locales/en.js +112 -0
- package/dist/node_modules/zod/v3/locales/en.js.map +1 -0
- package/dist/node_modules/zod/v3/types.js +3356 -0
- package/dist/node_modules/zod/v3/types.js.map +1 -0
- package/dist/{core → packages/core}/dist/checkout/checkout.js +88 -99
- package/dist/packages/core/dist/checkout/checkout.js.map +1 -0
- package/package.json +3 -3
- package/dist/core/dist/checkout/checkout.js.map +0 -1
package/dist/index.cjs
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var zod = require('zod');
|
|
4
|
-
|
|
5
3
|
// This is the public-facing client that developers will use.
|
|
6
4
|
class DodoPayments {
|
|
7
5
|
constructor(component, config) {
|
|
@@ -990,44 +988,4014 @@ var convex_config = defineComponent("dodopayments");
|
|
|
990
988
|
*/
|
|
991
989
|
const httpAction = httpActionGeneric;
|
|
992
990
|
|
|
993
|
-
var
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
991
|
+
var util;
|
|
992
|
+
(function (util) {
|
|
993
|
+
util.assertEqual = (_) => { };
|
|
994
|
+
function assertIs(_arg) { }
|
|
995
|
+
util.assertIs = assertIs;
|
|
996
|
+
function assertNever(_x) {
|
|
997
|
+
throw new Error();
|
|
998
|
+
}
|
|
999
|
+
util.assertNever = assertNever;
|
|
1000
|
+
util.arrayToEnum = (items) => {
|
|
1001
|
+
const obj = {};
|
|
1002
|
+
for (const item of items) {
|
|
1003
|
+
obj[item] = item;
|
|
1004
|
+
}
|
|
1005
|
+
return obj;
|
|
1006
|
+
};
|
|
1007
|
+
util.getValidEnumValues = (obj) => {
|
|
1008
|
+
const validKeys = util.objectKeys(obj).filter((k) => typeof obj[obj[k]] !== "number");
|
|
1009
|
+
const filtered = {};
|
|
1010
|
+
for (const k of validKeys) {
|
|
1011
|
+
filtered[k] = obj[k];
|
|
1012
|
+
}
|
|
1013
|
+
return util.objectValues(filtered);
|
|
1014
|
+
};
|
|
1015
|
+
util.objectValues = (obj) => {
|
|
1016
|
+
return util.objectKeys(obj).map(function (e) {
|
|
1017
|
+
return obj[e];
|
|
1018
|
+
});
|
|
1019
|
+
};
|
|
1020
|
+
util.objectKeys = typeof Object.keys === "function" // eslint-disable-line ban/ban
|
|
1021
|
+
? (obj) => Object.keys(obj) // eslint-disable-line ban/ban
|
|
1022
|
+
: (object) => {
|
|
1023
|
+
const keys = [];
|
|
1024
|
+
for (const key in object) {
|
|
1025
|
+
if (Object.prototype.hasOwnProperty.call(object, key)) {
|
|
1026
|
+
keys.push(key);
|
|
1027
|
+
}
|
|
1028
|
+
}
|
|
1029
|
+
return keys;
|
|
1030
|
+
};
|
|
1031
|
+
util.find = (arr, checker) => {
|
|
1032
|
+
for (const item of arr) {
|
|
1033
|
+
if (checker(item))
|
|
1034
|
+
return item;
|
|
1035
|
+
}
|
|
1036
|
+
return undefined;
|
|
1037
|
+
};
|
|
1038
|
+
util.isInteger = typeof Number.isInteger === "function"
|
|
1039
|
+
? (val) => Number.isInteger(val) // eslint-disable-line ban/ban
|
|
1040
|
+
: (val) => typeof val === "number" && Number.isFinite(val) && Math.floor(val) === val;
|
|
1041
|
+
function joinValues(array, separator = " | ") {
|
|
1042
|
+
return array.map((val) => (typeof val === "string" ? `'${val}'` : val)).join(separator);
|
|
1043
|
+
}
|
|
1044
|
+
util.joinValues = joinValues;
|
|
1045
|
+
util.jsonStringifyReplacer = (_, value) => {
|
|
1046
|
+
if (typeof value === "bigint") {
|
|
1047
|
+
return value.toString();
|
|
1048
|
+
}
|
|
1049
|
+
return value;
|
|
1050
|
+
};
|
|
1051
|
+
})(util || (util = {}));
|
|
1052
|
+
var objectUtil;
|
|
1053
|
+
(function (objectUtil) {
|
|
1054
|
+
objectUtil.mergeShapes = (first, second) => {
|
|
1055
|
+
return {
|
|
1056
|
+
...first,
|
|
1057
|
+
...second, // second overwrites first
|
|
1058
|
+
};
|
|
1059
|
+
};
|
|
1060
|
+
})(objectUtil || (objectUtil = {}));
|
|
1061
|
+
const ZodParsedType = util.arrayToEnum([
|
|
1062
|
+
"string",
|
|
1063
|
+
"nan",
|
|
1064
|
+
"number",
|
|
1065
|
+
"integer",
|
|
1066
|
+
"float",
|
|
1067
|
+
"boolean",
|
|
1068
|
+
"date",
|
|
1069
|
+
"bigint",
|
|
1070
|
+
"symbol",
|
|
1071
|
+
"function",
|
|
1072
|
+
"undefined",
|
|
1073
|
+
"null",
|
|
1074
|
+
"array",
|
|
1075
|
+
"object",
|
|
1076
|
+
"unknown",
|
|
1077
|
+
"promise",
|
|
1078
|
+
"void",
|
|
1079
|
+
"never",
|
|
1080
|
+
"map",
|
|
1081
|
+
"set",
|
|
1082
|
+
]);
|
|
1083
|
+
const getParsedType = (data) => {
|
|
1084
|
+
const t = typeof data;
|
|
1085
|
+
switch (t) {
|
|
1086
|
+
case "undefined":
|
|
1087
|
+
return ZodParsedType.undefined;
|
|
1088
|
+
case "string":
|
|
1089
|
+
return ZodParsedType.string;
|
|
1090
|
+
case "number":
|
|
1091
|
+
return Number.isNaN(data) ? ZodParsedType.nan : ZodParsedType.number;
|
|
1092
|
+
case "boolean":
|
|
1093
|
+
return ZodParsedType.boolean;
|
|
1094
|
+
case "function":
|
|
1095
|
+
return ZodParsedType.function;
|
|
1096
|
+
case "bigint":
|
|
1097
|
+
return ZodParsedType.bigint;
|
|
1098
|
+
case "symbol":
|
|
1099
|
+
return ZodParsedType.symbol;
|
|
1100
|
+
case "object":
|
|
1101
|
+
if (Array.isArray(data)) {
|
|
1102
|
+
return ZodParsedType.array;
|
|
1103
|
+
}
|
|
1104
|
+
if (data === null) {
|
|
1105
|
+
return ZodParsedType.null;
|
|
1106
|
+
}
|
|
1107
|
+
if (data.then && typeof data.then === "function" && data.catch && typeof data.catch === "function") {
|
|
1108
|
+
return ZodParsedType.promise;
|
|
1109
|
+
}
|
|
1110
|
+
if (typeof Map !== "undefined" && data instanceof Map) {
|
|
1111
|
+
return ZodParsedType.map;
|
|
1112
|
+
}
|
|
1113
|
+
if (typeof Set !== "undefined" && data instanceof Set) {
|
|
1114
|
+
return ZodParsedType.set;
|
|
1115
|
+
}
|
|
1116
|
+
if (typeof Date !== "undefined" && data instanceof Date) {
|
|
1117
|
+
return ZodParsedType.date;
|
|
1118
|
+
}
|
|
1119
|
+
return ZodParsedType.object;
|
|
1120
|
+
default:
|
|
1121
|
+
return ZodParsedType.unknown;
|
|
1122
|
+
}
|
|
1123
|
+
};
|
|
1124
|
+
|
|
1125
|
+
const ZodIssueCode = util.arrayToEnum([
|
|
1126
|
+
"invalid_type",
|
|
1127
|
+
"invalid_literal",
|
|
1128
|
+
"custom",
|
|
1129
|
+
"invalid_union",
|
|
1130
|
+
"invalid_union_discriminator",
|
|
1131
|
+
"invalid_enum_value",
|
|
1132
|
+
"unrecognized_keys",
|
|
1133
|
+
"invalid_arguments",
|
|
1134
|
+
"invalid_return_type",
|
|
1135
|
+
"invalid_date",
|
|
1136
|
+
"invalid_string",
|
|
1137
|
+
"too_small",
|
|
1138
|
+
"too_big",
|
|
1139
|
+
"invalid_intersection_types",
|
|
1140
|
+
"not_multiple_of",
|
|
1141
|
+
"not_finite",
|
|
1142
|
+
]);
|
|
1143
|
+
class ZodError extends Error {
|
|
1144
|
+
get errors() {
|
|
1145
|
+
return this.issues;
|
|
1146
|
+
}
|
|
1147
|
+
constructor(issues) {
|
|
1148
|
+
super();
|
|
1149
|
+
this.issues = [];
|
|
1150
|
+
this.addIssue = (sub) => {
|
|
1151
|
+
this.issues = [...this.issues, sub];
|
|
1152
|
+
};
|
|
1153
|
+
this.addIssues = (subs = []) => {
|
|
1154
|
+
this.issues = [...this.issues, ...subs];
|
|
1155
|
+
};
|
|
1156
|
+
const actualProto = new.target.prototype;
|
|
1157
|
+
if (Object.setPrototypeOf) {
|
|
1158
|
+
// eslint-disable-next-line ban/ban
|
|
1159
|
+
Object.setPrototypeOf(this, actualProto);
|
|
1160
|
+
}
|
|
1161
|
+
else {
|
|
1162
|
+
this.__proto__ = actualProto;
|
|
1163
|
+
}
|
|
1164
|
+
this.name = "ZodError";
|
|
1165
|
+
this.issues = issues;
|
|
1166
|
+
}
|
|
1167
|
+
format(_mapper) {
|
|
1168
|
+
const mapper = _mapper ||
|
|
1169
|
+
function (issue) {
|
|
1170
|
+
return issue.message;
|
|
1171
|
+
};
|
|
1172
|
+
const fieldErrors = { _errors: [] };
|
|
1173
|
+
const processError = (error) => {
|
|
1174
|
+
for (const issue of error.issues) {
|
|
1175
|
+
if (issue.code === "invalid_union") {
|
|
1176
|
+
issue.unionErrors.map(processError);
|
|
1177
|
+
}
|
|
1178
|
+
else if (issue.code === "invalid_return_type") {
|
|
1179
|
+
processError(issue.returnTypeError);
|
|
1180
|
+
}
|
|
1181
|
+
else if (issue.code === "invalid_arguments") {
|
|
1182
|
+
processError(issue.argumentsError);
|
|
1183
|
+
}
|
|
1184
|
+
else if (issue.path.length === 0) {
|
|
1185
|
+
fieldErrors._errors.push(mapper(issue));
|
|
1186
|
+
}
|
|
1187
|
+
else {
|
|
1188
|
+
let curr = fieldErrors;
|
|
1189
|
+
let i = 0;
|
|
1190
|
+
while (i < issue.path.length) {
|
|
1191
|
+
const el = issue.path[i];
|
|
1192
|
+
const terminal = i === issue.path.length - 1;
|
|
1193
|
+
if (!terminal) {
|
|
1194
|
+
curr[el] = curr[el] || { _errors: [] };
|
|
1195
|
+
// if (typeof el === "string") {
|
|
1196
|
+
// curr[el] = curr[el] || { _errors: [] };
|
|
1197
|
+
// } else if (typeof el === "number") {
|
|
1198
|
+
// const errorArray: any = [];
|
|
1199
|
+
// errorArray._errors = [];
|
|
1200
|
+
// curr[el] = curr[el] || errorArray;
|
|
1201
|
+
// }
|
|
1202
|
+
}
|
|
1203
|
+
else {
|
|
1204
|
+
curr[el] = curr[el] || { _errors: [] };
|
|
1205
|
+
curr[el]._errors.push(mapper(issue));
|
|
1206
|
+
}
|
|
1207
|
+
curr = curr[el];
|
|
1208
|
+
i++;
|
|
1209
|
+
}
|
|
1210
|
+
}
|
|
1211
|
+
}
|
|
1212
|
+
};
|
|
1213
|
+
processError(this);
|
|
1214
|
+
return fieldErrors;
|
|
1215
|
+
}
|
|
1216
|
+
static assert(value) {
|
|
1217
|
+
if (!(value instanceof ZodError)) {
|
|
1218
|
+
throw new Error(`Not a ZodError: ${value}`);
|
|
1219
|
+
}
|
|
1220
|
+
}
|
|
1221
|
+
toString() {
|
|
1222
|
+
return this.message;
|
|
1223
|
+
}
|
|
1224
|
+
get message() {
|
|
1225
|
+
return JSON.stringify(this.issues, util.jsonStringifyReplacer, 2);
|
|
1226
|
+
}
|
|
1227
|
+
get isEmpty() {
|
|
1228
|
+
return this.issues.length === 0;
|
|
1229
|
+
}
|
|
1230
|
+
flatten(mapper = (issue) => issue.message) {
|
|
1231
|
+
const fieldErrors = {};
|
|
1232
|
+
const formErrors = [];
|
|
1233
|
+
for (const sub of this.issues) {
|
|
1234
|
+
if (sub.path.length > 0) {
|
|
1235
|
+
const firstEl = sub.path[0];
|
|
1236
|
+
fieldErrors[firstEl] = fieldErrors[firstEl] || [];
|
|
1237
|
+
fieldErrors[firstEl].push(mapper(sub));
|
|
1238
|
+
}
|
|
1239
|
+
else {
|
|
1240
|
+
formErrors.push(mapper(sub));
|
|
1241
|
+
}
|
|
1242
|
+
}
|
|
1243
|
+
return { formErrors, fieldErrors };
|
|
1244
|
+
}
|
|
1245
|
+
get formErrors() {
|
|
1246
|
+
return this.flatten();
|
|
1247
|
+
}
|
|
1248
|
+
}
|
|
1249
|
+
ZodError.create = (issues) => {
|
|
1250
|
+
const error = new ZodError(issues);
|
|
1251
|
+
return error;
|
|
1252
|
+
};
|
|
1253
|
+
|
|
1254
|
+
const errorMap = (issue, _ctx) => {
|
|
1255
|
+
let message;
|
|
1256
|
+
switch (issue.code) {
|
|
1257
|
+
case ZodIssueCode.invalid_type:
|
|
1258
|
+
if (issue.received === ZodParsedType.undefined) {
|
|
1259
|
+
message = "Required";
|
|
1260
|
+
}
|
|
1261
|
+
else {
|
|
1262
|
+
message = `Expected ${issue.expected}, received ${issue.received}`;
|
|
1263
|
+
}
|
|
1264
|
+
break;
|
|
1265
|
+
case ZodIssueCode.invalid_literal:
|
|
1266
|
+
message = `Invalid literal value, expected ${JSON.stringify(issue.expected, util.jsonStringifyReplacer)}`;
|
|
1267
|
+
break;
|
|
1268
|
+
case ZodIssueCode.unrecognized_keys:
|
|
1269
|
+
message = `Unrecognized key(s) in object: ${util.joinValues(issue.keys, ", ")}`;
|
|
1270
|
+
break;
|
|
1271
|
+
case ZodIssueCode.invalid_union:
|
|
1272
|
+
message = `Invalid input`;
|
|
1273
|
+
break;
|
|
1274
|
+
case ZodIssueCode.invalid_union_discriminator:
|
|
1275
|
+
message = `Invalid discriminator value. Expected ${util.joinValues(issue.options)}`;
|
|
1276
|
+
break;
|
|
1277
|
+
case ZodIssueCode.invalid_enum_value:
|
|
1278
|
+
message = `Invalid enum value. Expected ${util.joinValues(issue.options)}, received '${issue.received}'`;
|
|
1279
|
+
break;
|
|
1280
|
+
case ZodIssueCode.invalid_arguments:
|
|
1281
|
+
message = `Invalid function arguments`;
|
|
1282
|
+
break;
|
|
1283
|
+
case ZodIssueCode.invalid_return_type:
|
|
1284
|
+
message = `Invalid function return type`;
|
|
1285
|
+
break;
|
|
1286
|
+
case ZodIssueCode.invalid_date:
|
|
1287
|
+
message = `Invalid date`;
|
|
1288
|
+
break;
|
|
1289
|
+
case ZodIssueCode.invalid_string:
|
|
1290
|
+
if (typeof issue.validation === "object") {
|
|
1291
|
+
if ("includes" in issue.validation) {
|
|
1292
|
+
message = `Invalid input: must include "${issue.validation.includes}"`;
|
|
1293
|
+
if (typeof issue.validation.position === "number") {
|
|
1294
|
+
message = `${message} at one or more positions greater than or equal to ${issue.validation.position}`;
|
|
1295
|
+
}
|
|
1296
|
+
}
|
|
1297
|
+
else if ("startsWith" in issue.validation) {
|
|
1298
|
+
message = `Invalid input: must start with "${issue.validation.startsWith}"`;
|
|
1299
|
+
}
|
|
1300
|
+
else if ("endsWith" in issue.validation) {
|
|
1301
|
+
message = `Invalid input: must end with "${issue.validation.endsWith}"`;
|
|
1302
|
+
}
|
|
1303
|
+
else {
|
|
1304
|
+
util.assertNever(issue.validation);
|
|
1305
|
+
}
|
|
1306
|
+
}
|
|
1307
|
+
else if (issue.validation !== "regex") {
|
|
1308
|
+
message = `Invalid ${issue.validation}`;
|
|
1309
|
+
}
|
|
1310
|
+
else {
|
|
1311
|
+
message = "Invalid";
|
|
1312
|
+
}
|
|
1313
|
+
break;
|
|
1314
|
+
case ZodIssueCode.too_small:
|
|
1315
|
+
if (issue.type === "array")
|
|
1316
|
+
message = `Array must contain ${issue.exact ? "exactly" : issue.inclusive ? `at least` : `more than`} ${issue.minimum} element(s)`;
|
|
1317
|
+
else if (issue.type === "string")
|
|
1318
|
+
message = `String must contain ${issue.exact ? "exactly" : issue.inclusive ? `at least` : `over`} ${issue.minimum} character(s)`;
|
|
1319
|
+
else if (issue.type === "number")
|
|
1320
|
+
message = `Number must be ${issue.exact ? `exactly equal to ` : issue.inclusive ? `greater than or equal to ` : `greater than `}${issue.minimum}`;
|
|
1321
|
+
else if (issue.type === "bigint")
|
|
1322
|
+
message = `Number must be ${issue.exact ? `exactly equal to ` : issue.inclusive ? `greater than or equal to ` : `greater than `}${issue.minimum}`;
|
|
1323
|
+
else if (issue.type === "date")
|
|
1324
|
+
message = `Date must be ${issue.exact ? `exactly equal to ` : issue.inclusive ? `greater than or equal to ` : `greater than `}${new Date(Number(issue.minimum))}`;
|
|
1325
|
+
else
|
|
1326
|
+
message = "Invalid input";
|
|
1327
|
+
break;
|
|
1328
|
+
case ZodIssueCode.too_big:
|
|
1329
|
+
if (issue.type === "array")
|
|
1330
|
+
message = `Array must contain ${issue.exact ? `exactly` : issue.inclusive ? `at most` : `less than`} ${issue.maximum} element(s)`;
|
|
1331
|
+
else if (issue.type === "string")
|
|
1332
|
+
message = `String must contain ${issue.exact ? `exactly` : issue.inclusive ? `at most` : `under`} ${issue.maximum} character(s)`;
|
|
1333
|
+
else if (issue.type === "number")
|
|
1334
|
+
message = `Number must be ${issue.exact ? `exactly` : issue.inclusive ? `less than or equal to` : `less than`} ${issue.maximum}`;
|
|
1335
|
+
else if (issue.type === "bigint")
|
|
1336
|
+
message = `BigInt must be ${issue.exact ? `exactly` : issue.inclusive ? `less than or equal to` : `less than`} ${issue.maximum}`;
|
|
1337
|
+
else if (issue.type === "date")
|
|
1338
|
+
message = `Date must be ${issue.exact ? `exactly` : issue.inclusive ? `smaller than or equal to` : `smaller than`} ${new Date(Number(issue.maximum))}`;
|
|
1339
|
+
else
|
|
1340
|
+
message = "Invalid input";
|
|
1341
|
+
break;
|
|
1342
|
+
case ZodIssueCode.custom:
|
|
1343
|
+
message = `Invalid input`;
|
|
1344
|
+
break;
|
|
1345
|
+
case ZodIssueCode.invalid_intersection_types:
|
|
1346
|
+
message = `Intersection results could not be merged`;
|
|
1347
|
+
break;
|
|
1348
|
+
case ZodIssueCode.not_multiple_of:
|
|
1349
|
+
message = `Number must be a multiple of ${issue.multipleOf}`;
|
|
1350
|
+
break;
|
|
1351
|
+
case ZodIssueCode.not_finite:
|
|
1352
|
+
message = "Number must be finite";
|
|
1353
|
+
break;
|
|
1354
|
+
default:
|
|
1355
|
+
message = _ctx.defaultError;
|
|
1356
|
+
util.assertNever(issue);
|
|
1357
|
+
}
|
|
1358
|
+
return { message };
|
|
1359
|
+
};
|
|
1360
|
+
|
|
1361
|
+
let overrideErrorMap = errorMap;
|
|
1362
|
+
function getErrorMap() {
|
|
1363
|
+
return overrideErrorMap;
|
|
1364
|
+
}
|
|
1365
|
+
|
|
1366
|
+
const makeIssue = (params) => {
|
|
1367
|
+
const { data, path, errorMaps, issueData } = params;
|
|
1368
|
+
const fullPath = [...path, ...(issueData.path || [])];
|
|
1369
|
+
const fullIssue = {
|
|
1370
|
+
...issueData,
|
|
1371
|
+
path: fullPath,
|
|
1372
|
+
};
|
|
1373
|
+
if (issueData.message !== undefined) {
|
|
1374
|
+
return {
|
|
1375
|
+
...issueData,
|
|
1376
|
+
path: fullPath,
|
|
1377
|
+
message: issueData.message,
|
|
1378
|
+
};
|
|
1379
|
+
}
|
|
1380
|
+
let errorMessage = "";
|
|
1381
|
+
const maps = errorMaps
|
|
1382
|
+
.filter((m) => !!m)
|
|
1383
|
+
.slice()
|
|
1384
|
+
.reverse();
|
|
1385
|
+
for (const map of maps) {
|
|
1386
|
+
errorMessage = map(fullIssue, { data, defaultError: errorMessage }).message;
|
|
1387
|
+
}
|
|
1388
|
+
return {
|
|
1389
|
+
...issueData,
|
|
1390
|
+
path: fullPath,
|
|
1391
|
+
message: errorMessage,
|
|
1392
|
+
};
|
|
1393
|
+
};
|
|
1394
|
+
function addIssueToContext(ctx, issueData) {
|
|
1395
|
+
const overrideMap = getErrorMap();
|
|
1396
|
+
const issue = makeIssue({
|
|
1397
|
+
issueData: issueData,
|
|
1398
|
+
data: ctx.data,
|
|
1399
|
+
path: ctx.path,
|
|
1400
|
+
errorMaps: [
|
|
1401
|
+
ctx.common.contextualErrorMap, // contextual error map is first priority
|
|
1402
|
+
ctx.schemaErrorMap, // then schema-bound map if available
|
|
1403
|
+
overrideMap, // then global override map
|
|
1404
|
+
overrideMap === errorMap ? undefined : errorMap, // then global default map
|
|
1405
|
+
].filter((x) => !!x),
|
|
1406
|
+
});
|
|
1407
|
+
ctx.common.issues.push(issue);
|
|
1408
|
+
}
|
|
1409
|
+
class ParseStatus {
|
|
1410
|
+
constructor() {
|
|
1411
|
+
this.value = "valid";
|
|
1412
|
+
}
|
|
1413
|
+
dirty() {
|
|
1414
|
+
if (this.value === "valid")
|
|
1415
|
+
this.value = "dirty";
|
|
1416
|
+
}
|
|
1417
|
+
abort() {
|
|
1418
|
+
if (this.value !== "aborted")
|
|
1419
|
+
this.value = "aborted";
|
|
1420
|
+
}
|
|
1421
|
+
static mergeArray(status, results) {
|
|
1422
|
+
const arrayValue = [];
|
|
1423
|
+
for (const s of results) {
|
|
1424
|
+
if (s.status === "aborted")
|
|
1425
|
+
return INVALID;
|
|
1426
|
+
if (s.status === "dirty")
|
|
1427
|
+
status.dirty();
|
|
1428
|
+
arrayValue.push(s.value);
|
|
1429
|
+
}
|
|
1430
|
+
return { status: status.value, value: arrayValue };
|
|
1431
|
+
}
|
|
1432
|
+
static async mergeObjectAsync(status, pairs) {
|
|
1433
|
+
const syncPairs = [];
|
|
1434
|
+
for (const pair of pairs) {
|
|
1435
|
+
const key = await pair.key;
|
|
1436
|
+
const value = await pair.value;
|
|
1437
|
+
syncPairs.push({
|
|
1438
|
+
key,
|
|
1439
|
+
value,
|
|
1440
|
+
});
|
|
1441
|
+
}
|
|
1442
|
+
return ParseStatus.mergeObjectSync(status, syncPairs);
|
|
1443
|
+
}
|
|
1444
|
+
static mergeObjectSync(status, pairs) {
|
|
1445
|
+
const finalObject = {};
|
|
1446
|
+
for (const pair of pairs) {
|
|
1447
|
+
const { key, value } = pair;
|
|
1448
|
+
if (key.status === "aborted")
|
|
1449
|
+
return INVALID;
|
|
1450
|
+
if (value.status === "aborted")
|
|
1451
|
+
return INVALID;
|
|
1452
|
+
if (key.status === "dirty")
|
|
1453
|
+
status.dirty();
|
|
1454
|
+
if (value.status === "dirty")
|
|
1455
|
+
status.dirty();
|
|
1456
|
+
if (key.value !== "__proto__" && (typeof value.value !== "undefined" || pair.alwaysSet)) {
|
|
1457
|
+
finalObject[key.value] = value.value;
|
|
1458
|
+
}
|
|
1459
|
+
}
|
|
1460
|
+
return { status: status.value, value: finalObject };
|
|
1461
|
+
}
|
|
1462
|
+
}
|
|
1463
|
+
const INVALID = Object.freeze({
|
|
1464
|
+
status: "aborted",
|
|
1465
|
+
});
|
|
1466
|
+
const DIRTY = (value) => ({ status: "dirty", value });
|
|
1467
|
+
const OK = (value) => ({ status: "valid", value });
|
|
1468
|
+
const isAborted = (x) => x.status === "aborted";
|
|
1469
|
+
const isDirty = (x) => x.status === "dirty";
|
|
1470
|
+
const isValid = (x) => x.status === "valid";
|
|
1471
|
+
const isAsync = (x) => typeof Promise !== "undefined" && x instanceof Promise;
|
|
1472
|
+
|
|
1473
|
+
var errorUtil;
|
|
1474
|
+
(function (errorUtil) {
|
|
1475
|
+
errorUtil.errToObj = (message) => typeof message === "string" ? { message } : message || {};
|
|
1476
|
+
// biome-ignore lint:
|
|
1477
|
+
errorUtil.toString = (message) => typeof message === "string" ? message : message?.message;
|
|
1478
|
+
})(errorUtil || (errorUtil = {}));
|
|
1479
|
+
|
|
1480
|
+
class ParseInputLazyPath {
|
|
1481
|
+
constructor(parent, value, path, key) {
|
|
1482
|
+
this._cachedPath = [];
|
|
1483
|
+
this.parent = parent;
|
|
1484
|
+
this.data = value;
|
|
1485
|
+
this._path = path;
|
|
1486
|
+
this._key = key;
|
|
1487
|
+
}
|
|
1488
|
+
get path() {
|
|
1489
|
+
if (!this._cachedPath.length) {
|
|
1490
|
+
if (Array.isArray(this._key)) {
|
|
1491
|
+
this._cachedPath.push(...this._path, ...this._key);
|
|
1492
|
+
}
|
|
1493
|
+
else {
|
|
1494
|
+
this._cachedPath.push(...this._path, this._key);
|
|
1495
|
+
}
|
|
1496
|
+
}
|
|
1497
|
+
return this._cachedPath;
|
|
1498
|
+
}
|
|
1499
|
+
}
|
|
1500
|
+
const handleResult = (ctx, result) => {
|
|
1501
|
+
if (isValid(result)) {
|
|
1502
|
+
return { success: true, data: result.value };
|
|
1503
|
+
}
|
|
1504
|
+
else {
|
|
1505
|
+
if (!ctx.common.issues.length) {
|
|
1506
|
+
throw new Error("Validation failed but no issues detected.");
|
|
1507
|
+
}
|
|
1508
|
+
return {
|
|
1509
|
+
success: false,
|
|
1510
|
+
get error() {
|
|
1511
|
+
if (this._error)
|
|
1512
|
+
return this._error;
|
|
1513
|
+
const error = new ZodError(ctx.common.issues);
|
|
1514
|
+
this._error = error;
|
|
1515
|
+
return this._error;
|
|
1516
|
+
},
|
|
1517
|
+
};
|
|
1518
|
+
}
|
|
1519
|
+
};
|
|
1520
|
+
function processCreateParams(params) {
|
|
1521
|
+
if (!params)
|
|
1522
|
+
return {};
|
|
1523
|
+
const { errorMap, invalid_type_error, required_error, description } = params;
|
|
1524
|
+
if (errorMap && (invalid_type_error || required_error)) {
|
|
1525
|
+
throw new Error(`Can't use "invalid_type_error" or "required_error" in conjunction with custom error map.`);
|
|
1526
|
+
}
|
|
1527
|
+
if (errorMap)
|
|
1528
|
+
return { errorMap: errorMap, description };
|
|
1529
|
+
const customMap = (iss, ctx) => {
|
|
1530
|
+
const { message } = params;
|
|
1531
|
+
if (iss.code === "invalid_enum_value") {
|
|
1532
|
+
return { message: message ?? ctx.defaultError };
|
|
1533
|
+
}
|
|
1534
|
+
if (typeof ctx.data === "undefined") {
|
|
1535
|
+
return { message: message ?? required_error ?? ctx.defaultError };
|
|
1536
|
+
}
|
|
1537
|
+
if (iss.code !== "invalid_type")
|
|
1538
|
+
return { message: ctx.defaultError };
|
|
1539
|
+
return { message: message ?? invalid_type_error ?? ctx.defaultError };
|
|
1540
|
+
};
|
|
1541
|
+
return { errorMap: customMap, description };
|
|
1542
|
+
}
|
|
1543
|
+
class ZodType {
|
|
1544
|
+
get description() {
|
|
1545
|
+
return this._def.description;
|
|
1546
|
+
}
|
|
1547
|
+
_getType(input) {
|
|
1548
|
+
return getParsedType(input.data);
|
|
1549
|
+
}
|
|
1550
|
+
_getOrReturnCtx(input, ctx) {
|
|
1551
|
+
return (ctx || {
|
|
1552
|
+
common: input.parent.common,
|
|
1553
|
+
data: input.data,
|
|
1554
|
+
parsedType: getParsedType(input.data),
|
|
1555
|
+
schemaErrorMap: this._def.errorMap,
|
|
1556
|
+
path: input.path,
|
|
1557
|
+
parent: input.parent,
|
|
1558
|
+
});
|
|
1559
|
+
}
|
|
1560
|
+
_processInputParams(input) {
|
|
1561
|
+
return {
|
|
1562
|
+
status: new ParseStatus(),
|
|
1563
|
+
ctx: {
|
|
1564
|
+
common: input.parent.common,
|
|
1565
|
+
data: input.data,
|
|
1566
|
+
parsedType: getParsedType(input.data),
|
|
1567
|
+
schemaErrorMap: this._def.errorMap,
|
|
1568
|
+
path: input.path,
|
|
1569
|
+
parent: input.parent,
|
|
1570
|
+
},
|
|
1571
|
+
};
|
|
1572
|
+
}
|
|
1573
|
+
_parseSync(input) {
|
|
1574
|
+
const result = this._parse(input);
|
|
1575
|
+
if (isAsync(result)) {
|
|
1576
|
+
throw new Error("Synchronous parse encountered promise.");
|
|
1577
|
+
}
|
|
1578
|
+
return result;
|
|
1579
|
+
}
|
|
1580
|
+
_parseAsync(input) {
|
|
1581
|
+
const result = this._parse(input);
|
|
1582
|
+
return Promise.resolve(result);
|
|
1583
|
+
}
|
|
1584
|
+
parse(data, params) {
|
|
1585
|
+
const result = this.safeParse(data, params);
|
|
1586
|
+
if (result.success)
|
|
1587
|
+
return result.data;
|
|
1588
|
+
throw result.error;
|
|
1589
|
+
}
|
|
1590
|
+
safeParse(data, params) {
|
|
1591
|
+
const ctx = {
|
|
1592
|
+
common: {
|
|
1593
|
+
issues: [],
|
|
1594
|
+
async: params?.async ?? false,
|
|
1595
|
+
contextualErrorMap: params?.errorMap,
|
|
1596
|
+
},
|
|
1597
|
+
path: params?.path || [],
|
|
1598
|
+
schemaErrorMap: this._def.errorMap,
|
|
1599
|
+
parent: null,
|
|
1600
|
+
data,
|
|
1601
|
+
parsedType: getParsedType(data),
|
|
1602
|
+
};
|
|
1603
|
+
const result = this._parseSync({ data, path: ctx.path, parent: ctx });
|
|
1604
|
+
return handleResult(ctx, result);
|
|
1605
|
+
}
|
|
1606
|
+
"~validate"(data) {
|
|
1607
|
+
const ctx = {
|
|
1608
|
+
common: {
|
|
1609
|
+
issues: [],
|
|
1610
|
+
async: !!this["~standard"].async,
|
|
1611
|
+
},
|
|
1612
|
+
path: [],
|
|
1613
|
+
schemaErrorMap: this._def.errorMap,
|
|
1614
|
+
parent: null,
|
|
1615
|
+
data,
|
|
1616
|
+
parsedType: getParsedType(data),
|
|
1617
|
+
};
|
|
1618
|
+
if (!this["~standard"].async) {
|
|
1619
|
+
try {
|
|
1620
|
+
const result = this._parseSync({ data, path: [], parent: ctx });
|
|
1621
|
+
return isValid(result)
|
|
1622
|
+
? {
|
|
1623
|
+
value: result.value,
|
|
1624
|
+
}
|
|
1625
|
+
: {
|
|
1626
|
+
issues: ctx.common.issues,
|
|
1627
|
+
};
|
|
1628
|
+
}
|
|
1629
|
+
catch (err) {
|
|
1630
|
+
if (err?.message?.toLowerCase()?.includes("encountered")) {
|
|
1631
|
+
this["~standard"].async = true;
|
|
1632
|
+
}
|
|
1633
|
+
ctx.common = {
|
|
1634
|
+
issues: [],
|
|
1635
|
+
async: true,
|
|
1636
|
+
};
|
|
1637
|
+
}
|
|
1638
|
+
}
|
|
1639
|
+
return this._parseAsync({ data, path: [], parent: ctx }).then((result) => isValid(result)
|
|
1640
|
+
? {
|
|
1641
|
+
value: result.value,
|
|
1642
|
+
}
|
|
1643
|
+
: {
|
|
1644
|
+
issues: ctx.common.issues,
|
|
1645
|
+
});
|
|
1646
|
+
}
|
|
1647
|
+
async parseAsync(data, params) {
|
|
1648
|
+
const result = await this.safeParseAsync(data, params);
|
|
1649
|
+
if (result.success)
|
|
1650
|
+
return result.data;
|
|
1651
|
+
throw result.error;
|
|
1652
|
+
}
|
|
1653
|
+
async safeParseAsync(data, params) {
|
|
1654
|
+
const ctx = {
|
|
1655
|
+
common: {
|
|
1656
|
+
issues: [],
|
|
1657
|
+
contextualErrorMap: params?.errorMap,
|
|
1658
|
+
async: true,
|
|
1659
|
+
},
|
|
1660
|
+
path: params?.path || [],
|
|
1661
|
+
schemaErrorMap: this._def.errorMap,
|
|
1662
|
+
parent: null,
|
|
1663
|
+
data,
|
|
1664
|
+
parsedType: getParsedType(data),
|
|
1665
|
+
};
|
|
1666
|
+
const maybeAsyncResult = this._parse({ data, path: ctx.path, parent: ctx });
|
|
1667
|
+
const result = await (isAsync(maybeAsyncResult) ? maybeAsyncResult : Promise.resolve(maybeAsyncResult));
|
|
1668
|
+
return handleResult(ctx, result);
|
|
1669
|
+
}
|
|
1670
|
+
refine(check, message) {
|
|
1671
|
+
const getIssueProperties = (val) => {
|
|
1672
|
+
if (typeof message === "string" || typeof message === "undefined") {
|
|
1673
|
+
return { message };
|
|
1674
|
+
}
|
|
1675
|
+
else if (typeof message === "function") {
|
|
1676
|
+
return message(val);
|
|
1677
|
+
}
|
|
1678
|
+
else {
|
|
1679
|
+
return message;
|
|
1680
|
+
}
|
|
1681
|
+
};
|
|
1682
|
+
return this._refinement((val, ctx) => {
|
|
1683
|
+
const result = check(val);
|
|
1684
|
+
const setError = () => ctx.addIssue({
|
|
1685
|
+
code: ZodIssueCode.custom,
|
|
1686
|
+
...getIssueProperties(val),
|
|
1687
|
+
});
|
|
1688
|
+
if (typeof Promise !== "undefined" && result instanceof Promise) {
|
|
1689
|
+
return result.then((data) => {
|
|
1690
|
+
if (!data) {
|
|
1691
|
+
setError();
|
|
1692
|
+
return false;
|
|
1693
|
+
}
|
|
1694
|
+
else {
|
|
1695
|
+
return true;
|
|
1696
|
+
}
|
|
1697
|
+
});
|
|
1698
|
+
}
|
|
1699
|
+
if (!result) {
|
|
1700
|
+
setError();
|
|
1701
|
+
return false;
|
|
1702
|
+
}
|
|
1703
|
+
else {
|
|
1704
|
+
return true;
|
|
1705
|
+
}
|
|
1706
|
+
});
|
|
1707
|
+
}
|
|
1708
|
+
refinement(check, refinementData) {
|
|
1709
|
+
return this._refinement((val, ctx) => {
|
|
1710
|
+
if (!check(val)) {
|
|
1711
|
+
ctx.addIssue(typeof refinementData === "function" ? refinementData(val, ctx) : refinementData);
|
|
1712
|
+
return false;
|
|
1713
|
+
}
|
|
1714
|
+
else {
|
|
1715
|
+
return true;
|
|
1716
|
+
}
|
|
1717
|
+
});
|
|
1718
|
+
}
|
|
1719
|
+
_refinement(refinement) {
|
|
1720
|
+
return new ZodEffects({
|
|
1721
|
+
schema: this,
|
|
1722
|
+
typeName: ZodFirstPartyTypeKind.ZodEffects,
|
|
1723
|
+
effect: { type: "refinement", refinement },
|
|
1724
|
+
});
|
|
1725
|
+
}
|
|
1726
|
+
superRefine(refinement) {
|
|
1727
|
+
return this._refinement(refinement);
|
|
1728
|
+
}
|
|
1729
|
+
constructor(def) {
|
|
1730
|
+
/** Alias of safeParseAsync */
|
|
1731
|
+
this.spa = this.safeParseAsync;
|
|
1732
|
+
this._def = def;
|
|
1733
|
+
this.parse = this.parse.bind(this);
|
|
1734
|
+
this.safeParse = this.safeParse.bind(this);
|
|
1735
|
+
this.parseAsync = this.parseAsync.bind(this);
|
|
1736
|
+
this.safeParseAsync = this.safeParseAsync.bind(this);
|
|
1737
|
+
this.spa = this.spa.bind(this);
|
|
1738
|
+
this.refine = this.refine.bind(this);
|
|
1739
|
+
this.refinement = this.refinement.bind(this);
|
|
1740
|
+
this.superRefine = this.superRefine.bind(this);
|
|
1741
|
+
this.optional = this.optional.bind(this);
|
|
1742
|
+
this.nullable = this.nullable.bind(this);
|
|
1743
|
+
this.nullish = this.nullish.bind(this);
|
|
1744
|
+
this.array = this.array.bind(this);
|
|
1745
|
+
this.promise = this.promise.bind(this);
|
|
1746
|
+
this.or = this.or.bind(this);
|
|
1747
|
+
this.and = this.and.bind(this);
|
|
1748
|
+
this.transform = this.transform.bind(this);
|
|
1749
|
+
this.brand = this.brand.bind(this);
|
|
1750
|
+
this.default = this.default.bind(this);
|
|
1751
|
+
this.catch = this.catch.bind(this);
|
|
1752
|
+
this.describe = this.describe.bind(this);
|
|
1753
|
+
this.pipe = this.pipe.bind(this);
|
|
1754
|
+
this.readonly = this.readonly.bind(this);
|
|
1755
|
+
this.isNullable = this.isNullable.bind(this);
|
|
1756
|
+
this.isOptional = this.isOptional.bind(this);
|
|
1757
|
+
this["~standard"] = {
|
|
1758
|
+
version: 1,
|
|
1759
|
+
vendor: "zod",
|
|
1760
|
+
validate: (data) => this["~validate"](data),
|
|
1761
|
+
};
|
|
1762
|
+
}
|
|
1763
|
+
optional() {
|
|
1764
|
+
return ZodOptional.create(this, this._def);
|
|
1765
|
+
}
|
|
1766
|
+
nullable() {
|
|
1767
|
+
return ZodNullable.create(this, this._def);
|
|
1768
|
+
}
|
|
1769
|
+
nullish() {
|
|
1770
|
+
return this.nullable().optional();
|
|
1771
|
+
}
|
|
1772
|
+
array() {
|
|
1773
|
+
return ZodArray.create(this);
|
|
1774
|
+
}
|
|
1775
|
+
promise() {
|
|
1776
|
+
return ZodPromise.create(this, this._def);
|
|
1777
|
+
}
|
|
1778
|
+
or(option) {
|
|
1779
|
+
return ZodUnion.create([this, option], this._def);
|
|
1780
|
+
}
|
|
1781
|
+
and(incoming) {
|
|
1782
|
+
return ZodIntersection.create(this, incoming, this._def);
|
|
1783
|
+
}
|
|
1784
|
+
transform(transform) {
|
|
1785
|
+
return new ZodEffects({
|
|
1786
|
+
...processCreateParams(this._def),
|
|
1787
|
+
schema: this,
|
|
1788
|
+
typeName: ZodFirstPartyTypeKind.ZodEffects,
|
|
1789
|
+
effect: { type: "transform", transform },
|
|
1790
|
+
});
|
|
1791
|
+
}
|
|
1792
|
+
default(def) {
|
|
1793
|
+
const defaultValueFunc = typeof def === "function" ? def : () => def;
|
|
1794
|
+
return new ZodDefault({
|
|
1795
|
+
...processCreateParams(this._def),
|
|
1796
|
+
innerType: this,
|
|
1797
|
+
defaultValue: defaultValueFunc,
|
|
1798
|
+
typeName: ZodFirstPartyTypeKind.ZodDefault,
|
|
1799
|
+
});
|
|
1800
|
+
}
|
|
1801
|
+
brand() {
|
|
1802
|
+
return new ZodBranded({
|
|
1803
|
+
typeName: ZodFirstPartyTypeKind.ZodBranded,
|
|
1804
|
+
type: this,
|
|
1805
|
+
...processCreateParams(this._def),
|
|
1806
|
+
});
|
|
1807
|
+
}
|
|
1808
|
+
catch(def) {
|
|
1809
|
+
const catchValueFunc = typeof def === "function" ? def : () => def;
|
|
1810
|
+
return new ZodCatch({
|
|
1811
|
+
...processCreateParams(this._def),
|
|
1812
|
+
innerType: this,
|
|
1813
|
+
catchValue: catchValueFunc,
|
|
1814
|
+
typeName: ZodFirstPartyTypeKind.ZodCatch,
|
|
1815
|
+
});
|
|
1816
|
+
}
|
|
1817
|
+
describe(description) {
|
|
1818
|
+
const This = this.constructor;
|
|
1819
|
+
return new This({
|
|
1820
|
+
...this._def,
|
|
1821
|
+
description,
|
|
1822
|
+
});
|
|
1823
|
+
}
|
|
1824
|
+
pipe(target) {
|
|
1825
|
+
return ZodPipeline.create(this, target);
|
|
1826
|
+
}
|
|
1827
|
+
readonly() {
|
|
1828
|
+
return ZodReadonly.create(this);
|
|
1829
|
+
}
|
|
1830
|
+
isOptional() {
|
|
1831
|
+
return this.safeParse(undefined).success;
|
|
1832
|
+
}
|
|
1833
|
+
isNullable() {
|
|
1834
|
+
return this.safeParse(null).success;
|
|
1835
|
+
}
|
|
1836
|
+
}
|
|
1837
|
+
const cuidRegex = /^c[^\s-]{8,}$/i;
|
|
1838
|
+
const cuid2Regex = /^[0-9a-z]+$/;
|
|
1839
|
+
const ulidRegex = /^[0-9A-HJKMNP-TV-Z]{26}$/i;
|
|
1840
|
+
// const uuidRegex =
|
|
1841
|
+
// /^([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;
|
|
1842
|
+
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;
|
|
1843
|
+
const nanoidRegex = /^[a-z0-9_-]{21}$/i;
|
|
1844
|
+
const jwtRegex = /^[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+\.[A-Za-z0-9-_]*$/;
|
|
1845
|
+
const durationRegex = /^[-+]?P(?!$)(?:(?:[-+]?\d+Y)|(?:[-+]?\d+[.,]\d+Y$))?(?:(?:[-+]?\d+M)|(?:[-+]?\d+[.,]\d+M$))?(?:(?:[-+]?\d+W)|(?:[-+]?\d+[.,]\d+W$))?(?:(?:[-+]?\d+D)|(?:[-+]?\d+[.,]\d+D$))?(?:T(?=[\d+-])(?:(?:[-+]?\d+H)|(?:[-+]?\d+[.,]\d+H$))?(?:(?:[-+]?\d+M)|(?:[-+]?\d+[.,]\d+M$))?(?:[-+]?\d+(?:[.,]\d+)?S)?)??$/;
|
|
1846
|
+
// from https://stackoverflow.com/a/46181/1550155
|
|
1847
|
+
// old version: too slow, didn't support unicode
|
|
1848
|
+
// 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;
|
|
1849
|
+
//old email regex
|
|
1850
|
+
// const emailRegex = /^(([^<>()[\].,;:\s@"]+(\.[^<>()[\].,;:\s@"]+)*)|(".+"))@((?!-)([^<>()[\].,;:\s@"]+\.)+[^<>()[\].,;:\s@"]{1,})[^-<>()[\].,;:\s@"]$/i;
|
|
1851
|
+
// eslint-disable-next-line
|
|
1852
|
+
// const emailRegex =
|
|
1853
|
+
// /^(([^<>()[\]\\.,;:\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,})+))$/;
|
|
1854
|
+
// const emailRegex =
|
|
1855
|
+
// /^[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])?)*$/;
|
|
1856
|
+
// const emailRegex =
|
|
1857
|
+
// /^(?:[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;
|
|
1858
|
+
const emailRegex = /^(?!\.)(?!.*\.\.)([A-Z0-9_'+\-\.]*)[A-Z0-9_+-]@([A-Z0-9][A-Z0-9\-]*\.)+[A-Z]{2,}$/i;
|
|
1859
|
+
// const emailRegex =
|
|
1860
|
+
// /^[a-z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-z0-9-]+(?:\.[a-z0-9\-]+)*$/i;
|
|
1861
|
+
// from https://thekevinscott.com/emojis-in-javascript/#writing-a-regular-expression
|
|
1862
|
+
const _emojiRegex = `^(\\p{Extended_Pictographic}|\\p{Emoji_Component})+$`;
|
|
1863
|
+
let emojiRegex;
|
|
1864
|
+
// faster, simpler, safer
|
|
1865
|
+
const ipv4Regex = /^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$/;
|
|
1866
|
+
const ipv4CidrRegex = /^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\/(3[0-2]|[12]?[0-9])$/;
|
|
1867
|
+
// const ipv6Regex =
|
|
1868
|
+
// /^(([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})))$/;
|
|
1869
|
+
const ipv6Regex = /^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$/;
|
|
1870
|
+
const ipv6CidrRegex = /^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))\/(12[0-8]|1[01][0-9]|[1-9]?[0-9])$/;
|
|
1871
|
+
// https://stackoverflow.com/questions/7860392/determine-if-string-is-in-base64-using-javascript
|
|
1872
|
+
const base64Regex = /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/;
|
|
1873
|
+
// https://base64.guru/standards/base64url
|
|
1874
|
+
const base64urlRegex = /^([0-9a-zA-Z-_]{4})*(([0-9a-zA-Z-_]{2}(==)?)|([0-9a-zA-Z-_]{3}(=)?))?$/;
|
|
1875
|
+
// simple
|
|
1876
|
+
// const dateRegexSource = `\\d{4}-\\d{2}-\\d{2}`;
|
|
1877
|
+
// no leap year validation
|
|
1878
|
+
// const dateRegexSource = `\\d{4}-((0[13578]|10|12)-31|(0[13-9]|1[0-2])-30|(0[1-9]|1[0-2])-(0[1-9]|1\\d|2\\d))`;
|
|
1879
|
+
// with leap year validation
|
|
1880
|
+
const dateRegexSource = `((\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-((0[13578]|1[02])-(0[1-9]|[12]\\d|3[01])|(0[469]|11)-(0[1-9]|[12]\\d|30)|(02)-(0[1-9]|1\\d|2[0-8])))`;
|
|
1881
|
+
const dateRegex = new RegExp(`^${dateRegexSource}$`);
|
|
1882
|
+
function timeRegexSource(args) {
|
|
1883
|
+
let secondsRegexSource = `[0-5]\\d`;
|
|
1884
|
+
if (args.precision) {
|
|
1885
|
+
secondsRegexSource = `${secondsRegexSource}\\.\\d{${args.precision}}`;
|
|
1886
|
+
}
|
|
1887
|
+
else if (args.precision == null) {
|
|
1888
|
+
secondsRegexSource = `${secondsRegexSource}(\\.\\d+)?`;
|
|
1889
|
+
}
|
|
1890
|
+
const secondsQuantifier = args.precision ? "+" : "?"; // require seconds if precision is nonzero
|
|
1891
|
+
return `([01]\\d|2[0-3]):[0-5]\\d(:${secondsRegexSource})${secondsQuantifier}`;
|
|
1892
|
+
}
|
|
1893
|
+
function timeRegex(args) {
|
|
1894
|
+
return new RegExp(`^${timeRegexSource(args)}$`);
|
|
1895
|
+
}
|
|
1896
|
+
// Adapted from https://stackoverflow.com/a/3143231
|
|
1897
|
+
function datetimeRegex(args) {
|
|
1898
|
+
let regex = `${dateRegexSource}T${timeRegexSource(args)}`;
|
|
1899
|
+
const opts = [];
|
|
1900
|
+
opts.push(args.local ? `Z?` : `Z`);
|
|
1901
|
+
if (args.offset)
|
|
1902
|
+
opts.push(`([+-]\\d{2}:?\\d{2})`);
|
|
1903
|
+
regex = `${regex}(${opts.join("|")})`;
|
|
1904
|
+
return new RegExp(`^${regex}$`);
|
|
1905
|
+
}
|
|
1906
|
+
function isValidIP(ip, version) {
|
|
1907
|
+
if ((version === "v4" || !version) && ipv4Regex.test(ip)) {
|
|
1908
|
+
return true;
|
|
1909
|
+
}
|
|
1910
|
+
if ((version === "v6" || !version) && ipv6Regex.test(ip)) {
|
|
1911
|
+
return true;
|
|
1912
|
+
}
|
|
1913
|
+
return false;
|
|
1914
|
+
}
|
|
1915
|
+
function isValidJWT(jwt, alg) {
|
|
1916
|
+
if (!jwtRegex.test(jwt))
|
|
1917
|
+
return false;
|
|
1918
|
+
try {
|
|
1919
|
+
const [header] = jwt.split(".");
|
|
1920
|
+
if (!header)
|
|
1921
|
+
return false;
|
|
1922
|
+
// Convert base64url to base64
|
|
1923
|
+
const base64 = header
|
|
1924
|
+
.replace(/-/g, "+")
|
|
1925
|
+
.replace(/_/g, "/")
|
|
1926
|
+
.padEnd(header.length + ((4 - (header.length % 4)) % 4), "=");
|
|
1927
|
+
const decoded = JSON.parse(atob(base64));
|
|
1928
|
+
if (typeof decoded !== "object" || decoded === null)
|
|
1929
|
+
return false;
|
|
1930
|
+
if ("typ" in decoded && decoded?.typ !== "JWT")
|
|
1931
|
+
return false;
|
|
1932
|
+
if (!decoded.alg)
|
|
1933
|
+
return false;
|
|
1934
|
+
if (alg && decoded.alg !== alg)
|
|
1935
|
+
return false;
|
|
1936
|
+
return true;
|
|
1937
|
+
}
|
|
1938
|
+
catch {
|
|
1939
|
+
return false;
|
|
1940
|
+
}
|
|
1941
|
+
}
|
|
1942
|
+
function isValidCidr(ip, version) {
|
|
1943
|
+
if ((version === "v4" || !version) && ipv4CidrRegex.test(ip)) {
|
|
1944
|
+
return true;
|
|
1945
|
+
}
|
|
1946
|
+
if ((version === "v6" || !version) && ipv6CidrRegex.test(ip)) {
|
|
1947
|
+
return true;
|
|
1948
|
+
}
|
|
1949
|
+
return false;
|
|
1950
|
+
}
|
|
1951
|
+
class ZodString extends ZodType {
|
|
1952
|
+
_parse(input) {
|
|
1953
|
+
if (this._def.coerce) {
|
|
1954
|
+
input.data = String(input.data);
|
|
1955
|
+
}
|
|
1956
|
+
const parsedType = this._getType(input);
|
|
1957
|
+
if (parsedType !== ZodParsedType.string) {
|
|
1958
|
+
const ctx = this._getOrReturnCtx(input);
|
|
1959
|
+
addIssueToContext(ctx, {
|
|
1960
|
+
code: ZodIssueCode.invalid_type,
|
|
1961
|
+
expected: ZodParsedType.string,
|
|
1962
|
+
received: ctx.parsedType,
|
|
1963
|
+
});
|
|
1964
|
+
return INVALID;
|
|
1965
|
+
}
|
|
1966
|
+
const status = new ParseStatus();
|
|
1967
|
+
let ctx = undefined;
|
|
1968
|
+
for (const check of this._def.checks) {
|
|
1969
|
+
if (check.kind === "min") {
|
|
1970
|
+
if (input.data.length < check.value) {
|
|
1971
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1972
|
+
addIssueToContext(ctx, {
|
|
1973
|
+
code: ZodIssueCode.too_small,
|
|
1974
|
+
minimum: check.value,
|
|
1975
|
+
type: "string",
|
|
1976
|
+
inclusive: true,
|
|
1977
|
+
exact: false,
|
|
1978
|
+
message: check.message,
|
|
1979
|
+
});
|
|
1980
|
+
status.dirty();
|
|
1981
|
+
}
|
|
1982
|
+
}
|
|
1983
|
+
else if (check.kind === "max") {
|
|
1984
|
+
if (input.data.length > check.value) {
|
|
1985
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1986
|
+
addIssueToContext(ctx, {
|
|
1987
|
+
code: ZodIssueCode.too_big,
|
|
1988
|
+
maximum: check.value,
|
|
1989
|
+
type: "string",
|
|
1990
|
+
inclusive: true,
|
|
1991
|
+
exact: false,
|
|
1992
|
+
message: check.message,
|
|
1993
|
+
});
|
|
1994
|
+
status.dirty();
|
|
1995
|
+
}
|
|
1996
|
+
}
|
|
1997
|
+
else if (check.kind === "length") {
|
|
1998
|
+
const tooBig = input.data.length > check.value;
|
|
1999
|
+
const tooSmall = input.data.length < check.value;
|
|
2000
|
+
if (tooBig || tooSmall) {
|
|
2001
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2002
|
+
if (tooBig) {
|
|
2003
|
+
addIssueToContext(ctx, {
|
|
2004
|
+
code: ZodIssueCode.too_big,
|
|
2005
|
+
maximum: check.value,
|
|
2006
|
+
type: "string",
|
|
2007
|
+
inclusive: true,
|
|
2008
|
+
exact: true,
|
|
2009
|
+
message: check.message,
|
|
2010
|
+
});
|
|
2011
|
+
}
|
|
2012
|
+
else if (tooSmall) {
|
|
2013
|
+
addIssueToContext(ctx, {
|
|
2014
|
+
code: ZodIssueCode.too_small,
|
|
2015
|
+
minimum: check.value,
|
|
2016
|
+
type: "string",
|
|
2017
|
+
inclusive: true,
|
|
2018
|
+
exact: true,
|
|
2019
|
+
message: check.message,
|
|
2020
|
+
});
|
|
2021
|
+
}
|
|
2022
|
+
status.dirty();
|
|
2023
|
+
}
|
|
2024
|
+
}
|
|
2025
|
+
else if (check.kind === "email") {
|
|
2026
|
+
if (!emailRegex.test(input.data)) {
|
|
2027
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2028
|
+
addIssueToContext(ctx, {
|
|
2029
|
+
validation: "email",
|
|
2030
|
+
code: ZodIssueCode.invalid_string,
|
|
2031
|
+
message: check.message,
|
|
2032
|
+
});
|
|
2033
|
+
status.dirty();
|
|
2034
|
+
}
|
|
2035
|
+
}
|
|
2036
|
+
else if (check.kind === "emoji") {
|
|
2037
|
+
if (!emojiRegex) {
|
|
2038
|
+
emojiRegex = new RegExp(_emojiRegex, "u");
|
|
2039
|
+
}
|
|
2040
|
+
if (!emojiRegex.test(input.data)) {
|
|
2041
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2042
|
+
addIssueToContext(ctx, {
|
|
2043
|
+
validation: "emoji",
|
|
2044
|
+
code: ZodIssueCode.invalid_string,
|
|
2045
|
+
message: check.message,
|
|
2046
|
+
});
|
|
2047
|
+
status.dirty();
|
|
2048
|
+
}
|
|
2049
|
+
}
|
|
2050
|
+
else if (check.kind === "uuid") {
|
|
2051
|
+
if (!uuidRegex.test(input.data)) {
|
|
2052
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2053
|
+
addIssueToContext(ctx, {
|
|
2054
|
+
validation: "uuid",
|
|
2055
|
+
code: ZodIssueCode.invalid_string,
|
|
2056
|
+
message: check.message,
|
|
2057
|
+
});
|
|
2058
|
+
status.dirty();
|
|
2059
|
+
}
|
|
2060
|
+
}
|
|
2061
|
+
else if (check.kind === "nanoid") {
|
|
2062
|
+
if (!nanoidRegex.test(input.data)) {
|
|
2063
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2064
|
+
addIssueToContext(ctx, {
|
|
2065
|
+
validation: "nanoid",
|
|
2066
|
+
code: ZodIssueCode.invalid_string,
|
|
2067
|
+
message: check.message,
|
|
2068
|
+
});
|
|
2069
|
+
status.dirty();
|
|
2070
|
+
}
|
|
2071
|
+
}
|
|
2072
|
+
else if (check.kind === "cuid") {
|
|
2073
|
+
if (!cuidRegex.test(input.data)) {
|
|
2074
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2075
|
+
addIssueToContext(ctx, {
|
|
2076
|
+
validation: "cuid",
|
|
2077
|
+
code: ZodIssueCode.invalid_string,
|
|
2078
|
+
message: check.message,
|
|
2079
|
+
});
|
|
2080
|
+
status.dirty();
|
|
2081
|
+
}
|
|
2082
|
+
}
|
|
2083
|
+
else if (check.kind === "cuid2") {
|
|
2084
|
+
if (!cuid2Regex.test(input.data)) {
|
|
2085
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2086
|
+
addIssueToContext(ctx, {
|
|
2087
|
+
validation: "cuid2",
|
|
2088
|
+
code: ZodIssueCode.invalid_string,
|
|
2089
|
+
message: check.message,
|
|
2090
|
+
});
|
|
2091
|
+
status.dirty();
|
|
2092
|
+
}
|
|
2093
|
+
}
|
|
2094
|
+
else if (check.kind === "ulid") {
|
|
2095
|
+
if (!ulidRegex.test(input.data)) {
|
|
2096
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2097
|
+
addIssueToContext(ctx, {
|
|
2098
|
+
validation: "ulid",
|
|
2099
|
+
code: ZodIssueCode.invalid_string,
|
|
2100
|
+
message: check.message,
|
|
2101
|
+
});
|
|
2102
|
+
status.dirty();
|
|
2103
|
+
}
|
|
2104
|
+
}
|
|
2105
|
+
else if (check.kind === "url") {
|
|
2106
|
+
try {
|
|
2107
|
+
new URL(input.data);
|
|
2108
|
+
}
|
|
2109
|
+
catch {
|
|
2110
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2111
|
+
addIssueToContext(ctx, {
|
|
2112
|
+
validation: "url",
|
|
2113
|
+
code: ZodIssueCode.invalid_string,
|
|
2114
|
+
message: check.message,
|
|
2115
|
+
});
|
|
2116
|
+
status.dirty();
|
|
2117
|
+
}
|
|
2118
|
+
}
|
|
2119
|
+
else if (check.kind === "regex") {
|
|
2120
|
+
check.regex.lastIndex = 0;
|
|
2121
|
+
const testResult = check.regex.test(input.data);
|
|
2122
|
+
if (!testResult) {
|
|
2123
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2124
|
+
addIssueToContext(ctx, {
|
|
2125
|
+
validation: "regex",
|
|
2126
|
+
code: ZodIssueCode.invalid_string,
|
|
2127
|
+
message: check.message,
|
|
2128
|
+
});
|
|
2129
|
+
status.dirty();
|
|
2130
|
+
}
|
|
2131
|
+
}
|
|
2132
|
+
else if (check.kind === "trim") {
|
|
2133
|
+
input.data = input.data.trim();
|
|
2134
|
+
}
|
|
2135
|
+
else if (check.kind === "includes") {
|
|
2136
|
+
if (!input.data.includes(check.value, check.position)) {
|
|
2137
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2138
|
+
addIssueToContext(ctx, {
|
|
2139
|
+
code: ZodIssueCode.invalid_string,
|
|
2140
|
+
validation: { includes: check.value, position: check.position },
|
|
2141
|
+
message: check.message,
|
|
2142
|
+
});
|
|
2143
|
+
status.dirty();
|
|
2144
|
+
}
|
|
2145
|
+
}
|
|
2146
|
+
else if (check.kind === "toLowerCase") {
|
|
2147
|
+
input.data = input.data.toLowerCase();
|
|
2148
|
+
}
|
|
2149
|
+
else if (check.kind === "toUpperCase") {
|
|
2150
|
+
input.data = input.data.toUpperCase();
|
|
2151
|
+
}
|
|
2152
|
+
else if (check.kind === "startsWith") {
|
|
2153
|
+
if (!input.data.startsWith(check.value)) {
|
|
2154
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2155
|
+
addIssueToContext(ctx, {
|
|
2156
|
+
code: ZodIssueCode.invalid_string,
|
|
2157
|
+
validation: { startsWith: check.value },
|
|
2158
|
+
message: check.message,
|
|
2159
|
+
});
|
|
2160
|
+
status.dirty();
|
|
2161
|
+
}
|
|
2162
|
+
}
|
|
2163
|
+
else if (check.kind === "endsWith") {
|
|
2164
|
+
if (!input.data.endsWith(check.value)) {
|
|
2165
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2166
|
+
addIssueToContext(ctx, {
|
|
2167
|
+
code: ZodIssueCode.invalid_string,
|
|
2168
|
+
validation: { endsWith: check.value },
|
|
2169
|
+
message: check.message,
|
|
2170
|
+
});
|
|
2171
|
+
status.dirty();
|
|
2172
|
+
}
|
|
2173
|
+
}
|
|
2174
|
+
else if (check.kind === "datetime") {
|
|
2175
|
+
const regex = datetimeRegex(check);
|
|
2176
|
+
if (!regex.test(input.data)) {
|
|
2177
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2178
|
+
addIssueToContext(ctx, {
|
|
2179
|
+
code: ZodIssueCode.invalid_string,
|
|
2180
|
+
validation: "datetime",
|
|
2181
|
+
message: check.message,
|
|
2182
|
+
});
|
|
2183
|
+
status.dirty();
|
|
2184
|
+
}
|
|
2185
|
+
}
|
|
2186
|
+
else if (check.kind === "date") {
|
|
2187
|
+
const regex = dateRegex;
|
|
2188
|
+
if (!regex.test(input.data)) {
|
|
2189
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2190
|
+
addIssueToContext(ctx, {
|
|
2191
|
+
code: ZodIssueCode.invalid_string,
|
|
2192
|
+
validation: "date",
|
|
2193
|
+
message: check.message,
|
|
2194
|
+
});
|
|
2195
|
+
status.dirty();
|
|
2196
|
+
}
|
|
2197
|
+
}
|
|
2198
|
+
else if (check.kind === "time") {
|
|
2199
|
+
const regex = timeRegex(check);
|
|
2200
|
+
if (!regex.test(input.data)) {
|
|
2201
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2202
|
+
addIssueToContext(ctx, {
|
|
2203
|
+
code: ZodIssueCode.invalid_string,
|
|
2204
|
+
validation: "time",
|
|
2205
|
+
message: check.message,
|
|
2206
|
+
});
|
|
2207
|
+
status.dirty();
|
|
2208
|
+
}
|
|
2209
|
+
}
|
|
2210
|
+
else if (check.kind === "duration") {
|
|
2211
|
+
if (!durationRegex.test(input.data)) {
|
|
2212
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2213
|
+
addIssueToContext(ctx, {
|
|
2214
|
+
validation: "duration",
|
|
2215
|
+
code: ZodIssueCode.invalid_string,
|
|
2216
|
+
message: check.message,
|
|
2217
|
+
});
|
|
2218
|
+
status.dirty();
|
|
2219
|
+
}
|
|
2220
|
+
}
|
|
2221
|
+
else if (check.kind === "ip") {
|
|
2222
|
+
if (!isValidIP(input.data, check.version)) {
|
|
2223
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2224
|
+
addIssueToContext(ctx, {
|
|
2225
|
+
validation: "ip",
|
|
2226
|
+
code: ZodIssueCode.invalid_string,
|
|
2227
|
+
message: check.message,
|
|
2228
|
+
});
|
|
2229
|
+
status.dirty();
|
|
2230
|
+
}
|
|
2231
|
+
}
|
|
2232
|
+
else if (check.kind === "jwt") {
|
|
2233
|
+
if (!isValidJWT(input.data, check.alg)) {
|
|
2234
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2235
|
+
addIssueToContext(ctx, {
|
|
2236
|
+
validation: "jwt",
|
|
2237
|
+
code: ZodIssueCode.invalid_string,
|
|
2238
|
+
message: check.message,
|
|
2239
|
+
});
|
|
2240
|
+
status.dirty();
|
|
2241
|
+
}
|
|
2242
|
+
}
|
|
2243
|
+
else if (check.kind === "cidr") {
|
|
2244
|
+
if (!isValidCidr(input.data, check.version)) {
|
|
2245
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2246
|
+
addIssueToContext(ctx, {
|
|
2247
|
+
validation: "cidr",
|
|
2248
|
+
code: ZodIssueCode.invalid_string,
|
|
2249
|
+
message: check.message,
|
|
2250
|
+
});
|
|
2251
|
+
status.dirty();
|
|
2252
|
+
}
|
|
2253
|
+
}
|
|
2254
|
+
else if (check.kind === "base64") {
|
|
2255
|
+
if (!base64Regex.test(input.data)) {
|
|
2256
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2257
|
+
addIssueToContext(ctx, {
|
|
2258
|
+
validation: "base64",
|
|
2259
|
+
code: ZodIssueCode.invalid_string,
|
|
2260
|
+
message: check.message,
|
|
2261
|
+
});
|
|
2262
|
+
status.dirty();
|
|
2263
|
+
}
|
|
2264
|
+
}
|
|
2265
|
+
else if (check.kind === "base64url") {
|
|
2266
|
+
if (!base64urlRegex.test(input.data)) {
|
|
2267
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2268
|
+
addIssueToContext(ctx, {
|
|
2269
|
+
validation: "base64url",
|
|
2270
|
+
code: ZodIssueCode.invalid_string,
|
|
2271
|
+
message: check.message,
|
|
2272
|
+
});
|
|
2273
|
+
status.dirty();
|
|
2274
|
+
}
|
|
2275
|
+
}
|
|
2276
|
+
else {
|
|
2277
|
+
util.assertNever(check);
|
|
2278
|
+
}
|
|
2279
|
+
}
|
|
2280
|
+
return { status: status.value, value: input.data };
|
|
2281
|
+
}
|
|
2282
|
+
_regex(regex, validation, message) {
|
|
2283
|
+
return this.refinement((data) => regex.test(data), {
|
|
2284
|
+
validation,
|
|
2285
|
+
code: ZodIssueCode.invalid_string,
|
|
2286
|
+
...errorUtil.errToObj(message),
|
|
2287
|
+
});
|
|
2288
|
+
}
|
|
2289
|
+
_addCheck(check) {
|
|
2290
|
+
return new ZodString({
|
|
2291
|
+
...this._def,
|
|
2292
|
+
checks: [...this._def.checks, check],
|
|
2293
|
+
});
|
|
2294
|
+
}
|
|
2295
|
+
email(message) {
|
|
2296
|
+
return this._addCheck({ kind: "email", ...errorUtil.errToObj(message) });
|
|
2297
|
+
}
|
|
2298
|
+
url(message) {
|
|
2299
|
+
return this._addCheck({ kind: "url", ...errorUtil.errToObj(message) });
|
|
2300
|
+
}
|
|
2301
|
+
emoji(message) {
|
|
2302
|
+
return this._addCheck({ kind: "emoji", ...errorUtil.errToObj(message) });
|
|
2303
|
+
}
|
|
2304
|
+
uuid(message) {
|
|
2305
|
+
return this._addCheck({ kind: "uuid", ...errorUtil.errToObj(message) });
|
|
2306
|
+
}
|
|
2307
|
+
nanoid(message) {
|
|
2308
|
+
return this._addCheck({ kind: "nanoid", ...errorUtil.errToObj(message) });
|
|
2309
|
+
}
|
|
2310
|
+
cuid(message) {
|
|
2311
|
+
return this._addCheck({ kind: "cuid", ...errorUtil.errToObj(message) });
|
|
2312
|
+
}
|
|
2313
|
+
cuid2(message) {
|
|
2314
|
+
return this._addCheck({ kind: "cuid2", ...errorUtil.errToObj(message) });
|
|
2315
|
+
}
|
|
2316
|
+
ulid(message) {
|
|
2317
|
+
return this._addCheck({ kind: "ulid", ...errorUtil.errToObj(message) });
|
|
2318
|
+
}
|
|
2319
|
+
base64(message) {
|
|
2320
|
+
return this._addCheck({ kind: "base64", ...errorUtil.errToObj(message) });
|
|
2321
|
+
}
|
|
2322
|
+
base64url(message) {
|
|
2323
|
+
// base64url encoding is a modification of base64 that can safely be used in URLs and filenames
|
|
2324
|
+
return this._addCheck({
|
|
2325
|
+
kind: "base64url",
|
|
2326
|
+
...errorUtil.errToObj(message),
|
|
2327
|
+
});
|
|
2328
|
+
}
|
|
2329
|
+
jwt(options) {
|
|
2330
|
+
return this._addCheck({ kind: "jwt", ...errorUtil.errToObj(options) });
|
|
2331
|
+
}
|
|
2332
|
+
ip(options) {
|
|
2333
|
+
return this._addCheck({ kind: "ip", ...errorUtil.errToObj(options) });
|
|
2334
|
+
}
|
|
2335
|
+
cidr(options) {
|
|
2336
|
+
return this._addCheck({ kind: "cidr", ...errorUtil.errToObj(options) });
|
|
2337
|
+
}
|
|
2338
|
+
datetime(options) {
|
|
2339
|
+
if (typeof options === "string") {
|
|
2340
|
+
return this._addCheck({
|
|
2341
|
+
kind: "datetime",
|
|
2342
|
+
precision: null,
|
|
2343
|
+
offset: false,
|
|
2344
|
+
local: false,
|
|
2345
|
+
message: options,
|
|
2346
|
+
});
|
|
2347
|
+
}
|
|
2348
|
+
return this._addCheck({
|
|
2349
|
+
kind: "datetime",
|
|
2350
|
+
precision: typeof options?.precision === "undefined" ? null : options?.precision,
|
|
2351
|
+
offset: options?.offset ?? false,
|
|
2352
|
+
local: options?.local ?? false,
|
|
2353
|
+
...errorUtil.errToObj(options?.message),
|
|
2354
|
+
});
|
|
2355
|
+
}
|
|
2356
|
+
date(message) {
|
|
2357
|
+
return this._addCheck({ kind: "date", message });
|
|
2358
|
+
}
|
|
2359
|
+
time(options) {
|
|
2360
|
+
if (typeof options === "string") {
|
|
2361
|
+
return this._addCheck({
|
|
2362
|
+
kind: "time",
|
|
2363
|
+
precision: null,
|
|
2364
|
+
message: options,
|
|
2365
|
+
});
|
|
2366
|
+
}
|
|
2367
|
+
return this._addCheck({
|
|
2368
|
+
kind: "time",
|
|
2369
|
+
precision: typeof options?.precision === "undefined" ? null : options?.precision,
|
|
2370
|
+
...errorUtil.errToObj(options?.message),
|
|
2371
|
+
});
|
|
2372
|
+
}
|
|
2373
|
+
duration(message) {
|
|
2374
|
+
return this._addCheck({ kind: "duration", ...errorUtil.errToObj(message) });
|
|
2375
|
+
}
|
|
2376
|
+
regex(regex, message) {
|
|
2377
|
+
return this._addCheck({
|
|
2378
|
+
kind: "regex",
|
|
2379
|
+
regex: regex,
|
|
2380
|
+
...errorUtil.errToObj(message),
|
|
2381
|
+
});
|
|
2382
|
+
}
|
|
2383
|
+
includes(value, options) {
|
|
2384
|
+
return this._addCheck({
|
|
2385
|
+
kind: "includes",
|
|
2386
|
+
value: value,
|
|
2387
|
+
position: options?.position,
|
|
2388
|
+
...errorUtil.errToObj(options?.message),
|
|
2389
|
+
});
|
|
2390
|
+
}
|
|
2391
|
+
startsWith(value, message) {
|
|
2392
|
+
return this._addCheck({
|
|
2393
|
+
kind: "startsWith",
|
|
2394
|
+
value: value,
|
|
2395
|
+
...errorUtil.errToObj(message),
|
|
2396
|
+
});
|
|
2397
|
+
}
|
|
2398
|
+
endsWith(value, message) {
|
|
2399
|
+
return this._addCheck({
|
|
2400
|
+
kind: "endsWith",
|
|
2401
|
+
value: value,
|
|
2402
|
+
...errorUtil.errToObj(message),
|
|
2403
|
+
});
|
|
2404
|
+
}
|
|
2405
|
+
min(minLength, message) {
|
|
2406
|
+
return this._addCheck({
|
|
2407
|
+
kind: "min",
|
|
2408
|
+
value: minLength,
|
|
2409
|
+
...errorUtil.errToObj(message),
|
|
2410
|
+
});
|
|
2411
|
+
}
|
|
2412
|
+
max(maxLength, message) {
|
|
2413
|
+
return this._addCheck({
|
|
2414
|
+
kind: "max",
|
|
2415
|
+
value: maxLength,
|
|
2416
|
+
...errorUtil.errToObj(message),
|
|
2417
|
+
});
|
|
2418
|
+
}
|
|
2419
|
+
length(len, message) {
|
|
2420
|
+
return this._addCheck({
|
|
2421
|
+
kind: "length",
|
|
2422
|
+
value: len,
|
|
2423
|
+
...errorUtil.errToObj(message),
|
|
2424
|
+
});
|
|
2425
|
+
}
|
|
2426
|
+
/**
|
|
2427
|
+
* Equivalent to `.min(1)`
|
|
2428
|
+
*/
|
|
2429
|
+
nonempty(message) {
|
|
2430
|
+
return this.min(1, errorUtil.errToObj(message));
|
|
2431
|
+
}
|
|
2432
|
+
trim() {
|
|
2433
|
+
return new ZodString({
|
|
2434
|
+
...this._def,
|
|
2435
|
+
checks: [...this._def.checks, { kind: "trim" }],
|
|
2436
|
+
});
|
|
2437
|
+
}
|
|
2438
|
+
toLowerCase() {
|
|
2439
|
+
return new ZodString({
|
|
2440
|
+
...this._def,
|
|
2441
|
+
checks: [...this._def.checks, { kind: "toLowerCase" }],
|
|
2442
|
+
});
|
|
2443
|
+
}
|
|
2444
|
+
toUpperCase() {
|
|
2445
|
+
return new ZodString({
|
|
2446
|
+
...this._def,
|
|
2447
|
+
checks: [...this._def.checks, { kind: "toUpperCase" }],
|
|
2448
|
+
});
|
|
2449
|
+
}
|
|
2450
|
+
get isDatetime() {
|
|
2451
|
+
return !!this._def.checks.find((ch) => ch.kind === "datetime");
|
|
2452
|
+
}
|
|
2453
|
+
get isDate() {
|
|
2454
|
+
return !!this._def.checks.find((ch) => ch.kind === "date");
|
|
2455
|
+
}
|
|
2456
|
+
get isTime() {
|
|
2457
|
+
return !!this._def.checks.find((ch) => ch.kind === "time");
|
|
2458
|
+
}
|
|
2459
|
+
get isDuration() {
|
|
2460
|
+
return !!this._def.checks.find((ch) => ch.kind === "duration");
|
|
2461
|
+
}
|
|
2462
|
+
get isEmail() {
|
|
2463
|
+
return !!this._def.checks.find((ch) => ch.kind === "email");
|
|
2464
|
+
}
|
|
2465
|
+
get isURL() {
|
|
2466
|
+
return !!this._def.checks.find((ch) => ch.kind === "url");
|
|
2467
|
+
}
|
|
2468
|
+
get isEmoji() {
|
|
2469
|
+
return !!this._def.checks.find((ch) => ch.kind === "emoji");
|
|
2470
|
+
}
|
|
2471
|
+
get isUUID() {
|
|
2472
|
+
return !!this._def.checks.find((ch) => ch.kind === "uuid");
|
|
2473
|
+
}
|
|
2474
|
+
get isNANOID() {
|
|
2475
|
+
return !!this._def.checks.find((ch) => ch.kind === "nanoid");
|
|
2476
|
+
}
|
|
2477
|
+
get isCUID() {
|
|
2478
|
+
return !!this._def.checks.find((ch) => ch.kind === "cuid");
|
|
2479
|
+
}
|
|
2480
|
+
get isCUID2() {
|
|
2481
|
+
return !!this._def.checks.find((ch) => ch.kind === "cuid2");
|
|
2482
|
+
}
|
|
2483
|
+
get isULID() {
|
|
2484
|
+
return !!this._def.checks.find((ch) => ch.kind === "ulid");
|
|
2485
|
+
}
|
|
2486
|
+
get isIP() {
|
|
2487
|
+
return !!this._def.checks.find((ch) => ch.kind === "ip");
|
|
2488
|
+
}
|
|
2489
|
+
get isCIDR() {
|
|
2490
|
+
return !!this._def.checks.find((ch) => ch.kind === "cidr");
|
|
2491
|
+
}
|
|
2492
|
+
get isBase64() {
|
|
2493
|
+
return !!this._def.checks.find((ch) => ch.kind === "base64");
|
|
2494
|
+
}
|
|
2495
|
+
get isBase64url() {
|
|
2496
|
+
// base64url encoding is a modification of base64 that can safely be used in URLs and filenames
|
|
2497
|
+
return !!this._def.checks.find((ch) => ch.kind === "base64url");
|
|
2498
|
+
}
|
|
2499
|
+
get minLength() {
|
|
2500
|
+
let min = null;
|
|
2501
|
+
for (const ch of this._def.checks) {
|
|
2502
|
+
if (ch.kind === "min") {
|
|
2503
|
+
if (min === null || ch.value > min)
|
|
2504
|
+
min = ch.value;
|
|
2505
|
+
}
|
|
2506
|
+
}
|
|
2507
|
+
return min;
|
|
2508
|
+
}
|
|
2509
|
+
get maxLength() {
|
|
2510
|
+
let max = null;
|
|
2511
|
+
for (const ch of this._def.checks) {
|
|
2512
|
+
if (ch.kind === "max") {
|
|
2513
|
+
if (max === null || ch.value < max)
|
|
2514
|
+
max = ch.value;
|
|
2515
|
+
}
|
|
2516
|
+
}
|
|
2517
|
+
return max;
|
|
2518
|
+
}
|
|
2519
|
+
}
|
|
2520
|
+
ZodString.create = (params) => {
|
|
2521
|
+
return new ZodString({
|
|
2522
|
+
checks: [],
|
|
2523
|
+
typeName: ZodFirstPartyTypeKind.ZodString,
|
|
2524
|
+
coerce: params?.coerce ?? false,
|
|
2525
|
+
...processCreateParams(params),
|
|
2526
|
+
});
|
|
2527
|
+
};
|
|
2528
|
+
// https://stackoverflow.com/questions/3966484/why-does-modulus-operator-return-fractional-number-in-javascript/31711034#31711034
|
|
2529
|
+
function floatSafeRemainder(val, step) {
|
|
2530
|
+
const valDecCount = (val.toString().split(".")[1] || "").length;
|
|
2531
|
+
const stepDecCount = (step.toString().split(".")[1] || "").length;
|
|
2532
|
+
const decCount = valDecCount > stepDecCount ? valDecCount : stepDecCount;
|
|
2533
|
+
const valInt = Number.parseInt(val.toFixed(decCount).replace(".", ""));
|
|
2534
|
+
const stepInt = Number.parseInt(step.toFixed(decCount).replace(".", ""));
|
|
2535
|
+
return (valInt % stepInt) / 10 ** decCount;
|
|
2536
|
+
}
|
|
2537
|
+
class ZodNumber extends ZodType {
|
|
2538
|
+
constructor() {
|
|
2539
|
+
super(...arguments);
|
|
2540
|
+
this.min = this.gte;
|
|
2541
|
+
this.max = this.lte;
|
|
2542
|
+
this.step = this.multipleOf;
|
|
2543
|
+
}
|
|
2544
|
+
_parse(input) {
|
|
2545
|
+
if (this._def.coerce) {
|
|
2546
|
+
input.data = Number(input.data);
|
|
2547
|
+
}
|
|
2548
|
+
const parsedType = this._getType(input);
|
|
2549
|
+
if (parsedType !== ZodParsedType.number) {
|
|
2550
|
+
const ctx = this._getOrReturnCtx(input);
|
|
2551
|
+
addIssueToContext(ctx, {
|
|
2552
|
+
code: ZodIssueCode.invalid_type,
|
|
2553
|
+
expected: ZodParsedType.number,
|
|
2554
|
+
received: ctx.parsedType,
|
|
2555
|
+
});
|
|
2556
|
+
return INVALID;
|
|
2557
|
+
}
|
|
2558
|
+
let ctx = undefined;
|
|
2559
|
+
const status = new ParseStatus();
|
|
2560
|
+
for (const check of this._def.checks) {
|
|
2561
|
+
if (check.kind === "int") {
|
|
2562
|
+
if (!util.isInteger(input.data)) {
|
|
2563
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2564
|
+
addIssueToContext(ctx, {
|
|
2565
|
+
code: ZodIssueCode.invalid_type,
|
|
2566
|
+
expected: "integer",
|
|
2567
|
+
received: "float",
|
|
2568
|
+
message: check.message,
|
|
2569
|
+
});
|
|
2570
|
+
status.dirty();
|
|
2571
|
+
}
|
|
2572
|
+
}
|
|
2573
|
+
else if (check.kind === "min") {
|
|
2574
|
+
const tooSmall = check.inclusive ? input.data < check.value : input.data <= check.value;
|
|
2575
|
+
if (tooSmall) {
|
|
2576
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2577
|
+
addIssueToContext(ctx, {
|
|
2578
|
+
code: ZodIssueCode.too_small,
|
|
2579
|
+
minimum: check.value,
|
|
2580
|
+
type: "number",
|
|
2581
|
+
inclusive: check.inclusive,
|
|
2582
|
+
exact: false,
|
|
2583
|
+
message: check.message,
|
|
2584
|
+
});
|
|
2585
|
+
status.dirty();
|
|
2586
|
+
}
|
|
2587
|
+
}
|
|
2588
|
+
else if (check.kind === "max") {
|
|
2589
|
+
const tooBig = check.inclusive ? input.data > check.value : input.data >= check.value;
|
|
2590
|
+
if (tooBig) {
|
|
2591
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2592
|
+
addIssueToContext(ctx, {
|
|
2593
|
+
code: ZodIssueCode.too_big,
|
|
2594
|
+
maximum: check.value,
|
|
2595
|
+
type: "number",
|
|
2596
|
+
inclusive: check.inclusive,
|
|
2597
|
+
exact: false,
|
|
2598
|
+
message: check.message,
|
|
2599
|
+
});
|
|
2600
|
+
status.dirty();
|
|
2601
|
+
}
|
|
2602
|
+
}
|
|
2603
|
+
else if (check.kind === "multipleOf") {
|
|
2604
|
+
if (floatSafeRemainder(input.data, check.value) !== 0) {
|
|
2605
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2606
|
+
addIssueToContext(ctx, {
|
|
2607
|
+
code: ZodIssueCode.not_multiple_of,
|
|
2608
|
+
multipleOf: check.value,
|
|
2609
|
+
message: check.message,
|
|
2610
|
+
});
|
|
2611
|
+
status.dirty();
|
|
2612
|
+
}
|
|
2613
|
+
}
|
|
2614
|
+
else if (check.kind === "finite") {
|
|
2615
|
+
if (!Number.isFinite(input.data)) {
|
|
2616
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2617
|
+
addIssueToContext(ctx, {
|
|
2618
|
+
code: ZodIssueCode.not_finite,
|
|
2619
|
+
message: check.message,
|
|
2620
|
+
});
|
|
2621
|
+
status.dirty();
|
|
2622
|
+
}
|
|
2623
|
+
}
|
|
2624
|
+
else {
|
|
2625
|
+
util.assertNever(check);
|
|
2626
|
+
}
|
|
2627
|
+
}
|
|
2628
|
+
return { status: status.value, value: input.data };
|
|
2629
|
+
}
|
|
2630
|
+
gte(value, message) {
|
|
2631
|
+
return this.setLimit("min", value, true, errorUtil.toString(message));
|
|
2632
|
+
}
|
|
2633
|
+
gt(value, message) {
|
|
2634
|
+
return this.setLimit("min", value, false, errorUtil.toString(message));
|
|
2635
|
+
}
|
|
2636
|
+
lte(value, message) {
|
|
2637
|
+
return this.setLimit("max", value, true, errorUtil.toString(message));
|
|
2638
|
+
}
|
|
2639
|
+
lt(value, message) {
|
|
2640
|
+
return this.setLimit("max", value, false, errorUtil.toString(message));
|
|
2641
|
+
}
|
|
2642
|
+
setLimit(kind, value, inclusive, message) {
|
|
2643
|
+
return new ZodNumber({
|
|
2644
|
+
...this._def,
|
|
2645
|
+
checks: [
|
|
2646
|
+
...this._def.checks,
|
|
2647
|
+
{
|
|
2648
|
+
kind,
|
|
2649
|
+
value,
|
|
2650
|
+
inclusive,
|
|
2651
|
+
message: errorUtil.toString(message),
|
|
2652
|
+
},
|
|
2653
|
+
],
|
|
2654
|
+
});
|
|
2655
|
+
}
|
|
2656
|
+
_addCheck(check) {
|
|
2657
|
+
return new ZodNumber({
|
|
2658
|
+
...this._def,
|
|
2659
|
+
checks: [...this._def.checks, check],
|
|
2660
|
+
});
|
|
2661
|
+
}
|
|
2662
|
+
int(message) {
|
|
2663
|
+
return this._addCheck({
|
|
2664
|
+
kind: "int",
|
|
2665
|
+
message: errorUtil.toString(message),
|
|
2666
|
+
});
|
|
2667
|
+
}
|
|
2668
|
+
positive(message) {
|
|
2669
|
+
return this._addCheck({
|
|
2670
|
+
kind: "min",
|
|
2671
|
+
value: 0,
|
|
2672
|
+
inclusive: false,
|
|
2673
|
+
message: errorUtil.toString(message),
|
|
2674
|
+
});
|
|
2675
|
+
}
|
|
2676
|
+
negative(message) {
|
|
2677
|
+
return this._addCheck({
|
|
2678
|
+
kind: "max",
|
|
2679
|
+
value: 0,
|
|
2680
|
+
inclusive: false,
|
|
2681
|
+
message: errorUtil.toString(message),
|
|
2682
|
+
});
|
|
2683
|
+
}
|
|
2684
|
+
nonpositive(message) {
|
|
2685
|
+
return this._addCheck({
|
|
2686
|
+
kind: "max",
|
|
2687
|
+
value: 0,
|
|
2688
|
+
inclusive: true,
|
|
2689
|
+
message: errorUtil.toString(message),
|
|
2690
|
+
});
|
|
2691
|
+
}
|
|
2692
|
+
nonnegative(message) {
|
|
2693
|
+
return this._addCheck({
|
|
2694
|
+
kind: "min",
|
|
2695
|
+
value: 0,
|
|
2696
|
+
inclusive: true,
|
|
2697
|
+
message: errorUtil.toString(message),
|
|
2698
|
+
});
|
|
2699
|
+
}
|
|
2700
|
+
multipleOf(value, message) {
|
|
2701
|
+
return this._addCheck({
|
|
2702
|
+
kind: "multipleOf",
|
|
2703
|
+
value: value,
|
|
2704
|
+
message: errorUtil.toString(message),
|
|
2705
|
+
});
|
|
2706
|
+
}
|
|
2707
|
+
finite(message) {
|
|
2708
|
+
return this._addCheck({
|
|
2709
|
+
kind: "finite",
|
|
2710
|
+
message: errorUtil.toString(message),
|
|
2711
|
+
});
|
|
2712
|
+
}
|
|
2713
|
+
safe(message) {
|
|
2714
|
+
return this._addCheck({
|
|
2715
|
+
kind: "min",
|
|
2716
|
+
inclusive: true,
|
|
2717
|
+
value: Number.MIN_SAFE_INTEGER,
|
|
2718
|
+
message: errorUtil.toString(message),
|
|
2719
|
+
})._addCheck({
|
|
2720
|
+
kind: "max",
|
|
2721
|
+
inclusive: true,
|
|
2722
|
+
value: Number.MAX_SAFE_INTEGER,
|
|
2723
|
+
message: errorUtil.toString(message),
|
|
2724
|
+
});
|
|
2725
|
+
}
|
|
2726
|
+
get minValue() {
|
|
2727
|
+
let min = null;
|
|
2728
|
+
for (const ch of this._def.checks) {
|
|
2729
|
+
if (ch.kind === "min") {
|
|
2730
|
+
if (min === null || ch.value > min)
|
|
2731
|
+
min = ch.value;
|
|
2732
|
+
}
|
|
2733
|
+
}
|
|
2734
|
+
return min;
|
|
2735
|
+
}
|
|
2736
|
+
get maxValue() {
|
|
2737
|
+
let max = null;
|
|
2738
|
+
for (const ch of this._def.checks) {
|
|
2739
|
+
if (ch.kind === "max") {
|
|
2740
|
+
if (max === null || ch.value < max)
|
|
2741
|
+
max = ch.value;
|
|
2742
|
+
}
|
|
2743
|
+
}
|
|
2744
|
+
return max;
|
|
2745
|
+
}
|
|
2746
|
+
get isInt() {
|
|
2747
|
+
return !!this._def.checks.find((ch) => ch.kind === "int" || (ch.kind === "multipleOf" && util.isInteger(ch.value)));
|
|
2748
|
+
}
|
|
2749
|
+
get isFinite() {
|
|
2750
|
+
let max = null;
|
|
2751
|
+
let min = null;
|
|
2752
|
+
for (const ch of this._def.checks) {
|
|
2753
|
+
if (ch.kind === "finite" || ch.kind === "int" || ch.kind === "multipleOf") {
|
|
2754
|
+
return true;
|
|
2755
|
+
}
|
|
2756
|
+
else if (ch.kind === "min") {
|
|
2757
|
+
if (min === null || ch.value > min)
|
|
2758
|
+
min = ch.value;
|
|
2759
|
+
}
|
|
2760
|
+
else if (ch.kind === "max") {
|
|
2761
|
+
if (max === null || ch.value < max)
|
|
2762
|
+
max = ch.value;
|
|
2763
|
+
}
|
|
2764
|
+
}
|
|
2765
|
+
return Number.isFinite(min) && Number.isFinite(max);
|
|
2766
|
+
}
|
|
2767
|
+
}
|
|
2768
|
+
ZodNumber.create = (params) => {
|
|
2769
|
+
return new ZodNumber({
|
|
2770
|
+
checks: [],
|
|
2771
|
+
typeName: ZodFirstPartyTypeKind.ZodNumber,
|
|
2772
|
+
coerce: params?.coerce || false,
|
|
2773
|
+
...processCreateParams(params),
|
|
2774
|
+
});
|
|
2775
|
+
};
|
|
2776
|
+
class ZodBigInt extends ZodType {
|
|
2777
|
+
constructor() {
|
|
2778
|
+
super(...arguments);
|
|
2779
|
+
this.min = this.gte;
|
|
2780
|
+
this.max = this.lte;
|
|
2781
|
+
}
|
|
2782
|
+
_parse(input) {
|
|
2783
|
+
if (this._def.coerce) {
|
|
2784
|
+
try {
|
|
2785
|
+
input.data = BigInt(input.data);
|
|
2786
|
+
}
|
|
2787
|
+
catch {
|
|
2788
|
+
return this._getInvalidInput(input);
|
|
2789
|
+
}
|
|
2790
|
+
}
|
|
2791
|
+
const parsedType = this._getType(input);
|
|
2792
|
+
if (parsedType !== ZodParsedType.bigint) {
|
|
2793
|
+
return this._getInvalidInput(input);
|
|
2794
|
+
}
|
|
2795
|
+
let ctx = undefined;
|
|
2796
|
+
const status = new ParseStatus();
|
|
2797
|
+
for (const check of this._def.checks) {
|
|
2798
|
+
if (check.kind === "min") {
|
|
2799
|
+
const tooSmall = check.inclusive ? input.data < check.value : input.data <= check.value;
|
|
2800
|
+
if (tooSmall) {
|
|
2801
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2802
|
+
addIssueToContext(ctx, {
|
|
2803
|
+
code: ZodIssueCode.too_small,
|
|
2804
|
+
type: "bigint",
|
|
2805
|
+
minimum: check.value,
|
|
2806
|
+
inclusive: check.inclusive,
|
|
2807
|
+
message: check.message,
|
|
2808
|
+
});
|
|
2809
|
+
status.dirty();
|
|
2810
|
+
}
|
|
2811
|
+
}
|
|
2812
|
+
else if (check.kind === "max") {
|
|
2813
|
+
const tooBig = check.inclusive ? input.data > check.value : input.data >= check.value;
|
|
2814
|
+
if (tooBig) {
|
|
2815
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2816
|
+
addIssueToContext(ctx, {
|
|
2817
|
+
code: ZodIssueCode.too_big,
|
|
2818
|
+
type: "bigint",
|
|
2819
|
+
maximum: check.value,
|
|
2820
|
+
inclusive: check.inclusive,
|
|
2821
|
+
message: check.message,
|
|
2822
|
+
});
|
|
2823
|
+
status.dirty();
|
|
2824
|
+
}
|
|
2825
|
+
}
|
|
2826
|
+
else if (check.kind === "multipleOf") {
|
|
2827
|
+
if (input.data % check.value !== BigInt(0)) {
|
|
2828
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2829
|
+
addIssueToContext(ctx, {
|
|
2830
|
+
code: ZodIssueCode.not_multiple_of,
|
|
2831
|
+
multipleOf: check.value,
|
|
2832
|
+
message: check.message,
|
|
2833
|
+
});
|
|
2834
|
+
status.dirty();
|
|
2835
|
+
}
|
|
2836
|
+
}
|
|
2837
|
+
else {
|
|
2838
|
+
util.assertNever(check);
|
|
2839
|
+
}
|
|
2840
|
+
}
|
|
2841
|
+
return { status: status.value, value: input.data };
|
|
2842
|
+
}
|
|
2843
|
+
_getInvalidInput(input) {
|
|
2844
|
+
const ctx = this._getOrReturnCtx(input);
|
|
2845
|
+
addIssueToContext(ctx, {
|
|
2846
|
+
code: ZodIssueCode.invalid_type,
|
|
2847
|
+
expected: ZodParsedType.bigint,
|
|
2848
|
+
received: ctx.parsedType,
|
|
2849
|
+
});
|
|
2850
|
+
return INVALID;
|
|
2851
|
+
}
|
|
2852
|
+
gte(value, message) {
|
|
2853
|
+
return this.setLimit("min", value, true, errorUtil.toString(message));
|
|
2854
|
+
}
|
|
2855
|
+
gt(value, message) {
|
|
2856
|
+
return this.setLimit("min", value, false, errorUtil.toString(message));
|
|
2857
|
+
}
|
|
2858
|
+
lte(value, message) {
|
|
2859
|
+
return this.setLimit("max", value, true, errorUtil.toString(message));
|
|
2860
|
+
}
|
|
2861
|
+
lt(value, message) {
|
|
2862
|
+
return this.setLimit("max", value, false, errorUtil.toString(message));
|
|
2863
|
+
}
|
|
2864
|
+
setLimit(kind, value, inclusive, message) {
|
|
2865
|
+
return new ZodBigInt({
|
|
2866
|
+
...this._def,
|
|
2867
|
+
checks: [
|
|
2868
|
+
...this._def.checks,
|
|
2869
|
+
{
|
|
2870
|
+
kind,
|
|
2871
|
+
value,
|
|
2872
|
+
inclusive,
|
|
2873
|
+
message: errorUtil.toString(message),
|
|
2874
|
+
},
|
|
2875
|
+
],
|
|
2876
|
+
});
|
|
2877
|
+
}
|
|
2878
|
+
_addCheck(check) {
|
|
2879
|
+
return new ZodBigInt({
|
|
2880
|
+
...this._def,
|
|
2881
|
+
checks: [...this._def.checks, check],
|
|
2882
|
+
});
|
|
2883
|
+
}
|
|
2884
|
+
positive(message) {
|
|
2885
|
+
return this._addCheck({
|
|
2886
|
+
kind: "min",
|
|
2887
|
+
value: BigInt(0),
|
|
2888
|
+
inclusive: false,
|
|
2889
|
+
message: errorUtil.toString(message),
|
|
2890
|
+
});
|
|
2891
|
+
}
|
|
2892
|
+
negative(message) {
|
|
2893
|
+
return this._addCheck({
|
|
2894
|
+
kind: "max",
|
|
2895
|
+
value: BigInt(0),
|
|
2896
|
+
inclusive: false,
|
|
2897
|
+
message: errorUtil.toString(message),
|
|
2898
|
+
});
|
|
2899
|
+
}
|
|
2900
|
+
nonpositive(message) {
|
|
2901
|
+
return this._addCheck({
|
|
2902
|
+
kind: "max",
|
|
2903
|
+
value: BigInt(0),
|
|
2904
|
+
inclusive: true,
|
|
2905
|
+
message: errorUtil.toString(message),
|
|
2906
|
+
});
|
|
2907
|
+
}
|
|
2908
|
+
nonnegative(message) {
|
|
2909
|
+
return this._addCheck({
|
|
2910
|
+
kind: "min",
|
|
2911
|
+
value: BigInt(0),
|
|
2912
|
+
inclusive: true,
|
|
2913
|
+
message: errorUtil.toString(message),
|
|
2914
|
+
});
|
|
2915
|
+
}
|
|
2916
|
+
multipleOf(value, message) {
|
|
2917
|
+
return this._addCheck({
|
|
2918
|
+
kind: "multipleOf",
|
|
2919
|
+
value,
|
|
2920
|
+
message: errorUtil.toString(message),
|
|
2921
|
+
});
|
|
2922
|
+
}
|
|
2923
|
+
get minValue() {
|
|
2924
|
+
let min = null;
|
|
2925
|
+
for (const ch of this._def.checks) {
|
|
2926
|
+
if (ch.kind === "min") {
|
|
2927
|
+
if (min === null || ch.value > min)
|
|
2928
|
+
min = ch.value;
|
|
2929
|
+
}
|
|
2930
|
+
}
|
|
2931
|
+
return min;
|
|
2932
|
+
}
|
|
2933
|
+
get maxValue() {
|
|
2934
|
+
let max = null;
|
|
2935
|
+
for (const ch of this._def.checks) {
|
|
2936
|
+
if (ch.kind === "max") {
|
|
2937
|
+
if (max === null || ch.value < max)
|
|
2938
|
+
max = ch.value;
|
|
2939
|
+
}
|
|
2940
|
+
}
|
|
2941
|
+
return max;
|
|
2942
|
+
}
|
|
2943
|
+
}
|
|
2944
|
+
ZodBigInt.create = (params) => {
|
|
2945
|
+
return new ZodBigInt({
|
|
2946
|
+
checks: [],
|
|
2947
|
+
typeName: ZodFirstPartyTypeKind.ZodBigInt,
|
|
2948
|
+
coerce: params?.coerce ?? false,
|
|
2949
|
+
...processCreateParams(params),
|
|
2950
|
+
});
|
|
2951
|
+
};
|
|
2952
|
+
class ZodBoolean extends ZodType {
|
|
2953
|
+
_parse(input) {
|
|
2954
|
+
if (this._def.coerce) {
|
|
2955
|
+
input.data = Boolean(input.data);
|
|
2956
|
+
}
|
|
2957
|
+
const parsedType = this._getType(input);
|
|
2958
|
+
if (parsedType !== ZodParsedType.boolean) {
|
|
2959
|
+
const ctx = this._getOrReturnCtx(input);
|
|
2960
|
+
addIssueToContext(ctx, {
|
|
2961
|
+
code: ZodIssueCode.invalid_type,
|
|
2962
|
+
expected: ZodParsedType.boolean,
|
|
2963
|
+
received: ctx.parsedType,
|
|
2964
|
+
});
|
|
2965
|
+
return INVALID;
|
|
2966
|
+
}
|
|
2967
|
+
return OK(input.data);
|
|
2968
|
+
}
|
|
2969
|
+
}
|
|
2970
|
+
ZodBoolean.create = (params) => {
|
|
2971
|
+
return new ZodBoolean({
|
|
2972
|
+
typeName: ZodFirstPartyTypeKind.ZodBoolean,
|
|
2973
|
+
coerce: params?.coerce || false,
|
|
2974
|
+
...processCreateParams(params),
|
|
2975
|
+
});
|
|
2976
|
+
};
|
|
2977
|
+
class ZodDate extends ZodType {
|
|
2978
|
+
_parse(input) {
|
|
2979
|
+
if (this._def.coerce) {
|
|
2980
|
+
input.data = new Date(input.data);
|
|
2981
|
+
}
|
|
2982
|
+
const parsedType = this._getType(input);
|
|
2983
|
+
if (parsedType !== ZodParsedType.date) {
|
|
2984
|
+
const ctx = this._getOrReturnCtx(input);
|
|
2985
|
+
addIssueToContext(ctx, {
|
|
2986
|
+
code: ZodIssueCode.invalid_type,
|
|
2987
|
+
expected: ZodParsedType.date,
|
|
2988
|
+
received: ctx.parsedType,
|
|
2989
|
+
});
|
|
2990
|
+
return INVALID;
|
|
2991
|
+
}
|
|
2992
|
+
if (Number.isNaN(input.data.getTime())) {
|
|
2993
|
+
const ctx = this._getOrReturnCtx(input);
|
|
2994
|
+
addIssueToContext(ctx, {
|
|
2995
|
+
code: ZodIssueCode.invalid_date,
|
|
2996
|
+
});
|
|
2997
|
+
return INVALID;
|
|
2998
|
+
}
|
|
2999
|
+
const status = new ParseStatus();
|
|
3000
|
+
let ctx = undefined;
|
|
3001
|
+
for (const check of this._def.checks) {
|
|
3002
|
+
if (check.kind === "min") {
|
|
3003
|
+
if (input.data.getTime() < check.value) {
|
|
3004
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
3005
|
+
addIssueToContext(ctx, {
|
|
3006
|
+
code: ZodIssueCode.too_small,
|
|
3007
|
+
message: check.message,
|
|
3008
|
+
inclusive: true,
|
|
3009
|
+
exact: false,
|
|
3010
|
+
minimum: check.value,
|
|
3011
|
+
type: "date",
|
|
3012
|
+
});
|
|
3013
|
+
status.dirty();
|
|
3014
|
+
}
|
|
3015
|
+
}
|
|
3016
|
+
else if (check.kind === "max") {
|
|
3017
|
+
if (input.data.getTime() > check.value) {
|
|
3018
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
3019
|
+
addIssueToContext(ctx, {
|
|
3020
|
+
code: ZodIssueCode.too_big,
|
|
3021
|
+
message: check.message,
|
|
3022
|
+
inclusive: true,
|
|
3023
|
+
exact: false,
|
|
3024
|
+
maximum: check.value,
|
|
3025
|
+
type: "date",
|
|
3026
|
+
});
|
|
3027
|
+
status.dirty();
|
|
3028
|
+
}
|
|
3029
|
+
}
|
|
3030
|
+
else {
|
|
3031
|
+
util.assertNever(check);
|
|
3032
|
+
}
|
|
3033
|
+
}
|
|
3034
|
+
return {
|
|
3035
|
+
status: status.value,
|
|
3036
|
+
value: new Date(input.data.getTime()),
|
|
3037
|
+
};
|
|
3038
|
+
}
|
|
3039
|
+
_addCheck(check) {
|
|
3040
|
+
return new ZodDate({
|
|
3041
|
+
...this._def,
|
|
3042
|
+
checks: [...this._def.checks, check],
|
|
3043
|
+
});
|
|
3044
|
+
}
|
|
3045
|
+
min(minDate, message) {
|
|
3046
|
+
return this._addCheck({
|
|
3047
|
+
kind: "min",
|
|
3048
|
+
value: minDate.getTime(),
|
|
3049
|
+
message: errorUtil.toString(message),
|
|
3050
|
+
});
|
|
3051
|
+
}
|
|
3052
|
+
max(maxDate, message) {
|
|
3053
|
+
return this._addCheck({
|
|
3054
|
+
kind: "max",
|
|
3055
|
+
value: maxDate.getTime(),
|
|
3056
|
+
message: errorUtil.toString(message),
|
|
3057
|
+
});
|
|
3058
|
+
}
|
|
3059
|
+
get minDate() {
|
|
3060
|
+
let min = null;
|
|
3061
|
+
for (const ch of this._def.checks) {
|
|
3062
|
+
if (ch.kind === "min") {
|
|
3063
|
+
if (min === null || ch.value > min)
|
|
3064
|
+
min = ch.value;
|
|
3065
|
+
}
|
|
3066
|
+
}
|
|
3067
|
+
return min != null ? new Date(min) : null;
|
|
3068
|
+
}
|
|
3069
|
+
get maxDate() {
|
|
3070
|
+
let max = null;
|
|
3071
|
+
for (const ch of this._def.checks) {
|
|
3072
|
+
if (ch.kind === "max") {
|
|
3073
|
+
if (max === null || ch.value < max)
|
|
3074
|
+
max = ch.value;
|
|
3075
|
+
}
|
|
3076
|
+
}
|
|
3077
|
+
return max != null ? new Date(max) : null;
|
|
3078
|
+
}
|
|
3079
|
+
}
|
|
3080
|
+
ZodDate.create = (params) => {
|
|
3081
|
+
return new ZodDate({
|
|
3082
|
+
checks: [],
|
|
3083
|
+
coerce: params?.coerce || false,
|
|
3084
|
+
typeName: ZodFirstPartyTypeKind.ZodDate,
|
|
3085
|
+
...processCreateParams(params),
|
|
3086
|
+
});
|
|
3087
|
+
};
|
|
3088
|
+
class ZodSymbol extends ZodType {
|
|
3089
|
+
_parse(input) {
|
|
3090
|
+
const parsedType = this._getType(input);
|
|
3091
|
+
if (parsedType !== ZodParsedType.symbol) {
|
|
3092
|
+
const ctx = this._getOrReturnCtx(input);
|
|
3093
|
+
addIssueToContext(ctx, {
|
|
3094
|
+
code: ZodIssueCode.invalid_type,
|
|
3095
|
+
expected: ZodParsedType.symbol,
|
|
3096
|
+
received: ctx.parsedType,
|
|
3097
|
+
});
|
|
3098
|
+
return INVALID;
|
|
3099
|
+
}
|
|
3100
|
+
return OK(input.data);
|
|
3101
|
+
}
|
|
3102
|
+
}
|
|
3103
|
+
ZodSymbol.create = (params) => {
|
|
3104
|
+
return new ZodSymbol({
|
|
3105
|
+
typeName: ZodFirstPartyTypeKind.ZodSymbol,
|
|
3106
|
+
...processCreateParams(params),
|
|
3107
|
+
});
|
|
3108
|
+
};
|
|
3109
|
+
class ZodUndefined extends ZodType {
|
|
3110
|
+
_parse(input) {
|
|
3111
|
+
const parsedType = this._getType(input);
|
|
3112
|
+
if (parsedType !== ZodParsedType.undefined) {
|
|
3113
|
+
const ctx = this._getOrReturnCtx(input);
|
|
3114
|
+
addIssueToContext(ctx, {
|
|
3115
|
+
code: ZodIssueCode.invalid_type,
|
|
3116
|
+
expected: ZodParsedType.undefined,
|
|
3117
|
+
received: ctx.parsedType,
|
|
3118
|
+
});
|
|
3119
|
+
return INVALID;
|
|
3120
|
+
}
|
|
3121
|
+
return OK(input.data);
|
|
3122
|
+
}
|
|
3123
|
+
}
|
|
3124
|
+
ZodUndefined.create = (params) => {
|
|
3125
|
+
return new ZodUndefined({
|
|
3126
|
+
typeName: ZodFirstPartyTypeKind.ZodUndefined,
|
|
3127
|
+
...processCreateParams(params),
|
|
3128
|
+
});
|
|
3129
|
+
};
|
|
3130
|
+
class ZodNull extends ZodType {
|
|
3131
|
+
_parse(input) {
|
|
3132
|
+
const parsedType = this._getType(input);
|
|
3133
|
+
if (parsedType !== ZodParsedType.null) {
|
|
3134
|
+
const ctx = this._getOrReturnCtx(input);
|
|
3135
|
+
addIssueToContext(ctx, {
|
|
3136
|
+
code: ZodIssueCode.invalid_type,
|
|
3137
|
+
expected: ZodParsedType.null,
|
|
3138
|
+
received: ctx.parsedType,
|
|
3139
|
+
});
|
|
3140
|
+
return INVALID;
|
|
3141
|
+
}
|
|
3142
|
+
return OK(input.data);
|
|
3143
|
+
}
|
|
3144
|
+
}
|
|
3145
|
+
ZodNull.create = (params) => {
|
|
3146
|
+
return new ZodNull({
|
|
3147
|
+
typeName: ZodFirstPartyTypeKind.ZodNull,
|
|
3148
|
+
...processCreateParams(params),
|
|
3149
|
+
});
|
|
3150
|
+
};
|
|
3151
|
+
class ZodAny extends ZodType {
|
|
3152
|
+
constructor() {
|
|
3153
|
+
super(...arguments);
|
|
3154
|
+
// to prevent instances of other classes from extending ZodAny. this causes issues with catchall in ZodObject.
|
|
3155
|
+
this._any = true;
|
|
3156
|
+
}
|
|
3157
|
+
_parse(input) {
|
|
3158
|
+
return OK(input.data);
|
|
3159
|
+
}
|
|
3160
|
+
}
|
|
3161
|
+
ZodAny.create = (params) => {
|
|
3162
|
+
return new ZodAny({
|
|
3163
|
+
typeName: ZodFirstPartyTypeKind.ZodAny,
|
|
3164
|
+
...processCreateParams(params),
|
|
3165
|
+
});
|
|
3166
|
+
};
|
|
3167
|
+
class ZodUnknown extends ZodType {
|
|
3168
|
+
constructor() {
|
|
3169
|
+
super(...arguments);
|
|
3170
|
+
// required
|
|
3171
|
+
this._unknown = true;
|
|
3172
|
+
}
|
|
3173
|
+
_parse(input) {
|
|
3174
|
+
return OK(input.data);
|
|
3175
|
+
}
|
|
3176
|
+
}
|
|
3177
|
+
ZodUnknown.create = (params) => {
|
|
3178
|
+
return new ZodUnknown({
|
|
3179
|
+
typeName: ZodFirstPartyTypeKind.ZodUnknown,
|
|
3180
|
+
...processCreateParams(params),
|
|
3181
|
+
});
|
|
3182
|
+
};
|
|
3183
|
+
class ZodNever extends ZodType {
|
|
3184
|
+
_parse(input) {
|
|
3185
|
+
const ctx = this._getOrReturnCtx(input);
|
|
3186
|
+
addIssueToContext(ctx, {
|
|
3187
|
+
code: ZodIssueCode.invalid_type,
|
|
3188
|
+
expected: ZodParsedType.never,
|
|
3189
|
+
received: ctx.parsedType,
|
|
3190
|
+
});
|
|
3191
|
+
return INVALID;
|
|
3192
|
+
}
|
|
3193
|
+
}
|
|
3194
|
+
ZodNever.create = (params) => {
|
|
3195
|
+
return new ZodNever({
|
|
3196
|
+
typeName: ZodFirstPartyTypeKind.ZodNever,
|
|
3197
|
+
...processCreateParams(params),
|
|
3198
|
+
});
|
|
3199
|
+
};
|
|
3200
|
+
class ZodVoid extends ZodType {
|
|
3201
|
+
_parse(input) {
|
|
3202
|
+
const parsedType = this._getType(input);
|
|
3203
|
+
if (parsedType !== ZodParsedType.undefined) {
|
|
3204
|
+
const ctx = this._getOrReturnCtx(input);
|
|
3205
|
+
addIssueToContext(ctx, {
|
|
3206
|
+
code: ZodIssueCode.invalid_type,
|
|
3207
|
+
expected: ZodParsedType.void,
|
|
3208
|
+
received: ctx.parsedType,
|
|
3209
|
+
});
|
|
3210
|
+
return INVALID;
|
|
3211
|
+
}
|
|
3212
|
+
return OK(input.data);
|
|
3213
|
+
}
|
|
3214
|
+
}
|
|
3215
|
+
ZodVoid.create = (params) => {
|
|
3216
|
+
return new ZodVoid({
|
|
3217
|
+
typeName: ZodFirstPartyTypeKind.ZodVoid,
|
|
3218
|
+
...processCreateParams(params),
|
|
3219
|
+
});
|
|
3220
|
+
};
|
|
3221
|
+
class ZodArray extends ZodType {
|
|
3222
|
+
_parse(input) {
|
|
3223
|
+
const { ctx, status } = this._processInputParams(input);
|
|
3224
|
+
const def = this._def;
|
|
3225
|
+
if (ctx.parsedType !== ZodParsedType.array) {
|
|
3226
|
+
addIssueToContext(ctx, {
|
|
3227
|
+
code: ZodIssueCode.invalid_type,
|
|
3228
|
+
expected: ZodParsedType.array,
|
|
3229
|
+
received: ctx.parsedType,
|
|
3230
|
+
});
|
|
3231
|
+
return INVALID;
|
|
3232
|
+
}
|
|
3233
|
+
if (def.exactLength !== null) {
|
|
3234
|
+
const tooBig = ctx.data.length > def.exactLength.value;
|
|
3235
|
+
const tooSmall = ctx.data.length < def.exactLength.value;
|
|
3236
|
+
if (tooBig || tooSmall) {
|
|
3237
|
+
addIssueToContext(ctx, {
|
|
3238
|
+
code: tooBig ? ZodIssueCode.too_big : ZodIssueCode.too_small,
|
|
3239
|
+
minimum: (tooSmall ? def.exactLength.value : undefined),
|
|
3240
|
+
maximum: (tooBig ? def.exactLength.value : undefined),
|
|
3241
|
+
type: "array",
|
|
3242
|
+
inclusive: true,
|
|
3243
|
+
exact: true,
|
|
3244
|
+
message: def.exactLength.message,
|
|
3245
|
+
});
|
|
3246
|
+
status.dirty();
|
|
3247
|
+
}
|
|
3248
|
+
}
|
|
3249
|
+
if (def.minLength !== null) {
|
|
3250
|
+
if (ctx.data.length < def.minLength.value) {
|
|
3251
|
+
addIssueToContext(ctx, {
|
|
3252
|
+
code: ZodIssueCode.too_small,
|
|
3253
|
+
minimum: def.minLength.value,
|
|
3254
|
+
type: "array",
|
|
3255
|
+
inclusive: true,
|
|
3256
|
+
exact: false,
|
|
3257
|
+
message: def.minLength.message,
|
|
3258
|
+
});
|
|
3259
|
+
status.dirty();
|
|
3260
|
+
}
|
|
3261
|
+
}
|
|
3262
|
+
if (def.maxLength !== null) {
|
|
3263
|
+
if (ctx.data.length > def.maxLength.value) {
|
|
3264
|
+
addIssueToContext(ctx, {
|
|
3265
|
+
code: ZodIssueCode.too_big,
|
|
3266
|
+
maximum: def.maxLength.value,
|
|
3267
|
+
type: "array",
|
|
3268
|
+
inclusive: true,
|
|
3269
|
+
exact: false,
|
|
3270
|
+
message: def.maxLength.message,
|
|
3271
|
+
});
|
|
3272
|
+
status.dirty();
|
|
3273
|
+
}
|
|
3274
|
+
}
|
|
3275
|
+
if (ctx.common.async) {
|
|
3276
|
+
return Promise.all([...ctx.data].map((item, i) => {
|
|
3277
|
+
return def.type._parseAsync(new ParseInputLazyPath(ctx, item, ctx.path, i));
|
|
3278
|
+
})).then((result) => {
|
|
3279
|
+
return ParseStatus.mergeArray(status, result);
|
|
3280
|
+
});
|
|
3281
|
+
}
|
|
3282
|
+
const result = [...ctx.data].map((item, i) => {
|
|
3283
|
+
return def.type._parseSync(new ParseInputLazyPath(ctx, item, ctx.path, i));
|
|
3284
|
+
});
|
|
3285
|
+
return ParseStatus.mergeArray(status, result);
|
|
3286
|
+
}
|
|
3287
|
+
get element() {
|
|
3288
|
+
return this._def.type;
|
|
3289
|
+
}
|
|
3290
|
+
min(minLength, message) {
|
|
3291
|
+
return new ZodArray({
|
|
3292
|
+
...this._def,
|
|
3293
|
+
minLength: { value: minLength, message: errorUtil.toString(message) },
|
|
3294
|
+
});
|
|
3295
|
+
}
|
|
3296
|
+
max(maxLength, message) {
|
|
3297
|
+
return new ZodArray({
|
|
3298
|
+
...this._def,
|
|
3299
|
+
maxLength: { value: maxLength, message: errorUtil.toString(message) },
|
|
3300
|
+
});
|
|
3301
|
+
}
|
|
3302
|
+
length(len, message) {
|
|
3303
|
+
return new ZodArray({
|
|
3304
|
+
...this._def,
|
|
3305
|
+
exactLength: { value: len, message: errorUtil.toString(message) },
|
|
3306
|
+
});
|
|
3307
|
+
}
|
|
3308
|
+
nonempty(message) {
|
|
3309
|
+
return this.min(1, message);
|
|
3310
|
+
}
|
|
3311
|
+
}
|
|
3312
|
+
ZodArray.create = (schema, params) => {
|
|
3313
|
+
return new ZodArray({
|
|
3314
|
+
type: schema,
|
|
3315
|
+
minLength: null,
|
|
3316
|
+
maxLength: null,
|
|
3317
|
+
exactLength: null,
|
|
3318
|
+
typeName: ZodFirstPartyTypeKind.ZodArray,
|
|
3319
|
+
...processCreateParams(params),
|
|
3320
|
+
});
|
|
3321
|
+
};
|
|
3322
|
+
function deepPartialify(schema) {
|
|
3323
|
+
if (schema instanceof ZodObject) {
|
|
3324
|
+
const newShape = {};
|
|
3325
|
+
for (const key in schema.shape) {
|
|
3326
|
+
const fieldSchema = schema.shape[key];
|
|
3327
|
+
newShape[key] = ZodOptional.create(deepPartialify(fieldSchema));
|
|
3328
|
+
}
|
|
3329
|
+
return new ZodObject({
|
|
3330
|
+
...schema._def,
|
|
3331
|
+
shape: () => newShape,
|
|
3332
|
+
});
|
|
3333
|
+
}
|
|
3334
|
+
else if (schema instanceof ZodArray) {
|
|
3335
|
+
return new ZodArray({
|
|
3336
|
+
...schema._def,
|
|
3337
|
+
type: deepPartialify(schema.element),
|
|
3338
|
+
});
|
|
3339
|
+
}
|
|
3340
|
+
else if (schema instanceof ZodOptional) {
|
|
3341
|
+
return ZodOptional.create(deepPartialify(schema.unwrap()));
|
|
3342
|
+
}
|
|
3343
|
+
else if (schema instanceof ZodNullable) {
|
|
3344
|
+
return ZodNullable.create(deepPartialify(schema.unwrap()));
|
|
3345
|
+
}
|
|
3346
|
+
else if (schema instanceof ZodTuple) {
|
|
3347
|
+
return ZodTuple.create(schema.items.map((item) => deepPartialify(item)));
|
|
3348
|
+
}
|
|
3349
|
+
else {
|
|
3350
|
+
return schema;
|
|
3351
|
+
}
|
|
3352
|
+
}
|
|
3353
|
+
class ZodObject extends ZodType {
|
|
3354
|
+
constructor() {
|
|
3355
|
+
super(...arguments);
|
|
3356
|
+
this._cached = null;
|
|
3357
|
+
/**
|
|
3358
|
+
* @deprecated In most cases, this is no longer needed - unknown properties are now silently stripped.
|
|
3359
|
+
* If you want to pass through unknown properties, use `.passthrough()` instead.
|
|
3360
|
+
*/
|
|
3361
|
+
this.nonstrict = this.passthrough;
|
|
3362
|
+
// extend<
|
|
3363
|
+
// Augmentation extends ZodRawShape,
|
|
3364
|
+
// NewOutput extends util.flatten<{
|
|
3365
|
+
// [k in keyof Augmentation | keyof Output]: k extends keyof Augmentation
|
|
3366
|
+
// ? Augmentation[k]["_output"]
|
|
3367
|
+
// : k extends keyof Output
|
|
3368
|
+
// ? Output[k]
|
|
3369
|
+
// : never;
|
|
3370
|
+
// }>,
|
|
3371
|
+
// NewInput extends util.flatten<{
|
|
3372
|
+
// [k in keyof Augmentation | keyof Input]: k extends keyof Augmentation
|
|
3373
|
+
// ? Augmentation[k]["_input"]
|
|
3374
|
+
// : k extends keyof Input
|
|
3375
|
+
// ? Input[k]
|
|
3376
|
+
// : never;
|
|
3377
|
+
// }>
|
|
3378
|
+
// >(
|
|
3379
|
+
// augmentation: Augmentation
|
|
3380
|
+
// ): ZodObject<
|
|
3381
|
+
// extendShape<T, Augmentation>,
|
|
3382
|
+
// UnknownKeys,
|
|
3383
|
+
// Catchall,
|
|
3384
|
+
// NewOutput,
|
|
3385
|
+
// NewInput
|
|
3386
|
+
// > {
|
|
3387
|
+
// return new ZodObject({
|
|
3388
|
+
// ...this._def,
|
|
3389
|
+
// shape: () => ({
|
|
3390
|
+
// ...this._def.shape(),
|
|
3391
|
+
// ...augmentation,
|
|
3392
|
+
// }),
|
|
3393
|
+
// }) as any;
|
|
3394
|
+
// }
|
|
3395
|
+
/**
|
|
3396
|
+
* @deprecated Use `.extend` instead
|
|
3397
|
+
* */
|
|
3398
|
+
this.augment = this.extend;
|
|
3399
|
+
}
|
|
3400
|
+
_getCached() {
|
|
3401
|
+
if (this._cached !== null)
|
|
3402
|
+
return this._cached;
|
|
3403
|
+
const shape = this._def.shape();
|
|
3404
|
+
const keys = util.objectKeys(shape);
|
|
3405
|
+
this._cached = { shape, keys };
|
|
3406
|
+
return this._cached;
|
|
3407
|
+
}
|
|
3408
|
+
_parse(input) {
|
|
3409
|
+
const parsedType = this._getType(input);
|
|
3410
|
+
if (parsedType !== ZodParsedType.object) {
|
|
3411
|
+
const ctx = this._getOrReturnCtx(input);
|
|
3412
|
+
addIssueToContext(ctx, {
|
|
3413
|
+
code: ZodIssueCode.invalid_type,
|
|
3414
|
+
expected: ZodParsedType.object,
|
|
3415
|
+
received: ctx.parsedType,
|
|
3416
|
+
});
|
|
3417
|
+
return INVALID;
|
|
3418
|
+
}
|
|
3419
|
+
const { status, ctx } = this._processInputParams(input);
|
|
3420
|
+
const { shape, keys: shapeKeys } = this._getCached();
|
|
3421
|
+
const extraKeys = [];
|
|
3422
|
+
if (!(this._def.catchall instanceof ZodNever && this._def.unknownKeys === "strip")) {
|
|
3423
|
+
for (const key in ctx.data) {
|
|
3424
|
+
if (!shapeKeys.includes(key)) {
|
|
3425
|
+
extraKeys.push(key);
|
|
3426
|
+
}
|
|
3427
|
+
}
|
|
3428
|
+
}
|
|
3429
|
+
const pairs = [];
|
|
3430
|
+
for (const key of shapeKeys) {
|
|
3431
|
+
const keyValidator = shape[key];
|
|
3432
|
+
const value = ctx.data[key];
|
|
3433
|
+
pairs.push({
|
|
3434
|
+
key: { status: "valid", value: key },
|
|
3435
|
+
value: keyValidator._parse(new ParseInputLazyPath(ctx, value, ctx.path, key)),
|
|
3436
|
+
alwaysSet: key in ctx.data,
|
|
3437
|
+
});
|
|
3438
|
+
}
|
|
3439
|
+
if (this._def.catchall instanceof ZodNever) {
|
|
3440
|
+
const unknownKeys = this._def.unknownKeys;
|
|
3441
|
+
if (unknownKeys === "passthrough") {
|
|
3442
|
+
for (const key of extraKeys) {
|
|
3443
|
+
pairs.push({
|
|
3444
|
+
key: { status: "valid", value: key },
|
|
3445
|
+
value: { status: "valid", value: ctx.data[key] },
|
|
3446
|
+
});
|
|
3447
|
+
}
|
|
3448
|
+
}
|
|
3449
|
+
else if (unknownKeys === "strict") {
|
|
3450
|
+
if (extraKeys.length > 0) {
|
|
3451
|
+
addIssueToContext(ctx, {
|
|
3452
|
+
code: ZodIssueCode.unrecognized_keys,
|
|
3453
|
+
keys: extraKeys,
|
|
3454
|
+
});
|
|
3455
|
+
status.dirty();
|
|
3456
|
+
}
|
|
3457
|
+
}
|
|
3458
|
+
else if (unknownKeys === "strip") ;
|
|
3459
|
+
else {
|
|
3460
|
+
throw new Error(`Internal ZodObject error: invalid unknownKeys value.`);
|
|
3461
|
+
}
|
|
3462
|
+
}
|
|
3463
|
+
else {
|
|
3464
|
+
// run catchall validation
|
|
3465
|
+
const catchall = this._def.catchall;
|
|
3466
|
+
for (const key of extraKeys) {
|
|
3467
|
+
const value = ctx.data[key];
|
|
3468
|
+
pairs.push({
|
|
3469
|
+
key: { status: "valid", value: key },
|
|
3470
|
+
value: catchall._parse(new ParseInputLazyPath(ctx, value, ctx.path, key) //, ctx.child(key), value, getParsedType(value)
|
|
3471
|
+
),
|
|
3472
|
+
alwaysSet: key in ctx.data,
|
|
3473
|
+
});
|
|
3474
|
+
}
|
|
3475
|
+
}
|
|
3476
|
+
if (ctx.common.async) {
|
|
3477
|
+
return Promise.resolve()
|
|
3478
|
+
.then(async () => {
|
|
3479
|
+
const syncPairs = [];
|
|
3480
|
+
for (const pair of pairs) {
|
|
3481
|
+
const key = await pair.key;
|
|
3482
|
+
const value = await pair.value;
|
|
3483
|
+
syncPairs.push({
|
|
3484
|
+
key,
|
|
3485
|
+
value,
|
|
3486
|
+
alwaysSet: pair.alwaysSet,
|
|
3487
|
+
});
|
|
3488
|
+
}
|
|
3489
|
+
return syncPairs;
|
|
3490
|
+
})
|
|
3491
|
+
.then((syncPairs) => {
|
|
3492
|
+
return ParseStatus.mergeObjectSync(status, syncPairs);
|
|
3493
|
+
});
|
|
3494
|
+
}
|
|
3495
|
+
else {
|
|
3496
|
+
return ParseStatus.mergeObjectSync(status, pairs);
|
|
3497
|
+
}
|
|
3498
|
+
}
|
|
3499
|
+
get shape() {
|
|
3500
|
+
return this._def.shape();
|
|
3501
|
+
}
|
|
3502
|
+
strict(message) {
|
|
3503
|
+
errorUtil.errToObj;
|
|
3504
|
+
return new ZodObject({
|
|
3505
|
+
...this._def,
|
|
3506
|
+
unknownKeys: "strict",
|
|
3507
|
+
...(message !== undefined
|
|
3508
|
+
? {
|
|
3509
|
+
errorMap: (issue, ctx) => {
|
|
3510
|
+
const defaultError = this._def.errorMap?.(issue, ctx).message ?? ctx.defaultError;
|
|
3511
|
+
if (issue.code === "unrecognized_keys")
|
|
3512
|
+
return {
|
|
3513
|
+
message: errorUtil.errToObj(message).message ?? defaultError,
|
|
3514
|
+
};
|
|
3515
|
+
return {
|
|
3516
|
+
message: defaultError,
|
|
3517
|
+
};
|
|
3518
|
+
},
|
|
3519
|
+
}
|
|
3520
|
+
: {}),
|
|
3521
|
+
});
|
|
3522
|
+
}
|
|
3523
|
+
strip() {
|
|
3524
|
+
return new ZodObject({
|
|
3525
|
+
...this._def,
|
|
3526
|
+
unknownKeys: "strip",
|
|
3527
|
+
});
|
|
3528
|
+
}
|
|
3529
|
+
passthrough() {
|
|
3530
|
+
return new ZodObject({
|
|
3531
|
+
...this._def,
|
|
3532
|
+
unknownKeys: "passthrough",
|
|
3533
|
+
});
|
|
3534
|
+
}
|
|
3535
|
+
// const AugmentFactory =
|
|
3536
|
+
// <Def extends ZodObjectDef>(def: Def) =>
|
|
3537
|
+
// <Augmentation extends ZodRawShape>(
|
|
3538
|
+
// augmentation: Augmentation
|
|
3539
|
+
// ): ZodObject<
|
|
3540
|
+
// extendShape<ReturnType<Def["shape"]>, Augmentation>,
|
|
3541
|
+
// Def["unknownKeys"],
|
|
3542
|
+
// Def["catchall"]
|
|
3543
|
+
// > => {
|
|
3544
|
+
// return new ZodObject({
|
|
3545
|
+
// ...def,
|
|
3546
|
+
// shape: () => ({
|
|
3547
|
+
// ...def.shape(),
|
|
3548
|
+
// ...augmentation,
|
|
3549
|
+
// }),
|
|
3550
|
+
// }) as any;
|
|
3551
|
+
// };
|
|
3552
|
+
extend(augmentation) {
|
|
3553
|
+
return new ZodObject({
|
|
3554
|
+
...this._def,
|
|
3555
|
+
shape: () => ({
|
|
3556
|
+
...this._def.shape(),
|
|
3557
|
+
...augmentation,
|
|
3558
|
+
}),
|
|
3559
|
+
});
|
|
3560
|
+
}
|
|
3561
|
+
/**
|
|
3562
|
+
* Prior to zod@1.0.12 there was a bug in the
|
|
3563
|
+
* inferred type of merged objects. Please
|
|
3564
|
+
* upgrade if you are experiencing issues.
|
|
3565
|
+
*/
|
|
3566
|
+
merge(merging) {
|
|
3567
|
+
const merged = new ZodObject({
|
|
3568
|
+
unknownKeys: merging._def.unknownKeys,
|
|
3569
|
+
catchall: merging._def.catchall,
|
|
3570
|
+
shape: () => ({
|
|
3571
|
+
...this._def.shape(),
|
|
3572
|
+
...merging._def.shape(),
|
|
3573
|
+
}),
|
|
3574
|
+
typeName: ZodFirstPartyTypeKind.ZodObject,
|
|
3575
|
+
});
|
|
3576
|
+
return merged;
|
|
3577
|
+
}
|
|
3578
|
+
// merge<
|
|
3579
|
+
// Incoming extends AnyZodObject,
|
|
3580
|
+
// Augmentation extends Incoming["shape"],
|
|
3581
|
+
// NewOutput extends {
|
|
3582
|
+
// [k in keyof Augmentation | keyof Output]: k extends keyof Augmentation
|
|
3583
|
+
// ? Augmentation[k]["_output"]
|
|
3584
|
+
// : k extends keyof Output
|
|
3585
|
+
// ? Output[k]
|
|
3586
|
+
// : never;
|
|
3587
|
+
// },
|
|
3588
|
+
// NewInput extends {
|
|
3589
|
+
// [k in keyof Augmentation | keyof Input]: k extends keyof Augmentation
|
|
3590
|
+
// ? Augmentation[k]["_input"]
|
|
3591
|
+
// : k extends keyof Input
|
|
3592
|
+
// ? Input[k]
|
|
3593
|
+
// : never;
|
|
3594
|
+
// }
|
|
3595
|
+
// >(
|
|
3596
|
+
// merging: Incoming
|
|
3597
|
+
// ): ZodObject<
|
|
3598
|
+
// extendShape<T, ReturnType<Incoming["_def"]["shape"]>>,
|
|
3599
|
+
// Incoming["_def"]["unknownKeys"],
|
|
3600
|
+
// Incoming["_def"]["catchall"],
|
|
3601
|
+
// NewOutput,
|
|
3602
|
+
// NewInput
|
|
3603
|
+
// > {
|
|
3604
|
+
// const merged: any = new ZodObject({
|
|
3605
|
+
// unknownKeys: merging._def.unknownKeys,
|
|
3606
|
+
// catchall: merging._def.catchall,
|
|
3607
|
+
// shape: () =>
|
|
3608
|
+
// objectUtil.mergeShapes(this._def.shape(), merging._def.shape()),
|
|
3609
|
+
// typeName: ZodFirstPartyTypeKind.ZodObject,
|
|
3610
|
+
// }) as any;
|
|
3611
|
+
// return merged;
|
|
3612
|
+
// }
|
|
3613
|
+
setKey(key, schema) {
|
|
3614
|
+
return this.augment({ [key]: schema });
|
|
3615
|
+
}
|
|
3616
|
+
// merge<Incoming extends AnyZodObject>(
|
|
3617
|
+
// merging: Incoming
|
|
3618
|
+
// ): //ZodObject<T & Incoming["_shape"], UnknownKeys, Catchall> = (merging) => {
|
|
3619
|
+
// ZodObject<
|
|
3620
|
+
// extendShape<T, ReturnType<Incoming["_def"]["shape"]>>,
|
|
3621
|
+
// Incoming["_def"]["unknownKeys"],
|
|
3622
|
+
// Incoming["_def"]["catchall"]
|
|
3623
|
+
// > {
|
|
3624
|
+
// // const mergedShape = objectUtil.mergeShapes(
|
|
3625
|
+
// // this._def.shape(),
|
|
3626
|
+
// // merging._def.shape()
|
|
3627
|
+
// // );
|
|
3628
|
+
// const merged: any = new ZodObject({
|
|
3629
|
+
// unknownKeys: merging._def.unknownKeys,
|
|
3630
|
+
// catchall: merging._def.catchall,
|
|
3631
|
+
// shape: () =>
|
|
3632
|
+
// objectUtil.mergeShapes(this._def.shape(), merging._def.shape()),
|
|
3633
|
+
// typeName: ZodFirstPartyTypeKind.ZodObject,
|
|
3634
|
+
// }) as any;
|
|
3635
|
+
// return merged;
|
|
3636
|
+
// }
|
|
3637
|
+
catchall(index) {
|
|
3638
|
+
return new ZodObject({
|
|
3639
|
+
...this._def,
|
|
3640
|
+
catchall: index,
|
|
3641
|
+
});
|
|
3642
|
+
}
|
|
3643
|
+
pick(mask) {
|
|
3644
|
+
const shape = {};
|
|
3645
|
+
for (const key of util.objectKeys(mask)) {
|
|
3646
|
+
if (mask[key] && this.shape[key]) {
|
|
3647
|
+
shape[key] = this.shape[key];
|
|
3648
|
+
}
|
|
3649
|
+
}
|
|
3650
|
+
return new ZodObject({
|
|
3651
|
+
...this._def,
|
|
3652
|
+
shape: () => shape,
|
|
3653
|
+
});
|
|
3654
|
+
}
|
|
3655
|
+
omit(mask) {
|
|
3656
|
+
const shape = {};
|
|
3657
|
+
for (const key of util.objectKeys(this.shape)) {
|
|
3658
|
+
if (!mask[key]) {
|
|
3659
|
+
shape[key] = this.shape[key];
|
|
3660
|
+
}
|
|
3661
|
+
}
|
|
3662
|
+
return new ZodObject({
|
|
3663
|
+
...this._def,
|
|
3664
|
+
shape: () => shape,
|
|
3665
|
+
});
|
|
3666
|
+
}
|
|
3667
|
+
/**
|
|
3668
|
+
* @deprecated
|
|
3669
|
+
*/
|
|
3670
|
+
deepPartial() {
|
|
3671
|
+
return deepPartialify(this);
|
|
3672
|
+
}
|
|
3673
|
+
partial(mask) {
|
|
3674
|
+
const newShape = {};
|
|
3675
|
+
for (const key of util.objectKeys(this.shape)) {
|
|
3676
|
+
const fieldSchema = this.shape[key];
|
|
3677
|
+
if (mask && !mask[key]) {
|
|
3678
|
+
newShape[key] = fieldSchema;
|
|
3679
|
+
}
|
|
3680
|
+
else {
|
|
3681
|
+
newShape[key] = fieldSchema.optional();
|
|
3682
|
+
}
|
|
3683
|
+
}
|
|
3684
|
+
return new ZodObject({
|
|
3685
|
+
...this._def,
|
|
3686
|
+
shape: () => newShape,
|
|
3687
|
+
});
|
|
3688
|
+
}
|
|
3689
|
+
required(mask) {
|
|
3690
|
+
const newShape = {};
|
|
3691
|
+
for (const key of util.objectKeys(this.shape)) {
|
|
3692
|
+
if (mask && !mask[key]) {
|
|
3693
|
+
newShape[key] = this.shape[key];
|
|
3694
|
+
}
|
|
3695
|
+
else {
|
|
3696
|
+
const fieldSchema = this.shape[key];
|
|
3697
|
+
let newField = fieldSchema;
|
|
3698
|
+
while (newField instanceof ZodOptional) {
|
|
3699
|
+
newField = newField._def.innerType;
|
|
3700
|
+
}
|
|
3701
|
+
newShape[key] = newField;
|
|
3702
|
+
}
|
|
3703
|
+
}
|
|
3704
|
+
return new ZodObject({
|
|
3705
|
+
...this._def,
|
|
3706
|
+
shape: () => newShape,
|
|
3707
|
+
});
|
|
3708
|
+
}
|
|
3709
|
+
keyof() {
|
|
3710
|
+
return createZodEnum(util.objectKeys(this.shape));
|
|
3711
|
+
}
|
|
3712
|
+
}
|
|
3713
|
+
ZodObject.create = (shape, params) => {
|
|
3714
|
+
return new ZodObject({
|
|
3715
|
+
shape: () => shape,
|
|
3716
|
+
unknownKeys: "strip",
|
|
3717
|
+
catchall: ZodNever.create(),
|
|
3718
|
+
typeName: ZodFirstPartyTypeKind.ZodObject,
|
|
3719
|
+
...processCreateParams(params),
|
|
3720
|
+
});
|
|
3721
|
+
};
|
|
3722
|
+
ZodObject.strictCreate = (shape, params) => {
|
|
3723
|
+
return new ZodObject({
|
|
3724
|
+
shape: () => shape,
|
|
3725
|
+
unknownKeys: "strict",
|
|
3726
|
+
catchall: ZodNever.create(),
|
|
3727
|
+
typeName: ZodFirstPartyTypeKind.ZodObject,
|
|
3728
|
+
...processCreateParams(params),
|
|
3729
|
+
});
|
|
3730
|
+
};
|
|
3731
|
+
ZodObject.lazycreate = (shape, params) => {
|
|
3732
|
+
return new ZodObject({
|
|
3733
|
+
shape,
|
|
3734
|
+
unknownKeys: "strip",
|
|
3735
|
+
catchall: ZodNever.create(),
|
|
3736
|
+
typeName: ZodFirstPartyTypeKind.ZodObject,
|
|
3737
|
+
...processCreateParams(params),
|
|
3738
|
+
});
|
|
3739
|
+
};
|
|
3740
|
+
class ZodUnion extends ZodType {
|
|
3741
|
+
_parse(input) {
|
|
3742
|
+
const { ctx } = this._processInputParams(input);
|
|
3743
|
+
const options = this._def.options;
|
|
3744
|
+
function handleResults(results) {
|
|
3745
|
+
// return first issue-free validation if it exists
|
|
3746
|
+
for (const result of results) {
|
|
3747
|
+
if (result.result.status === "valid") {
|
|
3748
|
+
return result.result;
|
|
3749
|
+
}
|
|
3750
|
+
}
|
|
3751
|
+
for (const result of results) {
|
|
3752
|
+
if (result.result.status === "dirty") {
|
|
3753
|
+
// add issues from dirty option
|
|
3754
|
+
ctx.common.issues.push(...result.ctx.common.issues);
|
|
3755
|
+
return result.result;
|
|
3756
|
+
}
|
|
3757
|
+
}
|
|
3758
|
+
// return invalid
|
|
3759
|
+
const unionErrors = results.map((result) => new ZodError(result.ctx.common.issues));
|
|
3760
|
+
addIssueToContext(ctx, {
|
|
3761
|
+
code: ZodIssueCode.invalid_union,
|
|
3762
|
+
unionErrors,
|
|
3763
|
+
});
|
|
3764
|
+
return INVALID;
|
|
3765
|
+
}
|
|
3766
|
+
if (ctx.common.async) {
|
|
3767
|
+
return Promise.all(options.map(async (option) => {
|
|
3768
|
+
const childCtx = {
|
|
3769
|
+
...ctx,
|
|
3770
|
+
common: {
|
|
3771
|
+
...ctx.common,
|
|
3772
|
+
issues: [],
|
|
3773
|
+
},
|
|
3774
|
+
parent: null,
|
|
3775
|
+
};
|
|
3776
|
+
return {
|
|
3777
|
+
result: await option._parseAsync({
|
|
3778
|
+
data: ctx.data,
|
|
3779
|
+
path: ctx.path,
|
|
3780
|
+
parent: childCtx,
|
|
3781
|
+
}),
|
|
3782
|
+
ctx: childCtx,
|
|
3783
|
+
};
|
|
3784
|
+
})).then(handleResults);
|
|
3785
|
+
}
|
|
3786
|
+
else {
|
|
3787
|
+
let dirty = undefined;
|
|
3788
|
+
const issues = [];
|
|
3789
|
+
for (const option of options) {
|
|
3790
|
+
const childCtx = {
|
|
3791
|
+
...ctx,
|
|
3792
|
+
common: {
|
|
3793
|
+
...ctx.common,
|
|
3794
|
+
issues: [],
|
|
3795
|
+
},
|
|
3796
|
+
parent: null,
|
|
3797
|
+
};
|
|
3798
|
+
const result = option._parseSync({
|
|
3799
|
+
data: ctx.data,
|
|
3800
|
+
path: ctx.path,
|
|
3801
|
+
parent: childCtx,
|
|
3802
|
+
});
|
|
3803
|
+
if (result.status === "valid") {
|
|
3804
|
+
return result;
|
|
3805
|
+
}
|
|
3806
|
+
else if (result.status === "dirty" && !dirty) {
|
|
3807
|
+
dirty = { result, ctx: childCtx };
|
|
3808
|
+
}
|
|
3809
|
+
if (childCtx.common.issues.length) {
|
|
3810
|
+
issues.push(childCtx.common.issues);
|
|
3811
|
+
}
|
|
3812
|
+
}
|
|
3813
|
+
if (dirty) {
|
|
3814
|
+
ctx.common.issues.push(...dirty.ctx.common.issues);
|
|
3815
|
+
return dirty.result;
|
|
3816
|
+
}
|
|
3817
|
+
const unionErrors = issues.map((issues) => new ZodError(issues));
|
|
3818
|
+
addIssueToContext(ctx, {
|
|
3819
|
+
code: ZodIssueCode.invalid_union,
|
|
3820
|
+
unionErrors,
|
|
3821
|
+
});
|
|
3822
|
+
return INVALID;
|
|
3823
|
+
}
|
|
3824
|
+
}
|
|
3825
|
+
get options() {
|
|
3826
|
+
return this._def.options;
|
|
3827
|
+
}
|
|
3828
|
+
}
|
|
3829
|
+
ZodUnion.create = (types, params) => {
|
|
3830
|
+
return new ZodUnion({
|
|
3831
|
+
options: types,
|
|
3832
|
+
typeName: ZodFirstPartyTypeKind.ZodUnion,
|
|
3833
|
+
...processCreateParams(params),
|
|
3834
|
+
});
|
|
3835
|
+
};
|
|
3836
|
+
/////////////////////////////////////////////////////
|
|
3837
|
+
/////////////////////////////////////////////////////
|
|
3838
|
+
////////// //////////
|
|
3839
|
+
////////// ZodDiscriminatedUnion //////////
|
|
3840
|
+
////////// //////////
|
|
3841
|
+
/////////////////////////////////////////////////////
|
|
3842
|
+
/////////////////////////////////////////////////////
|
|
3843
|
+
const getDiscriminator = (type) => {
|
|
3844
|
+
if (type instanceof ZodLazy) {
|
|
3845
|
+
return getDiscriminator(type.schema);
|
|
3846
|
+
}
|
|
3847
|
+
else if (type instanceof ZodEffects) {
|
|
3848
|
+
return getDiscriminator(type.innerType());
|
|
3849
|
+
}
|
|
3850
|
+
else if (type instanceof ZodLiteral) {
|
|
3851
|
+
return [type.value];
|
|
3852
|
+
}
|
|
3853
|
+
else if (type instanceof ZodEnum) {
|
|
3854
|
+
return type.options;
|
|
3855
|
+
}
|
|
3856
|
+
else if (type instanceof ZodNativeEnum) {
|
|
3857
|
+
// eslint-disable-next-line ban/ban
|
|
3858
|
+
return util.objectValues(type.enum);
|
|
3859
|
+
}
|
|
3860
|
+
else if (type instanceof ZodDefault) {
|
|
3861
|
+
return getDiscriminator(type._def.innerType);
|
|
3862
|
+
}
|
|
3863
|
+
else if (type instanceof ZodUndefined) {
|
|
3864
|
+
return [undefined];
|
|
3865
|
+
}
|
|
3866
|
+
else if (type instanceof ZodNull) {
|
|
3867
|
+
return [null];
|
|
3868
|
+
}
|
|
3869
|
+
else if (type instanceof ZodOptional) {
|
|
3870
|
+
return [undefined, ...getDiscriminator(type.unwrap())];
|
|
3871
|
+
}
|
|
3872
|
+
else if (type instanceof ZodNullable) {
|
|
3873
|
+
return [null, ...getDiscriminator(type.unwrap())];
|
|
3874
|
+
}
|
|
3875
|
+
else if (type instanceof ZodBranded) {
|
|
3876
|
+
return getDiscriminator(type.unwrap());
|
|
3877
|
+
}
|
|
3878
|
+
else if (type instanceof ZodReadonly) {
|
|
3879
|
+
return getDiscriminator(type.unwrap());
|
|
3880
|
+
}
|
|
3881
|
+
else if (type instanceof ZodCatch) {
|
|
3882
|
+
return getDiscriminator(type._def.innerType);
|
|
3883
|
+
}
|
|
3884
|
+
else {
|
|
3885
|
+
return [];
|
|
3886
|
+
}
|
|
3887
|
+
};
|
|
3888
|
+
class ZodDiscriminatedUnion extends ZodType {
|
|
3889
|
+
_parse(input) {
|
|
3890
|
+
const { ctx } = this._processInputParams(input);
|
|
3891
|
+
if (ctx.parsedType !== ZodParsedType.object) {
|
|
3892
|
+
addIssueToContext(ctx, {
|
|
3893
|
+
code: ZodIssueCode.invalid_type,
|
|
3894
|
+
expected: ZodParsedType.object,
|
|
3895
|
+
received: ctx.parsedType,
|
|
3896
|
+
});
|
|
3897
|
+
return INVALID;
|
|
3898
|
+
}
|
|
3899
|
+
const discriminator = this.discriminator;
|
|
3900
|
+
const discriminatorValue = ctx.data[discriminator];
|
|
3901
|
+
const option = this.optionsMap.get(discriminatorValue);
|
|
3902
|
+
if (!option) {
|
|
3903
|
+
addIssueToContext(ctx, {
|
|
3904
|
+
code: ZodIssueCode.invalid_union_discriminator,
|
|
3905
|
+
options: Array.from(this.optionsMap.keys()),
|
|
3906
|
+
path: [discriminator],
|
|
3907
|
+
});
|
|
3908
|
+
return INVALID;
|
|
3909
|
+
}
|
|
3910
|
+
if (ctx.common.async) {
|
|
3911
|
+
return option._parseAsync({
|
|
3912
|
+
data: ctx.data,
|
|
3913
|
+
path: ctx.path,
|
|
3914
|
+
parent: ctx,
|
|
3915
|
+
});
|
|
3916
|
+
}
|
|
3917
|
+
else {
|
|
3918
|
+
return option._parseSync({
|
|
3919
|
+
data: ctx.data,
|
|
3920
|
+
path: ctx.path,
|
|
3921
|
+
parent: ctx,
|
|
3922
|
+
});
|
|
3923
|
+
}
|
|
3924
|
+
}
|
|
3925
|
+
get discriminator() {
|
|
3926
|
+
return this._def.discriminator;
|
|
3927
|
+
}
|
|
3928
|
+
get options() {
|
|
3929
|
+
return this._def.options;
|
|
3930
|
+
}
|
|
3931
|
+
get optionsMap() {
|
|
3932
|
+
return this._def.optionsMap;
|
|
3933
|
+
}
|
|
3934
|
+
/**
|
|
3935
|
+
* The constructor of the discriminated union schema. Its behaviour is very similar to that of the normal z.union() constructor.
|
|
3936
|
+
* However, it only allows a union of objects, all of which need to share a discriminator property. This property must
|
|
3937
|
+
* have a different value for each object in the union.
|
|
3938
|
+
* @param discriminator the name of the discriminator property
|
|
3939
|
+
* @param types an array of object schemas
|
|
3940
|
+
* @param params
|
|
3941
|
+
*/
|
|
3942
|
+
static create(discriminator, options, params) {
|
|
3943
|
+
// Get all the valid discriminator values
|
|
3944
|
+
const optionsMap = new Map();
|
|
3945
|
+
// try {
|
|
3946
|
+
for (const type of options) {
|
|
3947
|
+
const discriminatorValues = getDiscriminator(type.shape[discriminator]);
|
|
3948
|
+
if (!discriminatorValues.length) {
|
|
3949
|
+
throw new Error(`A discriminator value for key \`${discriminator}\` could not be extracted from all schema options`);
|
|
3950
|
+
}
|
|
3951
|
+
for (const value of discriminatorValues) {
|
|
3952
|
+
if (optionsMap.has(value)) {
|
|
3953
|
+
throw new Error(`Discriminator property ${String(discriminator)} has duplicate value ${String(value)}`);
|
|
3954
|
+
}
|
|
3955
|
+
optionsMap.set(value, type);
|
|
3956
|
+
}
|
|
3957
|
+
}
|
|
3958
|
+
return new ZodDiscriminatedUnion({
|
|
3959
|
+
typeName: ZodFirstPartyTypeKind.ZodDiscriminatedUnion,
|
|
3960
|
+
discriminator,
|
|
3961
|
+
options,
|
|
3962
|
+
optionsMap,
|
|
3963
|
+
...processCreateParams(params),
|
|
3964
|
+
});
|
|
3965
|
+
}
|
|
3966
|
+
}
|
|
3967
|
+
function mergeValues(a, b) {
|
|
3968
|
+
const aType = getParsedType(a);
|
|
3969
|
+
const bType = getParsedType(b);
|
|
3970
|
+
if (a === b) {
|
|
3971
|
+
return { valid: true, data: a };
|
|
3972
|
+
}
|
|
3973
|
+
else if (aType === ZodParsedType.object && bType === ZodParsedType.object) {
|
|
3974
|
+
const bKeys = util.objectKeys(b);
|
|
3975
|
+
const sharedKeys = util.objectKeys(a).filter((key) => bKeys.indexOf(key) !== -1);
|
|
3976
|
+
const newObj = { ...a, ...b };
|
|
3977
|
+
for (const key of sharedKeys) {
|
|
3978
|
+
const sharedValue = mergeValues(a[key], b[key]);
|
|
3979
|
+
if (!sharedValue.valid) {
|
|
3980
|
+
return { valid: false };
|
|
3981
|
+
}
|
|
3982
|
+
newObj[key] = sharedValue.data;
|
|
3983
|
+
}
|
|
3984
|
+
return { valid: true, data: newObj };
|
|
3985
|
+
}
|
|
3986
|
+
else if (aType === ZodParsedType.array && bType === ZodParsedType.array) {
|
|
3987
|
+
if (a.length !== b.length) {
|
|
3988
|
+
return { valid: false };
|
|
3989
|
+
}
|
|
3990
|
+
const newArray = [];
|
|
3991
|
+
for (let index = 0; index < a.length; index++) {
|
|
3992
|
+
const itemA = a[index];
|
|
3993
|
+
const itemB = b[index];
|
|
3994
|
+
const sharedValue = mergeValues(itemA, itemB);
|
|
3995
|
+
if (!sharedValue.valid) {
|
|
3996
|
+
return { valid: false };
|
|
3997
|
+
}
|
|
3998
|
+
newArray.push(sharedValue.data);
|
|
3999
|
+
}
|
|
4000
|
+
return { valid: true, data: newArray };
|
|
4001
|
+
}
|
|
4002
|
+
else if (aType === ZodParsedType.date && bType === ZodParsedType.date && +a === +b) {
|
|
4003
|
+
return { valid: true, data: a };
|
|
4004
|
+
}
|
|
4005
|
+
else {
|
|
4006
|
+
return { valid: false };
|
|
4007
|
+
}
|
|
4008
|
+
}
|
|
4009
|
+
class ZodIntersection extends ZodType {
|
|
4010
|
+
_parse(input) {
|
|
4011
|
+
const { status, ctx } = this._processInputParams(input);
|
|
4012
|
+
const handleParsed = (parsedLeft, parsedRight) => {
|
|
4013
|
+
if (isAborted(parsedLeft) || isAborted(parsedRight)) {
|
|
4014
|
+
return INVALID;
|
|
4015
|
+
}
|
|
4016
|
+
const merged = mergeValues(parsedLeft.value, parsedRight.value);
|
|
4017
|
+
if (!merged.valid) {
|
|
4018
|
+
addIssueToContext(ctx, {
|
|
4019
|
+
code: ZodIssueCode.invalid_intersection_types,
|
|
4020
|
+
});
|
|
4021
|
+
return INVALID;
|
|
4022
|
+
}
|
|
4023
|
+
if (isDirty(parsedLeft) || isDirty(parsedRight)) {
|
|
4024
|
+
status.dirty();
|
|
4025
|
+
}
|
|
4026
|
+
return { status: status.value, value: merged.data };
|
|
4027
|
+
};
|
|
4028
|
+
if (ctx.common.async) {
|
|
4029
|
+
return Promise.all([
|
|
4030
|
+
this._def.left._parseAsync({
|
|
4031
|
+
data: ctx.data,
|
|
4032
|
+
path: ctx.path,
|
|
4033
|
+
parent: ctx,
|
|
4034
|
+
}),
|
|
4035
|
+
this._def.right._parseAsync({
|
|
4036
|
+
data: ctx.data,
|
|
4037
|
+
path: ctx.path,
|
|
4038
|
+
parent: ctx,
|
|
4039
|
+
}),
|
|
4040
|
+
]).then(([left, right]) => handleParsed(left, right));
|
|
4041
|
+
}
|
|
4042
|
+
else {
|
|
4043
|
+
return handleParsed(this._def.left._parseSync({
|
|
4044
|
+
data: ctx.data,
|
|
4045
|
+
path: ctx.path,
|
|
4046
|
+
parent: ctx,
|
|
4047
|
+
}), this._def.right._parseSync({
|
|
4048
|
+
data: ctx.data,
|
|
4049
|
+
path: ctx.path,
|
|
4050
|
+
parent: ctx,
|
|
4051
|
+
}));
|
|
4052
|
+
}
|
|
4053
|
+
}
|
|
4054
|
+
}
|
|
4055
|
+
ZodIntersection.create = (left, right, params) => {
|
|
4056
|
+
return new ZodIntersection({
|
|
4057
|
+
left: left,
|
|
4058
|
+
right: right,
|
|
4059
|
+
typeName: ZodFirstPartyTypeKind.ZodIntersection,
|
|
4060
|
+
...processCreateParams(params),
|
|
4061
|
+
});
|
|
4062
|
+
};
|
|
4063
|
+
// type ZodTupleItems = [ZodTypeAny, ...ZodTypeAny[]];
|
|
4064
|
+
class ZodTuple extends ZodType {
|
|
4065
|
+
_parse(input) {
|
|
4066
|
+
const { status, ctx } = this._processInputParams(input);
|
|
4067
|
+
if (ctx.parsedType !== ZodParsedType.array) {
|
|
4068
|
+
addIssueToContext(ctx, {
|
|
4069
|
+
code: ZodIssueCode.invalid_type,
|
|
4070
|
+
expected: ZodParsedType.array,
|
|
4071
|
+
received: ctx.parsedType,
|
|
4072
|
+
});
|
|
4073
|
+
return INVALID;
|
|
4074
|
+
}
|
|
4075
|
+
if (ctx.data.length < this._def.items.length) {
|
|
4076
|
+
addIssueToContext(ctx, {
|
|
4077
|
+
code: ZodIssueCode.too_small,
|
|
4078
|
+
minimum: this._def.items.length,
|
|
4079
|
+
inclusive: true,
|
|
4080
|
+
exact: false,
|
|
4081
|
+
type: "array",
|
|
4082
|
+
});
|
|
4083
|
+
return INVALID;
|
|
4084
|
+
}
|
|
4085
|
+
const rest = this._def.rest;
|
|
4086
|
+
if (!rest && ctx.data.length > this._def.items.length) {
|
|
4087
|
+
addIssueToContext(ctx, {
|
|
4088
|
+
code: ZodIssueCode.too_big,
|
|
4089
|
+
maximum: this._def.items.length,
|
|
4090
|
+
inclusive: true,
|
|
4091
|
+
exact: false,
|
|
4092
|
+
type: "array",
|
|
4093
|
+
});
|
|
4094
|
+
status.dirty();
|
|
4095
|
+
}
|
|
4096
|
+
const items = [...ctx.data]
|
|
4097
|
+
.map((item, itemIndex) => {
|
|
4098
|
+
const schema = this._def.items[itemIndex] || this._def.rest;
|
|
4099
|
+
if (!schema)
|
|
4100
|
+
return null;
|
|
4101
|
+
return schema._parse(new ParseInputLazyPath(ctx, item, ctx.path, itemIndex));
|
|
4102
|
+
})
|
|
4103
|
+
.filter((x) => !!x); // filter nulls
|
|
4104
|
+
if (ctx.common.async) {
|
|
4105
|
+
return Promise.all(items).then((results) => {
|
|
4106
|
+
return ParseStatus.mergeArray(status, results);
|
|
4107
|
+
});
|
|
4108
|
+
}
|
|
4109
|
+
else {
|
|
4110
|
+
return ParseStatus.mergeArray(status, items);
|
|
4111
|
+
}
|
|
4112
|
+
}
|
|
4113
|
+
get items() {
|
|
4114
|
+
return this._def.items;
|
|
4115
|
+
}
|
|
4116
|
+
rest(rest) {
|
|
4117
|
+
return new ZodTuple({
|
|
4118
|
+
...this._def,
|
|
4119
|
+
rest,
|
|
4120
|
+
});
|
|
4121
|
+
}
|
|
4122
|
+
}
|
|
4123
|
+
ZodTuple.create = (schemas, params) => {
|
|
4124
|
+
if (!Array.isArray(schemas)) {
|
|
4125
|
+
throw new Error("You must pass an array of schemas to z.tuple([ ... ])");
|
|
4126
|
+
}
|
|
4127
|
+
return new ZodTuple({
|
|
4128
|
+
items: schemas,
|
|
4129
|
+
typeName: ZodFirstPartyTypeKind.ZodTuple,
|
|
4130
|
+
rest: null,
|
|
4131
|
+
...processCreateParams(params),
|
|
4132
|
+
});
|
|
4133
|
+
};
|
|
4134
|
+
class ZodRecord extends ZodType {
|
|
4135
|
+
get keySchema() {
|
|
4136
|
+
return this._def.keyType;
|
|
4137
|
+
}
|
|
4138
|
+
get valueSchema() {
|
|
4139
|
+
return this._def.valueType;
|
|
4140
|
+
}
|
|
4141
|
+
_parse(input) {
|
|
4142
|
+
const { status, ctx } = this._processInputParams(input);
|
|
4143
|
+
if (ctx.parsedType !== ZodParsedType.object) {
|
|
4144
|
+
addIssueToContext(ctx, {
|
|
4145
|
+
code: ZodIssueCode.invalid_type,
|
|
4146
|
+
expected: ZodParsedType.object,
|
|
4147
|
+
received: ctx.parsedType,
|
|
4148
|
+
});
|
|
4149
|
+
return INVALID;
|
|
4150
|
+
}
|
|
4151
|
+
const pairs = [];
|
|
4152
|
+
const keyType = this._def.keyType;
|
|
4153
|
+
const valueType = this._def.valueType;
|
|
4154
|
+
for (const key in ctx.data) {
|
|
4155
|
+
pairs.push({
|
|
4156
|
+
key: keyType._parse(new ParseInputLazyPath(ctx, key, ctx.path, key)),
|
|
4157
|
+
value: valueType._parse(new ParseInputLazyPath(ctx, ctx.data[key], ctx.path, key)),
|
|
4158
|
+
alwaysSet: key in ctx.data,
|
|
4159
|
+
});
|
|
4160
|
+
}
|
|
4161
|
+
if (ctx.common.async) {
|
|
4162
|
+
return ParseStatus.mergeObjectAsync(status, pairs);
|
|
4163
|
+
}
|
|
4164
|
+
else {
|
|
4165
|
+
return ParseStatus.mergeObjectSync(status, pairs);
|
|
4166
|
+
}
|
|
4167
|
+
}
|
|
4168
|
+
get element() {
|
|
4169
|
+
return this._def.valueType;
|
|
4170
|
+
}
|
|
4171
|
+
static create(first, second, third) {
|
|
4172
|
+
if (second instanceof ZodType) {
|
|
4173
|
+
return new ZodRecord({
|
|
4174
|
+
keyType: first,
|
|
4175
|
+
valueType: second,
|
|
4176
|
+
typeName: ZodFirstPartyTypeKind.ZodRecord,
|
|
4177
|
+
...processCreateParams(third),
|
|
4178
|
+
});
|
|
4179
|
+
}
|
|
4180
|
+
return new ZodRecord({
|
|
4181
|
+
keyType: ZodString.create(),
|
|
4182
|
+
valueType: first,
|
|
4183
|
+
typeName: ZodFirstPartyTypeKind.ZodRecord,
|
|
4184
|
+
...processCreateParams(second),
|
|
4185
|
+
});
|
|
4186
|
+
}
|
|
4187
|
+
}
|
|
4188
|
+
class ZodMap extends ZodType {
|
|
4189
|
+
get keySchema() {
|
|
4190
|
+
return this._def.keyType;
|
|
4191
|
+
}
|
|
4192
|
+
get valueSchema() {
|
|
4193
|
+
return this._def.valueType;
|
|
4194
|
+
}
|
|
4195
|
+
_parse(input) {
|
|
4196
|
+
const { status, ctx } = this._processInputParams(input);
|
|
4197
|
+
if (ctx.parsedType !== ZodParsedType.map) {
|
|
4198
|
+
addIssueToContext(ctx, {
|
|
4199
|
+
code: ZodIssueCode.invalid_type,
|
|
4200
|
+
expected: ZodParsedType.map,
|
|
4201
|
+
received: ctx.parsedType,
|
|
4202
|
+
});
|
|
4203
|
+
return INVALID;
|
|
4204
|
+
}
|
|
4205
|
+
const keyType = this._def.keyType;
|
|
4206
|
+
const valueType = this._def.valueType;
|
|
4207
|
+
const pairs = [...ctx.data.entries()].map(([key, value], index) => {
|
|
4208
|
+
return {
|
|
4209
|
+
key: keyType._parse(new ParseInputLazyPath(ctx, key, ctx.path, [index, "key"])),
|
|
4210
|
+
value: valueType._parse(new ParseInputLazyPath(ctx, value, ctx.path, [index, "value"])),
|
|
4211
|
+
};
|
|
4212
|
+
});
|
|
4213
|
+
if (ctx.common.async) {
|
|
4214
|
+
const finalMap = new Map();
|
|
4215
|
+
return Promise.resolve().then(async () => {
|
|
4216
|
+
for (const pair of pairs) {
|
|
4217
|
+
const key = await pair.key;
|
|
4218
|
+
const value = await pair.value;
|
|
4219
|
+
if (key.status === "aborted" || value.status === "aborted") {
|
|
4220
|
+
return INVALID;
|
|
4221
|
+
}
|
|
4222
|
+
if (key.status === "dirty" || value.status === "dirty") {
|
|
4223
|
+
status.dirty();
|
|
4224
|
+
}
|
|
4225
|
+
finalMap.set(key.value, value.value);
|
|
4226
|
+
}
|
|
4227
|
+
return { status: status.value, value: finalMap };
|
|
4228
|
+
});
|
|
4229
|
+
}
|
|
4230
|
+
else {
|
|
4231
|
+
const finalMap = new Map();
|
|
4232
|
+
for (const pair of pairs) {
|
|
4233
|
+
const key = pair.key;
|
|
4234
|
+
const value = pair.value;
|
|
4235
|
+
if (key.status === "aborted" || value.status === "aborted") {
|
|
4236
|
+
return INVALID;
|
|
4237
|
+
}
|
|
4238
|
+
if (key.status === "dirty" || value.status === "dirty") {
|
|
4239
|
+
status.dirty();
|
|
4240
|
+
}
|
|
4241
|
+
finalMap.set(key.value, value.value);
|
|
4242
|
+
}
|
|
4243
|
+
return { status: status.value, value: finalMap };
|
|
4244
|
+
}
|
|
4245
|
+
}
|
|
4246
|
+
}
|
|
4247
|
+
ZodMap.create = (keyType, valueType, params) => {
|
|
4248
|
+
return new ZodMap({
|
|
4249
|
+
valueType,
|
|
4250
|
+
keyType,
|
|
4251
|
+
typeName: ZodFirstPartyTypeKind.ZodMap,
|
|
4252
|
+
...processCreateParams(params),
|
|
4253
|
+
});
|
|
4254
|
+
};
|
|
4255
|
+
class ZodSet extends ZodType {
|
|
4256
|
+
_parse(input) {
|
|
4257
|
+
const { status, ctx } = this._processInputParams(input);
|
|
4258
|
+
if (ctx.parsedType !== ZodParsedType.set) {
|
|
4259
|
+
addIssueToContext(ctx, {
|
|
4260
|
+
code: ZodIssueCode.invalid_type,
|
|
4261
|
+
expected: ZodParsedType.set,
|
|
4262
|
+
received: ctx.parsedType,
|
|
4263
|
+
});
|
|
4264
|
+
return INVALID;
|
|
4265
|
+
}
|
|
4266
|
+
const def = this._def;
|
|
4267
|
+
if (def.minSize !== null) {
|
|
4268
|
+
if (ctx.data.size < def.minSize.value) {
|
|
4269
|
+
addIssueToContext(ctx, {
|
|
4270
|
+
code: ZodIssueCode.too_small,
|
|
4271
|
+
minimum: def.minSize.value,
|
|
4272
|
+
type: "set",
|
|
4273
|
+
inclusive: true,
|
|
4274
|
+
exact: false,
|
|
4275
|
+
message: def.minSize.message,
|
|
4276
|
+
});
|
|
4277
|
+
status.dirty();
|
|
4278
|
+
}
|
|
4279
|
+
}
|
|
4280
|
+
if (def.maxSize !== null) {
|
|
4281
|
+
if (ctx.data.size > def.maxSize.value) {
|
|
4282
|
+
addIssueToContext(ctx, {
|
|
4283
|
+
code: ZodIssueCode.too_big,
|
|
4284
|
+
maximum: def.maxSize.value,
|
|
4285
|
+
type: "set",
|
|
4286
|
+
inclusive: true,
|
|
4287
|
+
exact: false,
|
|
4288
|
+
message: def.maxSize.message,
|
|
4289
|
+
});
|
|
4290
|
+
status.dirty();
|
|
4291
|
+
}
|
|
4292
|
+
}
|
|
4293
|
+
const valueType = this._def.valueType;
|
|
4294
|
+
function finalizeSet(elements) {
|
|
4295
|
+
const parsedSet = new Set();
|
|
4296
|
+
for (const element of elements) {
|
|
4297
|
+
if (element.status === "aborted")
|
|
4298
|
+
return INVALID;
|
|
4299
|
+
if (element.status === "dirty")
|
|
4300
|
+
status.dirty();
|
|
4301
|
+
parsedSet.add(element.value);
|
|
4302
|
+
}
|
|
4303
|
+
return { status: status.value, value: parsedSet };
|
|
4304
|
+
}
|
|
4305
|
+
const elements = [...ctx.data.values()].map((item, i) => valueType._parse(new ParseInputLazyPath(ctx, item, ctx.path, i)));
|
|
4306
|
+
if (ctx.common.async) {
|
|
4307
|
+
return Promise.all(elements).then((elements) => finalizeSet(elements));
|
|
4308
|
+
}
|
|
4309
|
+
else {
|
|
4310
|
+
return finalizeSet(elements);
|
|
4311
|
+
}
|
|
4312
|
+
}
|
|
4313
|
+
min(minSize, message) {
|
|
4314
|
+
return new ZodSet({
|
|
4315
|
+
...this._def,
|
|
4316
|
+
minSize: { value: minSize, message: errorUtil.toString(message) },
|
|
4317
|
+
});
|
|
4318
|
+
}
|
|
4319
|
+
max(maxSize, message) {
|
|
4320
|
+
return new ZodSet({
|
|
4321
|
+
...this._def,
|
|
4322
|
+
maxSize: { value: maxSize, message: errorUtil.toString(message) },
|
|
4323
|
+
});
|
|
4324
|
+
}
|
|
4325
|
+
size(size, message) {
|
|
4326
|
+
return this.min(size, message).max(size, message);
|
|
4327
|
+
}
|
|
4328
|
+
nonempty(message) {
|
|
4329
|
+
return this.min(1, message);
|
|
4330
|
+
}
|
|
4331
|
+
}
|
|
4332
|
+
ZodSet.create = (valueType, params) => {
|
|
4333
|
+
return new ZodSet({
|
|
4334
|
+
valueType,
|
|
4335
|
+
minSize: null,
|
|
4336
|
+
maxSize: null,
|
|
4337
|
+
typeName: ZodFirstPartyTypeKind.ZodSet,
|
|
4338
|
+
...processCreateParams(params),
|
|
4339
|
+
});
|
|
4340
|
+
};
|
|
4341
|
+
class ZodLazy extends ZodType {
|
|
4342
|
+
get schema() {
|
|
4343
|
+
return this._def.getter();
|
|
4344
|
+
}
|
|
4345
|
+
_parse(input) {
|
|
4346
|
+
const { ctx } = this._processInputParams(input);
|
|
4347
|
+
const lazySchema = this._def.getter();
|
|
4348
|
+
return lazySchema._parse({ data: ctx.data, path: ctx.path, parent: ctx });
|
|
4349
|
+
}
|
|
4350
|
+
}
|
|
4351
|
+
ZodLazy.create = (getter, params) => {
|
|
4352
|
+
return new ZodLazy({
|
|
4353
|
+
getter: getter,
|
|
4354
|
+
typeName: ZodFirstPartyTypeKind.ZodLazy,
|
|
4355
|
+
...processCreateParams(params),
|
|
4356
|
+
});
|
|
4357
|
+
};
|
|
4358
|
+
class ZodLiteral extends ZodType {
|
|
4359
|
+
_parse(input) {
|
|
4360
|
+
if (input.data !== this._def.value) {
|
|
4361
|
+
const ctx = this._getOrReturnCtx(input);
|
|
4362
|
+
addIssueToContext(ctx, {
|
|
4363
|
+
received: ctx.data,
|
|
4364
|
+
code: ZodIssueCode.invalid_literal,
|
|
4365
|
+
expected: this._def.value,
|
|
4366
|
+
});
|
|
4367
|
+
return INVALID;
|
|
4368
|
+
}
|
|
4369
|
+
return { status: "valid", value: input.data };
|
|
4370
|
+
}
|
|
4371
|
+
get value() {
|
|
4372
|
+
return this._def.value;
|
|
4373
|
+
}
|
|
4374
|
+
}
|
|
4375
|
+
ZodLiteral.create = (value, params) => {
|
|
4376
|
+
return new ZodLiteral({
|
|
4377
|
+
value: value,
|
|
4378
|
+
typeName: ZodFirstPartyTypeKind.ZodLiteral,
|
|
4379
|
+
...processCreateParams(params),
|
|
4380
|
+
});
|
|
4381
|
+
};
|
|
4382
|
+
function createZodEnum(values, params) {
|
|
4383
|
+
return new ZodEnum({
|
|
4384
|
+
values,
|
|
4385
|
+
typeName: ZodFirstPartyTypeKind.ZodEnum,
|
|
4386
|
+
...processCreateParams(params),
|
|
4387
|
+
});
|
|
4388
|
+
}
|
|
4389
|
+
class ZodEnum extends ZodType {
|
|
4390
|
+
_parse(input) {
|
|
4391
|
+
if (typeof input.data !== "string") {
|
|
4392
|
+
const ctx = this._getOrReturnCtx(input);
|
|
4393
|
+
const expectedValues = this._def.values;
|
|
4394
|
+
addIssueToContext(ctx, {
|
|
4395
|
+
expected: util.joinValues(expectedValues),
|
|
4396
|
+
received: ctx.parsedType,
|
|
4397
|
+
code: ZodIssueCode.invalid_type,
|
|
4398
|
+
});
|
|
4399
|
+
return INVALID;
|
|
4400
|
+
}
|
|
4401
|
+
if (!this._cache) {
|
|
4402
|
+
this._cache = new Set(this._def.values);
|
|
4403
|
+
}
|
|
4404
|
+
if (!this._cache.has(input.data)) {
|
|
4405
|
+
const ctx = this._getOrReturnCtx(input);
|
|
4406
|
+
const expectedValues = this._def.values;
|
|
4407
|
+
addIssueToContext(ctx, {
|
|
4408
|
+
received: ctx.data,
|
|
4409
|
+
code: ZodIssueCode.invalid_enum_value,
|
|
4410
|
+
options: expectedValues,
|
|
4411
|
+
});
|
|
4412
|
+
return INVALID;
|
|
4413
|
+
}
|
|
4414
|
+
return OK(input.data);
|
|
4415
|
+
}
|
|
4416
|
+
get options() {
|
|
4417
|
+
return this._def.values;
|
|
4418
|
+
}
|
|
4419
|
+
get enum() {
|
|
4420
|
+
const enumValues = {};
|
|
4421
|
+
for (const val of this._def.values) {
|
|
4422
|
+
enumValues[val] = val;
|
|
4423
|
+
}
|
|
4424
|
+
return enumValues;
|
|
4425
|
+
}
|
|
4426
|
+
get Values() {
|
|
4427
|
+
const enumValues = {};
|
|
4428
|
+
for (const val of this._def.values) {
|
|
4429
|
+
enumValues[val] = val;
|
|
4430
|
+
}
|
|
4431
|
+
return enumValues;
|
|
4432
|
+
}
|
|
4433
|
+
get Enum() {
|
|
4434
|
+
const enumValues = {};
|
|
4435
|
+
for (const val of this._def.values) {
|
|
4436
|
+
enumValues[val] = val;
|
|
4437
|
+
}
|
|
4438
|
+
return enumValues;
|
|
4439
|
+
}
|
|
4440
|
+
extract(values, newDef = this._def) {
|
|
4441
|
+
return ZodEnum.create(values, {
|
|
4442
|
+
...this._def,
|
|
4443
|
+
...newDef,
|
|
4444
|
+
});
|
|
4445
|
+
}
|
|
4446
|
+
exclude(values, newDef = this._def) {
|
|
4447
|
+
return ZodEnum.create(this.options.filter((opt) => !values.includes(opt)), {
|
|
4448
|
+
...this._def,
|
|
4449
|
+
...newDef,
|
|
4450
|
+
});
|
|
4451
|
+
}
|
|
4452
|
+
}
|
|
4453
|
+
ZodEnum.create = createZodEnum;
|
|
4454
|
+
class ZodNativeEnum extends ZodType {
|
|
4455
|
+
_parse(input) {
|
|
4456
|
+
const nativeEnumValues = util.getValidEnumValues(this._def.values);
|
|
4457
|
+
const ctx = this._getOrReturnCtx(input);
|
|
4458
|
+
if (ctx.parsedType !== ZodParsedType.string && ctx.parsedType !== ZodParsedType.number) {
|
|
4459
|
+
const expectedValues = util.objectValues(nativeEnumValues);
|
|
4460
|
+
addIssueToContext(ctx, {
|
|
4461
|
+
expected: util.joinValues(expectedValues),
|
|
4462
|
+
received: ctx.parsedType,
|
|
4463
|
+
code: ZodIssueCode.invalid_type,
|
|
4464
|
+
});
|
|
4465
|
+
return INVALID;
|
|
4466
|
+
}
|
|
4467
|
+
if (!this._cache) {
|
|
4468
|
+
this._cache = new Set(util.getValidEnumValues(this._def.values));
|
|
4469
|
+
}
|
|
4470
|
+
if (!this._cache.has(input.data)) {
|
|
4471
|
+
const expectedValues = util.objectValues(nativeEnumValues);
|
|
4472
|
+
addIssueToContext(ctx, {
|
|
4473
|
+
received: ctx.data,
|
|
4474
|
+
code: ZodIssueCode.invalid_enum_value,
|
|
4475
|
+
options: expectedValues,
|
|
4476
|
+
});
|
|
4477
|
+
return INVALID;
|
|
4478
|
+
}
|
|
4479
|
+
return OK(input.data);
|
|
4480
|
+
}
|
|
4481
|
+
get enum() {
|
|
4482
|
+
return this._def.values;
|
|
4483
|
+
}
|
|
4484
|
+
}
|
|
4485
|
+
ZodNativeEnum.create = (values, params) => {
|
|
4486
|
+
return new ZodNativeEnum({
|
|
4487
|
+
values: values,
|
|
4488
|
+
typeName: ZodFirstPartyTypeKind.ZodNativeEnum,
|
|
4489
|
+
...processCreateParams(params),
|
|
4490
|
+
});
|
|
4491
|
+
};
|
|
4492
|
+
class ZodPromise extends ZodType {
|
|
4493
|
+
unwrap() {
|
|
4494
|
+
return this._def.type;
|
|
4495
|
+
}
|
|
4496
|
+
_parse(input) {
|
|
4497
|
+
const { ctx } = this._processInputParams(input);
|
|
4498
|
+
if (ctx.parsedType !== ZodParsedType.promise && ctx.common.async === false) {
|
|
4499
|
+
addIssueToContext(ctx, {
|
|
4500
|
+
code: ZodIssueCode.invalid_type,
|
|
4501
|
+
expected: ZodParsedType.promise,
|
|
4502
|
+
received: ctx.parsedType,
|
|
4503
|
+
});
|
|
4504
|
+
return INVALID;
|
|
4505
|
+
}
|
|
4506
|
+
const promisified = ctx.parsedType === ZodParsedType.promise ? ctx.data : Promise.resolve(ctx.data);
|
|
4507
|
+
return OK(promisified.then((data) => {
|
|
4508
|
+
return this._def.type.parseAsync(data, {
|
|
4509
|
+
path: ctx.path,
|
|
4510
|
+
errorMap: ctx.common.contextualErrorMap,
|
|
4511
|
+
});
|
|
4512
|
+
}));
|
|
4513
|
+
}
|
|
4514
|
+
}
|
|
4515
|
+
ZodPromise.create = (schema, params) => {
|
|
4516
|
+
return new ZodPromise({
|
|
4517
|
+
type: schema,
|
|
4518
|
+
typeName: ZodFirstPartyTypeKind.ZodPromise,
|
|
4519
|
+
...processCreateParams(params),
|
|
4520
|
+
});
|
|
4521
|
+
};
|
|
4522
|
+
class ZodEffects extends ZodType {
|
|
4523
|
+
innerType() {
|
|
4524
|
+
return this._def.schema;
|
|
4525
|
+
}
|
|
4526
|
+
sourceType() {
|
|
4527
|
+
return this._def.schema._def.typeName === ZodFirstPartyTypeKind.ZodEffects
|
|
4528
|
+
? this._def.schema.sourceType()
|
|
4529
|
+
: this._def.schema;
|
|
4530
|
+
}
|
|
4531
|
+
_parse(input) {
|
|
4532
|
+
const { status, ctx } = this._processInputParams(input);
|
|
4533
|
+
const effect = this._def.effect || null;
|
|
4534
|
+
const checkCtx = {
|
|
4535
|
+
addIssue: (arg) => {
|
|
4536
|
+
addIssueToContext(ctx, arg);
|
|
4537
|
+
if (arg.fatal) {
|
|
4538
|
+
status.abort();
|
|
4539
|
+
}
|
|
4540
|
+
else {
|
|
4541
|
+
status.dirty();
|
|
4542
|
+
}
|
|
4543
|
+
},
|
|
4544
|
+
get path() {
|
|
4545
|
+
return ctx.path;
|
|
4546
|
+
},
|
|
4547
|
+
};
|
|
4548
|
+
checkCtx.addIssue = checkCtx.addIssue.bind(checkCtx);
|
|
4549
|
+
if (effect.type === "preprocess") {
|
|
4550
|
+
const processed = effect.transform(ctx.data, checkCtx);
|
|
4551
|
+
if (ctx.common.async) {
|
|
4552
|
+
return Promise.resolve(processed).then(async (processed) => {
|
|
4553
|
+
if (status.value === "aborted")
|
|
4554
|
+
return INVALID;
|
|
4555
|
+
const result = await this._def.schema._parseAsync({
|
|
4556
|
+
data: processed,
|
|
4557
|
+
path: ctx.path,
|
|
4558
|
+
parent: ctx,
|
|
4559
|
+
});
|
|
4560
|
+
if (result.status === "aborted")
|
|
4561
|
+
return INVALID;
|
|
4562
|
+
if (result.status === "dirty")
|
|
4563
|
+
return DIRTY(result.value);
|
|
4564
|
+
if (status.value === "dirty")
|
|
4565
|
+
return DIRTY(result.value);
|
|
4566
|
+
return result;
|
|
4567
|
+
});
|
|
4568
|
+
}
|
|
4569
|
+
else {
|
|
4570
|
+
if (status.value === "aborted")
|
|
4571
|
+
return INVALID;
|
|
4572
|
+
const result = this._def.schema._parseSync({
|
|
4573
|
+
data: processed,
|
|
4574
|
+
path: ctx.path,
|
|
4575
|
+
parent: ctx,
|
|
4576
|
+
});
|
|
4577
|
+
if (result.status === "aborted")
|
|
4578
|
+
return INVALID;
|
|
4579
|
+
if (result.status === "dirty")
|
|
4580
|
+
return DIRTY(result.value);
|
|
4581
|
+
if (status.value === "dirty")
|
|
4582
|
+
return DIRTY(result.value);
|
|
4583
|
+
return result;
|
|
4584
|
+
}
|
|
4585
|
+
}
|
|
4586
|
+
if (effect.type === "refinement") {
|
|
4587
|
+
const executeRefinement = (acc) => {
|
|
4588
|
+
const result = effect.refinement(acc, checkCtx);
|
|
4589
|
+
if (ctx.common.async) {
|
|
4590
|
+
return Promise.resolve(result);
|
|
4591
|
+
}
|
|
4592
|
+
if (result instanceof Promise) {
|
|
4593
|
+
throw new Error("Async refinement encountered during synchronous parse operation. Use .parseAsync instead.");
|
|
4594
|
+
}
|
|
4595
|
+
return acc;
|
|
4596
|
+
};
|
|
4597
|
+
if (ctx.common.async === false) {
|
|
4598
|
+
const inner = this._def.schema._parseSync({
|
|
4599
|
+
data: ctx.data,
|
|
4600
|
+
path: ctx.path,
|
|
4601
|
+
parent: ctx,
|
|
4602
|
+
});
|
|
4603
|
+
if (inner.status === "aborted")
|
|
4604
|
+
return INVALID;
|
|
4605
|
+
if (inner.status === "dirty")
|
|
4606
|
+
status.dirty();
|
|
4607
|
+
// return value is ignored
|
|
4608
|
+
executeRefinement(inner.value);
|
|
4609
|
+
return { status: status.value, value: inner.value };
|
|
4610
|
+
}
|
|
4611
|
+
else {
|
|
4612
|
+
return this._def.schema._parseAsync({ data: ctx.data, path: ctx.path, parent: ctx }).then((inner) => {
|
|
4613
|
+
if (inner.status === "aborted")
|
|
4614
|
+
return INVALID;
|
|
4615
|
+
if (inner.status === "dirty")
|
|
4616
|
+
status.dirty();
|
|
4617
|
+
return executeRefinement(inner.value).then(() => {
|
|
4618
|
+
return { status: status.value, value: inner.value };
|
|
4619
|
+
});
|
|
4620
|
+
});
|
|
4621
|
+
}
|
|
4622
|
+
}
|
|
4623
|
+
if (effect.type === "transform") {
|
|
4624
|
+
if (ctx.common.async === false) {
|
|
4625
|
+
const base = this._def.schema._parseSync({
|
|
4626
|
+
data: ctx.data,
|
|
4627
|
+
path: ctx.path,
|
|
4628
|
+
parent: ctx,
|
|
4629
|
+
});
|
|
4630
|
+
if (!isValid(base))
|
|
4631
|
+
return INVALID;
|
|
4632
|
+
const result = effect.transform(base.value, checkCtx);
|
|
4633
|
+
if (result instanceof Promise) {
|
|
4634
|
+
throw new Error(`Asynchronous transform encountered during synchronous parse operation. Use .parseAsync instead.`);
|
|
4635
|
+
}
|
|
4636
|
+
return { status: status.value, value: result };
|
|
4637
|
+
}
|
|
4638
|
+
else {
|
|
4639
|
+
return this._def.schema._parseAsync({ data: ctx.data, path: ctx.path, parent: ctx }).then((base) => {
|
|
4640
|
+
if (!isValid(base))
|
|
4641
|
+
return INVALID;
|
|
4642
|
+
return Promise.resolve(effect.transform(base.value, checkCtx)).then((result) => ({
|
|
4643
|
+
status: status.value,
|
|
4644
|
+
value: result,
|
|
4645
|
+
}));
|
|
4646
|
+
});
|
|
4647
|
+
}
|
|
4648
|
+
}
|
|
4649
|
+
util.assertNever(effect);
|
|
4650
|
+
}
|
|
4651
|
+
}
|
|
4652
|
+
ZodEffects.create = (schema, effect, params) => {
|
|
4653
|
+
return new ZodEffects({
|
|
4654
|
+
schema,
|
|
4655
|
+
typeName: ZodFirstPartyTypeKind.ZodEffects,
|
|
4656
|
+
effect,
|
|
4657
|
+
...processCreateParams(params),
|
|
4658
|
+
});
|
|
4659
|
+
};
|
|
4660
|
+
ZodEffects.createWithPreprocess = (preprocess, schema, params) => {
|
|
4661
|
+
return new ZodEffects({
|
|
4662
|
+
schema,
|
|
4663
|
+
effect: { type: "preprocess", transform: preprocess },
|
|
4664
|
+
typeName: ZodFirstPartyTypeKind.ZodEffects,
|
|
4665
|
+
...processCreateParams(params),
|
|
4666
|
+
});
|
|
4667
|
+
};
|
|
4668
|
+
class ZodOptional extends ZodType {
|
|
4669
|
+
_parse(input) {
|
|
4670
|
+
const parsedType = this._getType(input);
|
|
4671
|
+
if (parsedType === ZodParsedType.undefined) {
|
|
4672
|
+
return OK(undefined);
|
|
4673
|
+
}
|
|
4674
|
+
return this._def.innerType._parse(input);
|
|
4675
|
+
}
|
|
4676
|
+
unwrap() {
|
|
4677
|
+
return this._def.innerType;
|
|
4678
|
+
}
|
|
4679
|
+
}
|
|
4680
|
+
ZodOptional.create = (type, params) => {
|
|
4681
|
+
return new ZodOptional({
|
|
4682
|
+
innerType: type,
|
|
4683
|
+
typeName: ZodFirstPartyTypeKind.ZodOptional,
|
|
4684
|
+
...processCreateParams(params),
|
|
4685
|
+
});
|
|
4686
|
+
};
|
|
4687
|
+
class ZodNullable extends ZodType {
|
|
4688
|
+
_parse(input) {
|
|
4689
|
+
const parsedType = this._getType(input);
|
|
4690
|
+
if (parsedType === ZodParsedType.null) {
|
|
4691
|
+
return OK(null);
|
|
4692
|
+
}
|
|
4693
|
+
return this._def.innerType._parse(input);
|
|
4694
|
+
}
|
|
4695
|
+
unwrap() {
|
|
4696
|
+
return this._def.innerType;
|
|
4697
|
+
}
|
|
4698
|
+
}
|
|
4699
|
+
ZodNullable.create = (type, params) => {
|
|
4700
|
+
return new ZodNullable({
|
|
4701
|
+
innerType: type,
|
|
4702
|
+
typeName: ZodFirstPartyTypeKind.ZodNullable,
|
|
4703
|
+
...processCreateParams(params),
|
|
4704
|
+
});
|
|
4705
|
+
};
|
|
4706
|
+
class ZodDefault extends ZodType {
|
|
4707
|
+
_parse(input) {
|
|
4708
|
+
const { ctx } = this._processInputParams(input);
|
|
4709
|
+
let data = ctx.data;
|
|
4710
|
+
if (ctx.parsedType === ZodParsedType.undefined) {
|
|
4711
|
+
data = this._def.defaultValue();
|
|
4712
|
+
}
|
|
4713
|
+
return this._def.innerType._parse({
|
|
4714
|
+
data,
|
|
4715
|
+
path: ctx.path,
|
|
4716
|
+
parent: ctx,
|
|
4717
|
+
});
|
|
4718
|
+
}
|
|
4719
|
+
removeDefault() {
|
|
4720
|
+
return this._def.innerType;
|
|
4721
|
+
}
|
|
4722
|
+
}
|
|
4723
|
+
ZodDefault.create = (type, params) => {
|
|
4724
|
+
return new ZodDefault({
|
|
4725
|
+
innerType: type,
|
|
4726
|
+
typeName: ZodFirstPartyTypeKind.ZodDefault,
|
|
4727
|
+
defaultValue: typeof params.default === "function" ? params.default : () => params.default,
|
|
4728
|
+
...processCreateParams(params),
|
|
4729
|
+
});
|
|
4730
|
+
};
|
|
4731
|
+
class ZodCatch extends ZodType {
|
|
4732
|
+
_parse(input) {
|
|
4733
|
+
const { ctx } = this._processInputParams(input);
|
|
4734
|
+
// newCtx is used to not collect issues from inner types in ctx
|
|
4735
|
+
const newCtx = {
|
|
4736
|
+
...ctx,
|
|
4737
|
+
common: {
|
|
4738
|
+
...ctx.common,
|
|
4739
|
+
issues: [],
|
|
4740
|
+
},
|
|
4741
|
+
};
|
|
4742
|
+
const result = this._def.innerType._parse({
|
|
4743
|
+
data: newCtx.data,
|
|
4744
|
+
path: newCtx.path,
|
|
4745
|
+
parent: {
|
|
4746
|
+
...newCtx,
|
|
4747
|
+
},
|
|
4748
|
+
});
|
|
4749
|
+
if (isAsync(result)) {
|
|
4750
|
+
return result.then((result) => {
|
|
4751
|
+
return {
|
|
4752
|
+
status: "valid",
|
|
4753
|
+
value: result.status === "valid"
|
|
4754
|
+
? result.value
|
|
4755
|
+
: this._def.catchValue({
|
|
4756
|
+
get error() {
|
|
4757
|
+
return new ZodError(newCtx.common.issues);
|
|
4758
|
+
},
|
|
4759
|
+
input: newCtx.data,
|
|
4760
|
+
}),
|
|
4761
|
+
};
|
|
4762
|
+
});
|
|
4763
|
+
}
|
|
4764
|
+
else {
|
|
4765
|
+
return {
|
|
4766
|
+
status: "valid",
|
|
4767
|
+
value: result.status === "valid"
|
|
4768
|
+
? result.value
|
|
4769
|
+
: this._def.catchValue({
|
|
4770
|
+
get error() {
|
|
4771
|
+
return new ZodError(newCtx.common.issues);
|
|
4772
|
+
},
|
|
4773
|
+
input: newCtx.data,
|
|
4774
|
+
}),
|
|
4775
|
+
};
|
|
4776
|
+
}
|
|
4777
|
+
}
|
|
4778
|
+
removeCatch() {
|
|
4779
|
+
return this._def.innerType;
|
|
4780
|
+
}
|
|
4781
|
+
}
|
|
4782
|
+
ZodCatch.create = (type, params) => {
|
|
4783
|
+
return new ZodCatch({
|
|
4784
|
+
innerType: type,
|
|
4785
|
+
typeName: ZodFirstPartyTypeKind.ZodCatch,
|
|
4786
|
+
catchValue: typeof params.catch === "function" ? params.catch : () => params.catch,
|
|
4787
|
+
...processCreateParams(params),
|
|
4788
|
+
});
|
|
4789
|
+
};
|
|
4790
|
+
class ZodNaN extends ZodType {
|
|
4791
|
+
_parse(input) {
|
|
4792
|
+
const parsedType = this._getType(input);
|
|
4793
|
+
if (parsedType !== ZodParsedType.nan) {
|
|
4794
|
+
const ctx = this._getOrReturnCtx(input);
|
|
4795
|
+
addIssueToContext(ctx, {
|
|
4796
|
+
code: ZodIssueCode.invalid_type,
|
|
4797
|
+
expected: ZodParsedType.nan,
|
|
4798
|
+
received: ctx.parsedType,
|
|
4799
|
+
});
|
|
4800
|
+
return INVALID;
|
|
4801
|
+
}
|
|
4802
|
+
return { status: "valid", value: input.data };
|
|
4803
|
+
}
|
|
4804
|
+
}
|
|
4805
|
+
ZodNaN.create = (params) => {
|
|
4806
|
+
return new ZodNaN({
|
|
4807
|
+
typeName: ZodFirstPartyTypeKind.ZodNaN,
|
|
4808
|
+
...processCreateParams(params),
|
|
4809
|
+
});
|
|
4810
|
+
};
|
|
4811
|
+
class ZodBranded extends ZodType {
|
|
4812
|
+
_parse(input) {
|
|
4813
|
+
const { ctx } = this._processInputParams(input);
|
|
4814
|
+
const data = ctx.data;
|
|
4815
|
+
return this._def.type._parse({
|
|
4816
|
+
data,
|
|
4817
|
+
path: ctx.path,
|
|
4818
|
+
parent: ctx,
|
|
4819
|
+
});
|
|
4820
|
+
}
|
|
4821
|
+
unwrap() {
|
|
4822
|
+
return this._def.type;
|
|
4823
|
+
}
|
|
4824
|
+
}
|
|
4825
|
+
class ZodPipeline extends ZodType {
|
|
4826
|
+
_parse(input) {
|
|
4827
|
+
const { status, ctx } = this._processInputParams(input);
|
|
4828
|
+
if (ctx.common.async) {
|
|
4829
|
+
const handleAsync = async () => {
|
|
4830
|
+
const inResult = await this._def.in._parseAsync({
|
|
4831
|
+
data: ctx.data,
|
|
4832
|
+
path: ctx.path,
|
|
4833
|
+
parent: ctx,
|
|
4834
|
+
});
|
|
4835
|
+
if (inResult.status === "aborted")
|
|
4836
|
+
return INVALID;
|
|
4837
|
+
if (inResult.status === "dirty") {
|
|
4838
|
+
status.dirty();
|
|
4839
|
+
return DIRTY(inResult.value);
|
|
4840
|
+
}
|
|
4841
|
+
else {
|
|
4842
|
+
return this._def.out._parseAsync({
|
|
4843
|
+
data: inResult.value,
|
|
4844
|
+
path: ctx.path,
|
|
4845
|
+
parent: ctx,
|
|
4846
|
+
});
|
|
4847
|
+
}
|
|
4848
|
+
};
|
|
4849
|
+
return handleAsync();
|
|
4850
|
+
}
|
|
4851
|
+
else {
|
|
4852
|
+
const inResult = this._def.in._parseSync({
|
|
4853
|
+
data: ctx.data,
|
|
4854
|
+
path: ctx.path,
|
|
4855
|
+
parent: ctx,
|
|
4856
|
+
});
|
|
4857
|
+
if (inResult.status === "aborted")
|
|
4858
|
+
return INVALID;
|
|
4859
|
+
if (inResult.status === "dirty") {
|
|
4860
|
+
status.dirty();
|
|
4861
|
+
return {
|
|
4862
|
+
status: "dirty",
|
|
4863
|
+
value: inResult.value,
|
|
4864
|
+
};
|
|
4865
|
+
}
|
|
4866
|
+
else {
|
|
4867
|
+
return this._def.out._parseSync({
|
|
4868
|
+
data: inResult.value,
|
|
4869
|
+
path: ctx.path,
|
|
4870
|
+
parent: ctx,
|
|
4871
|
+
});
|
|
4872
|
+
}
|
|
4873
|
+
}
|
|
4874
|
+
}
|
|
4875
|
+
static create(a, b) {
|
|
4876
|
+
return new ZodPipeline({
|
|
4877
|
+
in: a,
|
|
4878
|
+
out: b,
|
|
4879
|
+
typeName: ZodFirstPartyTypeKind.ZodPipeline,
|
|
4880
|
+
});
|
|
4881
|
+
}
|
|
4882
|
+
}
|
|
4883
|
+
class ZodReadonly extends ZodType {
|
|
4884
|
+
_parse(input) {
|
|
4885
|
+
const result = this._def.innerType._parse(input);
|
|
4886
|
+
const freeze = (data) => {
|
|
4887
|
+
if (isValid(data)) {
|
|
4888
|
+
data.value = Object.freeze(data.value);
|
|
4889
|
+
}
|
|
4890
|
+
return data;
|
|
4891
|
+
};
|
|
4892
|
+
return isAsync(result) ? result.then((data) => freeze(data)) : freeze(result);
|
|
4893
|
+
}
|
|
4894
|
+
unwrap() {
|
|
4895
|
+
return this._def.innerType;
|
|
4896
|
+
}
|
|
4897
|
+
}
|
|
4898
|
+
ZodReadonly.create = (type, params) => {
|
|
4899
|
+
return new ZodReadonly({
|
|
4900
|
+
innerType: type,
|
|
4901
|
+
typeName: ZodFirstPartyTypeKind.ZodReadonly,
|
|
4902
|
+
...processCreateParams(params),
|
|
4903
|
+
});
|
|
4904
|
+
};
|
|
4905
|
+
var ZodFirstPartyTypeKind;
|
|
4906
|
+
(function (ZodFirstPartyTypeKind) {
|
|
4907
|
+
ZodFirstPartyTypeKind["ZodString"] = "ZodString";
|
|
4908
|
+
ZodFirstPartyTypeKind["ZodNumber"] = "ZodNumber";
|
|
4909
|
+
ZodFirstPartyTypeKind["ZodNaN"] = "ZodNaN";
|
|
4910
|
+
ZodFirstPartyTypeKind["ZodBigInt"] = "ZodBigInt";
|
|
4911
|
+
ZodFirstPartyTypeKind["ZodBoolean"] = "ZodBoolean";
|
|
4912
|
+
ZodFirstPartyTypeKind["ZodDate"] = "ZodDate";
|
|
4913
|
+
ZodFirstPartyTypeKind["ZodSymbol"] = "ZodSymbol";
|
|
4914
|
+
ZodFirstPartyTypeKind["ZodUndefined"] = "ZodUndefined";
|
|
4915
|
+
ZodFirstPartyTypeKind["ZodNull"] = "ZodNull";
|
|
4916
|
+
ZodFirstPartyTypeKind["ZodAny"] = "ZodAny";
|
|
4917
|
+
ZodFirstPartyTypeKind["ZodUnknown"] = "ZodUnknown";
|
|
4918
|
+
ZodFirstPartyTypeKind["ZodNever"] = "ZodNever";
|
|
4919
|
+
ZodFirstPartyTypeKind["ZodVoid"] = "ZodVoid";
|
|
4920
|
+
ZodFirstPartyTypeKind["ZodArray"] = "ZodArray";
|
|
4921
|
+
ZodFirstPartyTypeKind["ZodObject"] = "ZodObject";
|
|
4922
|
+
ZodFirstPartyTypeKind["ZodUnion"] = "ZodUnion";
|
|
4923
|
+
ZodFirstPartyTypeKind["ZodDiscriminatedUnion"] = "ZodDiscriminatedUnion";
|
|
4924
|
+
ZodFirstPartyTypeKind["ZodIntersection"] = "ZodIntersection";
|
|
4925
|
+
ZodFirstPartyTypeKind["ZodTuple"] = "ZodTuple";
|
|
4926
|
+
ZodFirstPartyTypeKind["ZodRecord"] = "ZodRecord";
|
|
4927
|
+
ZodFirstPartyTypeKind["ZodMap"] = "ZodMap";
|
|
4928
|
+
ZodFirstPartyTypeKind["ZodSet"] = "ZodSet";
|
|
4929
|
+
ZodFirstPartyTypeKind["ZodFunction"] = "ZodFunction";
|
|
4930
|
+
ZodFirstPartyTypeKind["ZodLazy"] = "ZodLazy";
|
|
4931
|
+
ZodFirstPartyTypeKind["ZodLiteral"] = "ZodLiteral";
|
|
4932
|
+
ZodFirstPartyTypeKind["ZodEnum"] = "ZodEnum";
|
|
4933
|
+
ZodFirstPartyTypeKind["ZodEffects"] = "ZodEffects";
|
|
4934
|
+
ZodFirstPartyTypeKind["ZodNativeEnum"] = "ZodNativeEnum";
|
|
4935
|
+
ZodFirstPartyTypeKind["ZodOptional"] = "ZodOptional";
|
|
4936
|
+
ZodFirstPartyTypeKind["ZodNullable"] = "ZodNullable";
|
|
4937
|
+
ZodFirstPartyTypeKind["ZodDefault"] = "ZodDefault";
|
|
4938
|
+
ZodFirstPartyTypeKind["ZodCatch"] = "ZodCatch";
|
|
4939
|
+
ZodFirstPartyTypeKind["ZodPromise"] = "ZodPromise";
|
|
4940
|
+
ZodFirstPartyTypeKind["ZodBranded"] = "ZodBranded";
|
|
4941
|
+
ZodFirstPartyTypeKind["ZodPipeline"] = "ZodPipeline";
|
|
4942
|
+
ZodFirstPartyTypeKind["ZodReadonly"] = "ZodReadonly";
|
|
4943
|
+
})(ZodFirstPartyTypeKind || (ZodFirstPartyTypeKind = {}));
|
|
4944
|
+
const stringType = ZodString.create;
|
|
4945
|
+
const numberType = ZodNumber.create;
|
|
4946
|
+
const booleanType = ZodBoolean.create;
|
|
4947
|
+
const anyType = ZodAny.create;
|
|
4948
|
+
ZodNever.create;
|
|
4949
|
+
const arrayType = ZodArray.create;
|
|
4950
|
+
const objectType = ZodObject.create;
|
|
4951
|
+
ZodUnion.create;
|
|
4952
|
+
const discriminatedUnionType = ZodDiscriminatedUnion.create;
|
|
4953
|
+
ZodIntersection.create;
|
|
4954
|
+
ZodTuple.create;
|
|
4955
|
+
const recordType = ZodRecord.create;
|
|
4956
|
+
const literalType = ZodLiteral.create;
|
|
4957
|
+
const enumType = ZodEnum.create;
|
|
4958
|
+
ZodPromise.create;
|
|
4959
|
+
ZodOptional.create;
|
|
4960
|
+
ZodNullable.create;
|
|
4961
|
+
|
|
4962
|
+
var PaymentSchema = objectType({
|
|
4963
|
+
payload_type: literalType("Payment"),
|
|
4964
|
+
billing: objectType({
|
|
4965
|
+
city: stringType().nullable(),
|
|
4966
|
+
country: stringType().nullable(),
|
|
4967
|
+
state: stringType().nullable(),
|
|
4968
|
+
street: stringType().nullable(),
|
|
4969
|
+
zipcode: stringType().nullable(),
|
|
1001
4970
|
}),
|
|
1002
|
-
brand_id:
|
|
1003
|
-
business_id:
|
|
1004
|
-
card_issuing_country:
|
|
1005
|
-
card_last_four:
|
|
1006
|
-
card_network:
|
|
1007
|
-
card_type:
|
|
1008
|
-
created_at:
|
|
1009
|
-
currency:
|
|
1010
|
-
customer:
|
|
1011
|
-
customer_id:
|
|
1012
|
-
email:
|
|
1013
|
-
name:
|
|
4971
|
+
brand_id: stringType(),
|
|
4972
|
+
business_id: stringType(),
|
|
4973
|
+
card_issuing_country: stringType().nullable(),
|
|
4974
|
+
card_last_four: stringType().nullable(),
|
|
4975
|
+
card_network: stringType().nullable(),
|
|
4976
|
+
card_type: stringType().nullable(),
|
|
4977
|
+
created_at: stringType().transform(function (d) { return new Date(d); }),
|
|
4978
|
+
currency: stringType(),
|
|
4979
|
+
customer: objectType({
|
|
4980
|
+
customer_id: stringType(),
|
|
4981
|
+
email: stringType(),
|
|
4982
|
+
name: stringType().nullable(),
|
|
1014
4983
|
}),
|
|
1015
|
-
digital_products_delivered:
|
|
1016
|
-
discount_id:
|
|
1017
|
-
disputes:
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
dispute_stage: zod.z.enum([
|
|
4984
|
+
digital_products_delivered: booleanType(),
|
|
4985
|
+
discount_id: stringType().nullable(),
|
|
4986
|
+
disputes: arrayType(objectType({
|
|
4987
|
+
amount: stringType(),
|
|
4988
|
+
business_id: stringType(),
|
|
4989
|
+
created_at: stringType().transform(function (d) { return new Date(d); }),
|
|
4990
|
+
currency: stringType(),
|
|
4991
|
+
dispute_id: stringType(),
|
|
4992
|
+
dispute_stage: enumType([
|
|
1025
4993
|
"pre_dispute",
|
|
1026
4994
|
"dispute_opened",
|
|
1027
4995
|
"dispute_won",
|
|
1028
4996
|
"dispute_lost",
|
|
1029
4997
|
]),
|
|
1030
|
-
dispute_status:
|
|
4998
|
+
dispute_status: enumType([
|
|
1031
4999
|
"dispute_opened",
|
|
1032
5000
|
"dispute_won",
|
|
1033
5001
|
"dispute_lost",
|
|
@@ -1035,92 +5003,85 @@ var PaymentSchema = zod.z.object({
|
|
|
1035
5003
|
"dispute_cancelled",
|
|
1036
5004
|
"dispute_challenged",
|
|
1037
5005
|
]),
|
|
1038
|
-
payment_id:
|
|
1039
|
-
remarks:
|
|
5006
|
+
payment_id: stringType(),
|
|
5007
|
+
remarks: stringType().nullable(),
|
|
1040
5008
|
}))
|
|
1041
5009
|
.nullable(),
|
|
1042
|
-
error_code:
|
|
1043
|
-
error_message:
|
|
1044
|
-
metadata:
|
|
1045
|
-
payment_id:
|
|
1046
|
-
payment_link:
|
|
1047
|
-
payment_method:
|
|
1048
|
-
payment_method_type:
|
|
1049
|
-
product_cart:
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
quantity: zod.z.number(),
|
|
5010
|
+
error_code: stringType().nullable(),
|
|
5011
|
+
error_message: stringType().nullable(),
|
|
5012
|
+
metadata: recordType(anyType()).nullable(),
|
|
5013
|
+
payment_id: stringType(),
|
|
5014
|
+
payment_link: stringType().nullable(),
|
|
5015
|
+
payment_method: stringType().nullable(),
|
|
5016
|
+
payment_method_type: stringType().nullable(),
|
|
5017
|
+
product_cart: arrayType(objectType({
|
|
5018
|
+
product_id: stringType(),
|
|
5019
|
+
quantity: numberType(),
|
|
1053
5020
|
}))
|
|
1054
5021
|
.nullable(),
|
|
1055
|
-
refunds:
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
status: zod.z.enum(["succeeded", "failed", "pending"]),
|
|
5022
|
+
refunds: arrayType(objectType({
|
|
5023
|
+
amount: numberType(),
|
|
5024
|
+
business_id: stringType(),
|
|
5025
|
+
created_at: stringType().transform(function (d) { return new Date(d); }),
|
|
5026
|
+
currency: stringType(),
|
|
5027
|
+
is_partial: booleanType(),
|
|
5028
|
+
payment_id: stringType(),
|
|
5029
|
+
reason: stringType().nullable(),
|
|
5030
|
+
refund_id: stringType(),
|
|
5031
|
+
status: enumType(["succeeded", "failed", "pending"]),
|
|
1066
5032
|
}))
|
|
1067
5033
|
.nullable(),
|
|
1068
|
-
settlement_amount:
|
|
1069
|
-
settlement_currency:
|
|
1070
|
-
settlement_tax:
|
|
1071
|
-
status:
|
|
1072
|
-
subscription_id:
|
|
1073
|
-
tax:
|
|
1074
|
-
total_amount:
|
|
1075
|
-
updated_at:
|
|
1076
|
-
.string()
|
|
5034
|
+
settlement_amount: numberType(),
|
|
5035
|
+
settlement_currency: stringType(),
|
|
5036
|
+
settlement_tax: numberType().nullable(),
|
|
5037
|
+
status: enumType(["succeeded", "failed", "pending", "processing", "cancelled"]),
|
|
5038
|
+
subscription_id: stringType().nullable(),
|
|
5039
|
+
tax: numberType().nullable(),
|
|
5040
|
+
total_amount: numberType(),
|
|
5041
|
+
updated_at: stringType()
|
|
1077
5042
|
.transform(function (d) { return new Date(d); })
|
|
1078
5043
|
.nullable(),
|
|
1079
5044
|
});
|
|
1080
|
-
var SubscriptionSchema =
|
|
1081
|
-
payload_type:
|
|
1082
|
-
addons:
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
quantity: zod.z.number(),
|
|
5045
|
+
var SubscriptionSchema = objectType({
|
|
5046
|
+
payload_type: literalType("Subscription"),
|
|
5047
|
+
addons: arrayType(objectType({
|
|
5048
|
+
addon_id: stringType(),
|
|
5049
|
+
quantity: numberType(),
|
|
1086
5050
|
}))
|
|
1087
5051
|
.nullable(),
|
|
1088
|
-
billing:
|
|
1089
|
-
city:
|
|
1090
|
-
country:
|
|
1091
|
-
state:
|
|
1092
|
-
street:
|
|
1093
|
-
zipcode:
|
|
5052
|
+
billing: objectType({
|
|
5053
|
+
city: stringType().nullable(),
|
|
5054
|
+
country: stringType().nullable(),
|
|
5055
|
+
state: stringType().nullable(),
|
|
5056
|
+
street: stringType().nullable(),
|
|
5057
|
+
zipcode: stringType().nullable(),
|
|
1094
5058
|
}),
|
|
1095
|
-
cancel_at_next_billing_date:
|
|
1096
|
-
cancelled_at:
|
|
1097
|
-
.string()
|
|
5059
|
+
cancel_at_next_billing_date: booleanType(),
|
|
5060
|
+
cancelled_at: stringType()
|
|
1098
5061
|
.transform(function (d) { return new Date(d); })
|
|
1099
5062
|
.nullable(),
|
|
1100
|
-
created_at:
|
|
1101
|
-
currency:
|
|
1102
|
-
customer:
|
|
1103
|
-
customer_id:
|
|
1104
|
-
email:
|
|
1105
|
-
name:
|
|
5063
|
+
created_at: stringType().transform(function (d) { return new Date(d); }),
|
|
5064
|
+
currency: stringType(),
|
|
5065
|
+
customer: objectType({
|
|
5066
|
+
customer_id: stringType(),
|
|
5067
|
+
email: stringType(),
|
|
5068
|
+
name: stringType().nullable(),
|
|
1106
5069
|
}),
|
|
1107
|
-
discount_id:
|
|
1108
|
-
metadata:
|
|
1109
|
-
next_billing_date:
|
|
1110
|
-
.string()
|
|
5070
|
+
discount_id: stringType().nullable(),
|
|
5071
|
+
metadata: recordType(anyType()).nullable(),
|
|
5072
|
+
next_billing_date: stringType()
|
|
1111
5073
|
.transform(function (d) { return new Date(d); })
|
|
1112
5074
|
.nullable(),
|
|
1113
|
-
on_demand:
|
|
1114
|
-
payment_frequency_count:
|
|
1115
|
-
payment_frequency_interval:
|
|
1116
|
-
previous_billing_date:
|
|
1117
|
-
.string()
|
|
5075
|
+
on_demand: booleanType(),
|
|
5076
|
+
payment_frequency_count: numberType(),
|
|
5077
|
+
payment_frequency_interval: enumType(["Day", "Week", "Month", "Year"]),
|
|
5078
|
+
previous_billing_date: stringType()
|
|
1118
5079
|
.transform(function (d) { return new Date(d); })
|
|
1119
5080
|
.nullable(),
|
|
1120
|
-
product_id:
|
|
1121
|
-
quantity:
|
|
1122
|
-
recurring_pre_tax_amount:
|
|
1123
|
-
status:
|
|
5081
|
+
product_id: stringType(),
|
|
5082
|
+
quantity: numberType(),
|
|
5083
|
+
recurring_pre_tax_amount: numberType(),
|
|
5084
|
+
status: enumType([
|
|
1124
5085
|
"pending",
|
|
1125
5086
|
"active",
|
|
1126
5087
|
"on_hold",
|
|
@@ -1129,38 +5090,38 @@ var SubscriptionSchema = zod.z.object({
|
|
|
1129
5090
|
"expired",
|
|
1130
5091
|
"failed",
|
|
1131
5092
|
]),
|
|
1132
|
-
subscription_id:
|
|
1133
|
-
subscription_period_count:
|
|
1134
|
-
subscription_period_interval:
|
|
1135
|
-
tax_inclusive:
|
|
1136
|
-
trial_period_days:
|
|
5093
|
+
subscription_id: stringType(),
|
|
5094
|
+
subscription_period_count: numberType(),
|
|
5095
|
+
subscription_period_interval: enumType(["Day", "Week", "Month", "Year"]),
|
|
5096
|
+
tax_inclusive: booleanType(),
|
|
5097
|
+
trial_period_days: numberType(),
|
|
1137
5098
|
});
|
|
1138
|
-
var RefundSchema =
|
|
1139
|
-
payload_type:
|
|
1140
|
-
amount:
|
|
1141
|
-
business_id:
|
|
1142
|
-
created_at:
|
|
1143
|
-
currency:
|
|
1144
|
-
is_partial:
|
|
1145
|
-
payment_id:
|
|
1146
|
-
reason:
|
|
1147
|
-
refund_id:
|
|
1148
|
-
status:
|
|
5099
|
+
var RefundSchema = objectType({
|
|
5100
|
+
payload_type: literalType("Refund"),
|
|
5101
|
+
amount: numberType(),
|
|
5102
|
+
business_id: stringType(),
|
|
5103
|
+
created_at: stringType().transform(function (d) { return new Date(d); }),
|
|
5104
|
+
currency: stringType(),
|
|
5105
|
+
is_partial: booleanType(),
|
|
5106
|
+
payment_id: stringType(),
|
|
5107
|
+
reason: stringType().nullable(),
|
|
5108
|
+
refund_id: stringType(),
|
|
5109
|
+
status: enumType(["succeeded", "failed", "pending"]),
|
|
1149
5110
|
});
|
|
1150
|
-
var DisputeSchema =
|
|
1151
|
-
payload_type:
|
|
1152
|
-
amount:
|
|
1153
|
-
business_id:
|
|
1154
|
-
created_at:
|
|
1155
|
-
currency:
|
|
1156
|
-
dispute_id:
|
|
1157
|
-
dispute_stage:
|
|
5111
|
+
var DisputeSchema = objectType({
|
|
5112
|
+
payload_type: literalType("Dispute"),
|
|
5113
|
+
amount: stringType(),
|
|
5114
|
+
business_id: stringType(),
|
|
5115
|
+
created_at: stringType().transform(function (d) { return new Date(d); }),
|
|
5116
|
+
currency: stringType(),
|
|
5117
|
+
dispute_id: stringType(),
|
|
5118
|
+
dispute_stage: enumType([
|
|
1158
5119
|
"pre_dispute",
|
|
1159
5120
|
"dispute_opened",
|
|
1160
5121
|
"dispute_won",
|
|
1161
5122
|
"dispute_lost",
|
|
1162
5123
|
]),
|
|
1163
|
-
dispute_status:
|
|
5124
|
+
dispute_status: enumType([
|
|
1164
5125
|
"dispute_opened",
|
|
1165
5126
|
"dispute_won",
|
|
1166
5127
|
"dispute_lost",
|
|
@@ -1168,160 +5129,159 @@ var DisputeSchema = zod.z.object({
|
|
|
1168
5129
|
"dispute_cancelled",
|
|
1169
5130
|
"dispute_challenged",
|
|
1170
5131
|
]),
|
|
1171
|
-
payment_id:
|
|
1172
|
-
remarks:
|
|
5132
|
+
payment_id: stringType(),
|
|
5133
|
+
remarks: stringType().nullable(),
|
|
1173
5134
|
});
|
|
1174
|
-
var LicenseKeySchema =
|
|
1175
|
-
payload_type:
|
|
1176
|
-
activations_limit:
|
|
1177
|
-
business_id:
|
|
1178
|
-
created_at:
|
|
1179
|
-
customer_id:
|
|
1180
|
-
expires_at:
|
|
1181
|
-
.string()
|
|
5135
|
+
var LicenseKeySchema = objectType({
|
|
5136
|
+
payload_type: literalType("LicenseKey"),
|
|
5137
|
+
activations_limit: numberType(),
|
|
5138
|
+
business_id: stringType(),
|
|
5139
|
+
created_at: stringType().transform(function (d) { return new Date(d); }),
|
|
5140
|
+
customer_id: stringType(),
|
|
5141
|
+
expires_at: stringType()
|
|
1182
5142
|
.transform(function (d) { return new Date(d); })
|
|
1183
5143
|
.nullable(),
|
|
1184
|
-
id:
|
|
1185
|
-
instances_count:
|
|
1186
|
-
key:
|
|
1187
|
-
payment_id:
|
|
1188
|
-
product_id:
|
|
1189
|
-
status:
|
|
1190
|
-
subscription_id:
|
|
5144
|
+
id: stringType(),
|
|
5145
|
+
instances_count: numberType(),
|
|
5146
|
+
key: stringType(),
|
|
5147
|
+
payment_id: stringType(),
|
|
5148
|
+
product_id: stringType(),
|
|
5149
|
+
status: enumType(["active", "inactive", "expired"]),
|
|
5150
|
+
subscription_id: stringType().nullable(),
|
|
1191
5151
|
});
|
|
1192
|
-
var PaymentSucceededPayloadSchema =
|
|
1193
|
-
business_id:
|
|
1194
|
-
type:
|
|
1195
|
-
timestamp:
|
|
5152
|
+
var PaymentSucceededPayloadSchema = objectType({
|
|
5153
|
+
business_id: stringType(),
|
|
5154
|
+
type: literalType("payment.succeeded"),
|
|
5155
|
+
timestamp: stringType().transform(function (d) { return new Date(d); }),
|
|
1196
5156
|
data: PaymentSchema,
|
|
1197
5157
|
});
|
|
1198
|
-
var PaymentFailedPayloadSchema =
|
|
1199
|
-
business_id:
|
|
1200
|
-
type:
|
|
1201
|
-
timestamp:
|
|
5158
|
+
var PaymentFailedPayloadSchema = objectType({
|
|
5159
|
+
business_id: stringType(),
|
|
5160
|
+
type: literalType("payment.failed"),
|
|
5161
|
+
timestamp: stringType().transform(function (d) { return new Date(d); }),
|
|
1202
5162
|
data: PaymentSchema,
|
|
1203
5163
|
});
|
|
1204
|
-
var PaymentProcessingPayloadSchema =
|
|
1205
|
-
business_id:
|
|
1206
|
-
type:
|
|
1207
|
-
timestamp:
|
|
5164
|
+
var PaymentProcessingPayloadSchema = objectType({
|
|
5165
|
+
business_id: stringType(),
|
|
5166
|
+
type: literalType("payment.processing"),
|
|
5167
|
+
timestamp: stringType().transform(function (d) { return new Date(d); }),
|
|
1208
5168
|
data: PaymentSchema,
|
|
1209
5169
|
});
|
|
1210
|
-
var PaymentCancelledPayloadSchema =
|
|
1211
|
-
business_id:
|
|
1212
|
-
type:
|
|
1213
|
-
timestamp:
|
|
5170
|
+
var PaymentCancelledPayloadSchema = objectType({
|
|
5171
|
+
business_id: stringType(),
|
|
5172
|
+
type: literalType("payment.cancelled"),
|
|
5173
|
+
timestamp: stringType().transform(function (d) { return new Date(d); }),
|
|
1214
5174
|
data: PaymentSchema,
|
|
1215
5175
|
});
|
|
1216
|
-
var RefundSucceededPayloadSchema =
|
|
1217
|
-
business_id:
|
|
1218
|
-
type:
|
|
1219
|
-
timestamp:
|
|
5176
|
+
var RefundSucceededPayloadSchema = objectType({
|
|
5177
|
+
business_id: stringType(),
|
|
5178
|
+
type: literalType("refund.succeeded"),
|
|
5179
|
+
timestamp: stringType().transform(function (d) { return new Date(d); }),
|
|
1220
5180
|
data: RefundSchema,
|
|
1221
5181
|
});
|
|
1222
|
-
var RefundFailedPayloadSchema =
|
|
1223
|
-
business_id:
|
|
1224
|
-
type:
|
|
1225
|
-
timestamp:
|
|
5182
|
+
var RefundFailedPayloadSchema = objectType({
|
|
5183
|
+
business_id: stringType(),
|
|
5184
|
+
type: literalType("refund.failed"),
|
|
5185
|
+
timestamp: stringType().transform(function (d) { return new Date(d); }),
|
|
1226
5186
|
data: RefundSchema,
|
|
1227
5187
|
});
|
|
1228
|
-
var DisputeOpenedPayloadSchema =
|
|
1229
|
-
business_id:
|
|
1230
|
-
type:
|
|
1231
|
-
timestamp:
|
|
5188
|
+
var DisputeOpenedPayloadSchema = objectType({
|
|
5189
|
+
business_id: stringType(),
|
|
5190
|
+
type: literalType("dispute.opened"),
|
|
5191
|
+
timestamp: stringType().transform(function (d) { return new Date(d); }),
|
|
1232
5192
|
data: DisputeSchema,
|
|
1233
5193
|
});
|
|
1234
|
-
var DisputeExpiredPayloadSchema =
|
|
1235
|
-
business_id:
|
|
1236
|
-
type:
|
|
1237
|
-
timestamp:
|
|
5194
|
+
var DisputeExpiredPayloadSchema = objectType({
|
|
5195
|
+
business_id: stringType(),
|
|
5196
|
+
type: literalType("dispute.expired"),
|
|
5197
|
+
timestamp: stringType().transform(function (d) { return new Date(d); }),
|
|
1238
5198
|
data: DisputeSchema,
|
|
1239
5199
|
});
|
|
1240
|
-
var DisputeAcceptedPayloadSchema =
|
|
1241
|
-
business_id:
|
|
1242
|
-
type:
|
|
1243
|
-
timestamp:
|
|
5200
|
+
var DisputeAcceptedPayloadSchema = objectType({
|
|
5201
|
+
business_id: stringType(),
|
|
5202
|
+
type: literalType("dispute.accepted"),
|
|
5203
|
+
timestamp: stringType().transform(function (d) { return new Date(d); }),
|
|
1244
5204
|
data: DisputeSchema,
|
|
1245
5205
|
});
|
|
1246
|
-
var DisputeCancelledPayloadSchema =
|
|
1247
|
-
business_id:
|
|
1248
|
-
type:
|
|
1249
|
-
timestamp:
|
|
5206
|
+
var DisputeCancelledPayloadSchema = objectType({
|
|
5207
|
+
business_id: stringType(),
|
|
5208
|
+
type: literalType("dispute.cancelled"),
|
|
5209
|
+
timestamp: stringType().transform(function (d) { return new Date(d); }),
|
|
1250
5210
|
data: DisputeSchema,
|
|
1251
5211
|
});
|
|
1252
|
-
var DisputeChallengedPayloadSchema =
|
|
1253
|
-
business_id:
|
|
1254
|
-
type:
|
|
1255
|
-
timestamp:
|
|
5212
|
+
var DisputeChallengedPayloadSchema = objectType({
|
|
5213
|
+
business_id: stringType(),
|
|
5214
|
+
type: literalType("dispute.challenged"),
|
|
5215
|
+
timestamp: stringType().transform(function (d) { return new Date(d); }),
|
|
1256
5216
|
data: DisputeSchema,
|
|
1257
5217
|
});
|
|
1258
|
-
var DisputeWonPayloadSchema =
|
|
1259
|
-
business_id:
|
|
1260
|
-
type:
|
|
1261
|
-
timestamp:
|
|
5218
|
+
var DisputeWonPayloadSchema = objectType({
|
|
5219
|
+
business_id: stringType(),
|
|
5220
|
+
type: literalType("dispute.won"),
|
|
5221
|
+
timestamp: stringType().transform(function (d) { return new Date(d); }),
|
|
1262
5222
|
data: DisputeSchema,
|
|
1263
5223
|
});
|
|
1264
|
-
var DisputeLostPayloadSchema =
|
|
1265
|
-
business_id:
|
|
1266
|
-
type:
|
|
1267
|
-
timestamp:
|
|
5224
|
+
var DisputeLostPayloadSchema = objectType({
|
|
5225
|
+
business_id: stringType(),
|
|
5226
|
+
type: literalType("dispute.lost"),
|
|
5227
|
+
timestamp: stringType().transform(function (d) { return new Date(d); }),
|
|
1268
5228
|
data: DisputeSchema,
|
|
1269
5229
|
});
|
|
1270
|
-
var SubscriptionActivePayloadSchema =
|
|
1271
|
-
business_id:
|
|
1272
|
-
type:
|
|
1273
|
-
timestamp:
|
|
5230
|
+
var SubscriptionActivePayloadSchema = objectType({
|
|
5231
|
+
business_id: stringType(),
|
|
5232
|
+
type: literalType("subscription.active"),
|
|
5233
|
+
timestamp: stringType().transform(function (d) { return new Date(d); }),
|
|
1274
5234
|
data: SubscriptionSchema,
|
|
1275
5235
|
});
|
|
1276
|
-
var SubscriptionOnHoldPayloadSchema =
|
|
1277
|
-
business_id:
|
|
1278
|
-
type:
|
|
1279
|
-
timestamp:
|
|
5236
|
+
var SubscriptionOnHoldPayloadSchema = objectType({
|
|
5237
|
+
business_id: stringType(),
|
|
5238
|
+
type: literalType("subscription.on_hold"),
|
|
5239
|
+
timestamp: stringType().transform(function (d) { return new Date(d); }),
|
|
1280
5240
|
data: SubscriptionSchema,
|
|
1281
5241
|
});
|
|
1282
|
-
var SubscriptionRenewedPayloadSchema =
|
|
1283
|
-
business_id:
|
|
1284
|
-
type:
|
|
1285
|
-
timestamp:
|
|
5242
|
+
var SubscriptionRenewedPayloadSchema = objectType({
|
|
5243
|
+
business_id: stringType(),
|
|
5244
|
+
type: literalType("subscription.renewed"),
|
|
5245
|
+
timestamp: stringType().transform(function (d) { return new Date(d); }),
|
|
1286
5246
|
data: SubscriptionSchema,
|
|
1287
5247
|
});
|
|
1288
|
-
var SubscriptionPausedPayloadSchema =
|
|
1289
|
-
business_id:
|
|
1290
|
-
type:
|
|
1291
|
-
timestamp:
|
|
5248
|
+
var SubscriptionPausedPayloadSchema = objectType({
|
|
5249
|
+
business_id: stringType(),
|
|
5250
|
+
type: literalType("subscription.paused"),
|
|
5251
|
+
timestamp: stringType().transform(function (d) { return new Date(d); }),
|
|
1292
5252
|
data: SubscriptionSchema,
|
|
1293
5253
|
});
|
|
1294
|
-
var SubscriptionPlanChangedPayloadSchema =
|
|
1295
|
-
business_id:
|
|
1296
|
-
type:
|
|
1297
|
-
timestamp:
|
|
5254
|
+
var SubscriptionPlanChangedPayloadSchema = objectType({
|
|
5255
|
+
business_id: stringType(),
|
|
5256
|
+
type: literalType("subscription.plan_changed"),
|
|
5257
|
+
timestamp: stringType().transform(function (d) { return new Date(d); }),
|
|
1298
5258
|
data: SubscriptionSchema,
|
|
1299
5259
|
});
|
|
1300
|
-
var SubscriptionCancelledPayloadSchema =
|
|
1301
|
-
business_id:
|
|
1302
|
-
type:
|
|
1303
|
-
timestamp:
|
|
5260
|
+
var SubscriptionCancelledPayloadSchema = objectType({
|
|
5261
|
+
business_id: stringType(),
|
|
5262
|
+
type: literalType("subscription.cancelled"),
|
|
5263
|
+
timestamp: stringType().transform(function (d) { return new Date(d); }),
|
|
1304
5264
|
data: SubscriptionSchema,
|
|
1305
5265
|
});
|
|
1306
|
-
var SubscriptionFailedPayloadSchema =
|
|
1307
|
-
business_id:
|
|
1308
|
-
type:
|
|
1309
|
-
timestamp:
|
|
5266
|
+
var SubscriptionFailedPayloadSchema = objectType({
|
|
5267
|
+
business_id: stringType(),
|
|
5268
|
+
type: literalType("subscription.failed"),
|
|
5269
|
+
timestamp: stringType().transform(function (d) { return new Date(d); }),
|
|
1310
5270
|
data: SubscriptionSchema,
|
|
1311
5271
|
});
|
|
1312
|
-
var SubscriptionExpiredPayloadSchema =
|
|
1313
|
-
business_id:
|
|
1314
|
-
type:
|
|
1315
|
-
timestamp:
|
|
5272
|
+
var SubscriptionExpiredPayloadSchema = objectType({
|
|
5273
|
+
business_id: stringType(),
|
|
5274
|
+
type: literalType("subscription.expired"),
|
|
5275
|
+
timestamp: stringType().transform(function (d) { return new Date(d); }),
|
|
1316
5276
|
data: SubscriptionSchema,
|
|
1317
5277
|
});
|
|
1318
|
-
var LicenseKeyCreatedPayloadSchema =
|
|
1319
|
-
business_id:
|
|
1320
|
-
type:
|
|
1321
|
-
timestamp:
|
|
5278
|
+
var LicenseKeyCreatedPayloadSchema = objectType({
|
|
5279
|
+
business_id: stringType(),
|
|
5280
|
+
type: literalType("license_key.created"),
|
|
5281
|
+
timestamp: stringType().transform(function (d) { return new Date(d); }),
|
|
1322
5282
|
data: LicenseKeySchema,
|
|
1323
5283
|
});
|
|
1324
|
-
var WebhookPayloadSchema =
|
|
5284
|
+
var WebhookPayloadSchema = discriminatedUnionType("type", [
|
|
1325
5285
|
PaymentSucceededPayloadSchema,
|
|
1326
5286
|
PaymentFailedPayloadSchema,
|
|
1327
5287
|
PaymentProcessingPayloadSchema,
|
|
@@ -2458,7 +6418,7 @@ const createDodoWebhookHandler = (handlers) => {
|
|
|
2458
6418
|
return new Response(null, { status: 200 });
|
|
2459
6419
|
}
|
|
2460
6420
|
catch (error) {
|
|
2461
|
-
const errorMessage = error instanceof Error ? error.message :
|
|
6421
|
+
const errorMessage = error instanceof Error ? error.message : "Unknown error occurred";
|
|
2462
6422
|
console.error("Dodo Payments Webhook Error:", errorMessage);
|
|
2463
6423
|
return new Response("Error while processing webhook", { status: 400 });
|
|
2464
6424
|
}
|