@tahanabavi/typefetch 1.4.1 → 1.5.6
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 +1026 -139
- package/dist/index.d.mts +104 -85
- package/dist/index.d.ts +104 -85
- package/dist/index.js +439 -4140
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +427 -4146
- package/dist/index.mjs.map +1 -1
- package/package.json +49 -15
package/dist/index.mjs
CHANGED
|
@@ -1,4067 +1,23 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __export = (target, all) => {
|
|
3
|
-
for (var name in all)
|
|
4
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
5
|
-
};
|
|
6
|
-
|
|
7
|
-
// node_modules/zod/v3/external.js
|
|
8
|
-
var external_exports = {};
|
|
9
|
-
__export(external_exports, {
|
|
10
|
-
BRAND: () => BRAND,
|
|
11
|
-
DIRTY: () => DIRTY,
|
|
12
|
-
EMPTY_PATH: () => EMPTY_PATH,
|
|
13
|
-
INVALID: () => INVALID,
|
|
14
|
-
NEVER: () => NEVER,
|
|
15
|
-
OK: () => OK,
|
|
16
|
-
ParseStatus: () => ParseStatus,
|
|
17
|
-
Schema: () => ZodType,
|
|
18
|
-
ZodAny: () => ZodAny,
|
|
19
|
-
ZodArray: () => ZodArray,
|
|
20
|
-
ZodBigInt: () => ZodBigInt,
|
|
21
|
-
ZodBoolean: () => ZodBoolean,
|
|
22
|
-
ZodBranded: () => ZodBranded,
|
|
23
|
-
ZodCatch: () => ZodCatch,
|
|
24
|
-
ZodDate: () => ZodDate,
|
|
25
|
-
ZodDefault: () => ZodDefault,
|
|
26
|
-
ZodDiscriminatedUnion: () => ZodDiscriminatedUnion,
|
|
27
|
-
ZodEffects: () => ZodEffects,
|
|
28
|
-
ZodEnum: () => ZodEnum,
|
|
29
|
-
ZodError: () => ZodError,
|
|
30
|
-
ZodFirstPartyTypeKind: () => ZodFirstPartyTypeKind,
|
|
31
|
-
ZodFunction: () => ZodFunction,
|
|
32
|
-
ZodIntersection: () => ZodIntersection,
|
|
33
|
-
ZodIssueCode: () => ZodIssueCode,
|
|
34
|
-
ZodLazy: () => ZodLazy,
|
|
35
|
-
ZodLiteral: () => ZodLiteral,
|
|
36
|
-
ZodMap: () => ZodMap,
|
|
37
|
-
ZodNaN: () => ZodNaN,
|
|
38
|
-
ZodNativeEnum: () => ZodNativeEnum,
|
|
39
|
-
ZodNever: () => ZodNever,
|
|
40
|
-
ZodNull: () => ZodNull,
|
|
41
|
-
ZodNullable: () => ZodNullable,
|
|
42
|
-
ZodNumber: () => ZodNumber,
|
|
43
|
-
ZodObject: () => ZodObject,
|
|
44
|
-
ZodOptional: () => ZodOptional,
|
|
45
|
-
ZodParsedType: () => ZodParsedType,
|
|
46
|
-
ZodPipeline: () => ZodPipeline,
|
|
47
|
-
ZodPromise: () => ZodPromise,
|
|
48
|
-
ZodReadonly: () => ZodReadonly,
|
|
49
|
-
ZodRecord: () => ZodRecord,
|
|
50
|
-
ZodSchema: () => ZodType,
|
|
51
|
-
ZodSet: () => ZodSet,
|
|
52
|
-
ZodString: () => ZodString,
|
|
53
|
-
ZodSymbol: () => ZodSymbol,
|
|
54
|
-
ZodTransformer: () => ZodEffects,
|
|
55
|
-
ZodTuple: () => ZodTuple,
|
|
56
|
-
ZodType: () => ZodType,
|
|
57
|
-
ZodUndefined: () => ZodUndefined,
|
|
58
|
-
ZodUnion: () => ZodUnion,
|
|
59
|
-
ZodUnknown: () => ZodUnknown,
|
|
60
|
-
ZodVoid: () => ZodVoid,
|
|
61
|
-
addIssueToContext: () => addIssueToContext,
|
|
62
|
-
any: () => anyType,
|
|
63
|
-
array: () => arrayType,
|
|
64
|
-
bigint: () => bigIntType,
|
|
65
|
-
boolean: () => booleanType,
|
|
66
|
-
coerce: () => coerce,
|
|
67
|
-
custom: () => custom,
|
|
68
|
-
date: () => dateType,
|
|
69
|
-
datetimeRegex: () => datetimeRegex,
|
|
70
|
-
defaultErrorMap: () => en_default,
|
|
71
|
-
discriminatedUnion: () => discriminatedUnionType,
|
|
72
|
-
effect: () => effectsType,
|
|
73
|
-
enum: () => enumType,
|
|
74
|
-
function: () => functionType,
|
|
75
|
-
getErrorMap: () => getErrorMap,
|
|
76
|
-
getParsedType: () => getParsedType,
|
|
77
|
-
instanceof: () => instanceOfType,
|
|
78
|
-
intersection: () => intersectionType,
|
|
79
|
-
isAborted: () => isAborted,
|
|
80
|
-
isAsync: () => isAsync,
|
|
81
|
-
isDirty: () => isDirty,
|
|
82
|
-
isValid: () => isValid,
|
|
83
|
-
late: () => late,
|
|
84
|
-
lazy: () => lazyType,
|
|
85
|
-
literal: () => literalType,
|
|
86
|
-
makeIssue: () => makeIssue,
|
|
87
|
-
map: () => mapType,
|
|
88
|
-
nan: () => nanType,
|
|
89
|
-
nativeEnum: () => nativeEnumType,
|
|
90
|
-
never: () => neverType,
|
|
91
|
-
null: () => nullType,
|
|
92
|
-
nullable: () => nullableType,
|
|
93
|
-
number: () => numberType,
|
|
94
|
-
object: () => objectType,
|
|
95
|
-
objectUtil: () => objectUtil,
|
|
96
|
-
oboolean: () => oboolean,
|
|
97
|
-
onumber: () => onumber,
|
|
98
|
-
optional: () => optionalType,
|
|
99
|
-
ostring: () => ostring,
|
|
100
|
-
pipeline: () => pipelineType,
|
|
101
|
-
preprocess: () => preprocessType,
|
|
102
|
-
promise: () => promiseType,
|
|
103
|
-
quotelessJson: () => quotelessJson,
|
|
104
|
-
record: () => recordType,
|
|
105
|
-
set: () => setType,
|
|
106
|
-
setErrorMap: () => setErrorMap,
|
|
107
|
-
strictObject: () => strictObjectType,
|
|
108
|
-
string: () => stringType,
|
|
109
|
-
symbol: () => symbolType,
|
|
110
|
-
transformer: () => effectsType,
|
|
111
|
-
tuple: () => tupleType,
|
|
112
|
-
undefined: () => undefinedType,
|
|
113
|
-
union: () => unionType,
|
|
114
|
-
unknown: () => unknownType,
|
|
115
|
-
util: () => util,
|
|
116
|
-
void: () => voidType
|
|
117
|
-
});
|
|
118
|
-
|
|
119
|
-
// node_modules/zod/v3/helpers/util.js
|
|
120
|
-
var util;
|
|
121
|
-
(function(util2) {
|
|
122
|
-
util2.assertEqual = (_) => {
|
|
123
|
-
};
|
|
124
|
-
function assertIs(_arg) {
|
|
125
|
-
}
|
|
126
|
-
util2.assertIs = assertIs;
|
|
127
|
-
function assertNever(_x) {
|
|
128
|
-
throw new Error();
|
|
129
|
-
}
|
|
130
|
-
util2.assertNever = assertNever;
|
|
131
|
-
util2.arrayToEnum = (items) => {
|
|
132
|
-
const obj = {};
|
|
133
|
-
for (const item of items) {
|
|
134
|
-
obj[item] = item;
|
|
135
|
-
}
|
|
136
|
-
return obj;
|
|
137
|
-
};
|
|
138
|
-
util2.getValidEnumValues = (obj) => {
|
|
139
|
-
const validKeys = util2.objectKeys(obj).filter((k) => typeof obj[obj[k]] !== "number");
|
|
140
|
-
const filtered = {};
|
|
141
|
-
for (const k of validKeys) {
|
|
142
|
-
filtered[k] = obj[k];
|
|
143
|
-
}
|
|
144
|
-
return util2.objectValues(filtered);
|
|
145
|
-
};
|
|
146
|
-
util2.objectValues = (obj) => {
|
|
147
|
-
return util2.objectKeys(obj).map(function(e) {
|
|
148
|
-
return obj[e];
|
|
149
|
-
});
|
|
150
|
-
};
|
|
151
|
-
util2.objectKeys = typeof Object.keys === "function" ? (obj) => Object.keys(obj) : (object) => {
|
|
152
|
-
const keys = [];
|
|
153
|
-
for (const key in object) {
|
|
154
|
-
if (Object.prototype.hasOwnProperty.call(object, key)) {
|
|
155
|
-
keys.push(key);
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
return keys;
|
|
159
|
-
};
|
|
160
|
-
util2.find = (arr, checker) => {
|
|
161
|
-
for (const item of arr) {
|
|
162
|
-
if (checker(item))
|
|
163
|
-
return item;
|
|
164
|
-
}
|
|
165
|
-
return void 0;
|
|
166
|
-
};
|
|
167
|
-
util2.isInteger = typeof Number.isInteger === "function" ? (val) => Number.isInteger(val) : (val) => typeof val === "number" && Number.isFinite(val) && Math.floor(val) === val;
|
|
168
|
-
function joinValues(array, separator = " | ") {
|
|
169
|
-
return array.map((val) => typeof val === "string" ? `'${val}'` : val).join(separator);
|
|
170
|
-
}
|
|
171
|
-
util2.joinValues = joinValues;
|
|
172
|
-
util2.jsonStringifyReplacer = (_, value) => {
|
|
173
|
-
if (typeof value === "bigint") {
|
|
174
|
-
return value.toString();
|
|
175
|
-
}
|
|
176
|
-
return value;
|
|
177
|
-
};
|
|
178
|
-
})(util || (util = {}));
|
|
179
|
-
var objectUtil;
|
|
180
|
-
(function(objectUtil2) {
|
|
181
|
-
objectUtil2.mergeShapes = (first, second) => {
|
|
182
|
-
return {
|
|
183
|
-
...first,
|
|
184
|
-
...second
|
|
185
|
-
// second overwrites first
|
|
186
|
-
};
|
|
187
|
-
};
|
|
188
|
-
})(objectUtil || (objectUtil = {}));
|
|
189
|
-
var ZodParsedType = util.arrayToEnum([
|
|
190
|
-
"string",
|
|
191
|
-
"nan",
|
|
192
|
-
"number",
|
|
193
|
-
"integer",
|
|
194
|
-
"float",
|
|
195
|
-
"boolean",
|
|
196
|
-
"date",
|
|
197
|
-
"bigint",
|
|
198
|
-
"symbol",
|
|
199
|
-
"function",
|
|
200
|
-
"undefined",
|
|
201
|
-
"null",
|
|
202
|
-
"array",
|
|
203
|
-
"object",
|
|
204
|
-
"unknown",
|
|
205
|
-
"promise",
|
|
206
|
-
"void",
|
|
207
|
-
"never",
|
|
208
|
-
"map",
|
|
209
|
-
"set"
|
|
210
|
-
]);
|
|
211
|
-
var getParsedType = (data) => {
|
|
212
|
-
const t = typeof data;
|
|
213
|
-
switch (t) {
|
|
214
|
-
case "undefined":
|
|
215
|
-
return ZodParsedType.undefined;
|
|
216
|
-
case "string":
|
|
217
|
-
return ZodParsedType.string;
|
|
218
|
-
case "number":
|
|
219
|
-
return Number.isNaN(data) ? ZodParsedType.nan : ZodParsedType.number;
|
|
220
|
-
case "boolean":
|
|
221
|
-
return ZodParsedType.boolean;
|
|
222
|
-
case "function":
|
|
223
|
-
return ZodParsedType.function;
|
|
224
|
-
case "bigint":
|
|
225
|
-
return ZodParsedType.bigint;
|
|
226
|
-
case "symbol":
|
|
227
|
-
return ZodParsedType.symbol;
|
|
228
|
-
case "object":
|
|
229
|
-
if (Array.isArray(data)) {
|
|
230
|
-
return ZodParsedType.array;
|
|
231
|
-
}
|
|
232
|
-
if (data === null) {
|
|
233
|
-
return ZodParsedType.null;
|
|
234
|
-
}
|
|
235
|
-
if (data.then && typeof data.then === "function" && data.catch && typeof data.catch === "function") {
|
|
236
|
-
return ZodParsedType.promise;
|
|
237
|
-
}
|
|
238
|
-
if (typeof Map !== "undefined" && data instanceof Map) {
|
|
239
|
-
return ZodParsedType.map;
|
|
240
|
-
}
|
|
241
|
-
if (typeof Set !== "undefined" && data instanceof Set) {
|
|
242
|
-
return ZodParsedType.set;
|
|
243
|
-
}
|
|
244
|
-
if (typeof Date !== "undefined" && data instanceof Date) {
|
|
245
|
-
return ZodParsedType.date;
|
|
246
|
-
}
|
|
247
|
-
return ZodParsedType.object;
|
|
248
|
-
default:
|
|
249
|
-
return ZodParsedType.unknown;
|
|
250
|
-
}
|
|
251
|
-
};
|
|
252
|
-
|
|
253
|
-
// node_modules/zod/v3/ZodError.js
|
|
254
|
-
var ZodIssueCode = util.arrayToEnum([
|
|
255
|
-
"invalid_type",
|
|
256
|
-
"invalid_literal",
|
|
257
|
-
"custom",
|
|
258
|
-
"invalid_union",
|
|
259
|
-
"invalid_union_discriminator",
|
|
260
|
-
"invalid_enum_value",
|
|
261
|
-
"unrecognized_keys",
|
|
262
|
-
"invalid_arguments",
|
|
263
|
-
"invalid_return_type",
|
|
264
|
-
"invalid_date",
|
|
265
|
-
"invalid_string",
|
|
266
|
-
"too_small",
|
|
267
|
-
"too_big",
|
|
268
|
-
"invalid_intersection_types",
|
|
269
|
-
"not_multiple_of",
|
|
270
|
-
"not_finite"
|
|
271
|
-
]);
|
|
272
|
-
var quotelessJson = (obj) => {
|
|
273
|
-
const json = JSON.stringify(obj, null, 2);
|
|
274
|
-
return json.replace(/"([^"]+)":/g, "$1:");
|
|
275
|
-
};
|
|
276
|
-
var ZodError = class _ZodError extends Error {
|
|
277
|
-
get errors() {
|
|
278
|
-
return this.issues;
|
|
279
|
-
}
|
|
280
|
-
constructor(issues) {
|
|
281
|
-
super();
|
|
282
|
-
this.issues = [];
|
|
283
|
-
this.addIssue = (sub) => {
|
|
284
|
-
this.issues = [...this.issues, sub];
|
|
285
|
-
};
|
|
286
|
-
this.addIssues = (subs = []) => {
|
|
287
|
-
this.issues = [...this.issues, ...subs];
|
|
288
|
-
};
|
|
289
|
-
const actualProto = new.target.prototype;
|
|
290
|
-
if (Object.setPrototypeOf) {
|
|
291
|
-
Object.setPrototypeOf(this, actualProto);
|
|
292
|
-
} else {
|
|
293
|
-
this.__proto__ = actualProto;
|
|
294
|
-
}
|
|
295
|
-
this.name = "ZodError";
|
|
296
|
-
this.issues = issues;
|
|
297
|
-
}
|
|
298
|
-
format(_mapper) {
|
|
299
|
-
const mapper = _mapper || function(issue) {
|
|
300
|
-
return issue.message;
|
|
301
|
-
};
|
|
302
|
-
const fieldErrors = { _errors: [] };
|
|
303
|
-
const processError = (error) => {
|
|
304
|
-
for (const issue of error.issues) {
|
|
305
|
-
if (issue.code === "invalid_union") {
|
|
306
|
-
issue.unionErrors.map(processError);
|
|
307
|
-
} else if (issue.code === "invalid_return_type") {
|
|
308
|
-
processError(issue.returnTypeError);
|
|
309
|
-
} else if (issue.code === "invalid_arguments") {
|
|
310
|
-
processError(issue.argumentsError);
|
|
311
|
-
} else if (issue.path.length === 0) {
|
|
312
|
-
fieldErrors._errors.push(mapper(issue));
|
|
313
|
-
} else {
|
|
314
|
-
let curr = fieldErrors;
|
|
315
|
-
let i = 0;
|
|
316
|
-
while (i < issue.path.length) {
|
|
317
|
-
const el = issue.path[i];
|
|
318
|
-
const terminal = i === issue.path.length - 1;
|
|
319
|
-
if (!terminal) {
|
|
320
|
-
curr[el] = curr[el] || { _errors: [] };
|
|
321
|
-
} else {
|
|
322
|
-
curr[el] = curr[el] || { _errors: [] };
|
|
323
|
-
curr[el]._errors.push(mapper(issue));
|
|
324
|
-
}
|
|
325
|
-
curr = curr[el];
|
|
326
|
-
i++;
|
|
327
|
-
}
|
|
328
|
-
}
|
|
329
|
-
}
|
|
330
|
-
};
|
|
331
|
-
processError(this);
|
|
332
|
-
return fieldErrors;
|
|
333
|
-
}
|
|
334
|
-
static assert(value) {
|
|
335
|
-
if (!(value instanceof _ZodError)) {
|
|
336
|
-
throw new Error(`Not a ZodError: ${value}`);
|
|
337
|
-
}
|
|
338
|
-
}
|
|
339
|
-
toString() {
|
|
340
|
-
return this.message;
|
|
341
|
-
}
|
|
342
|
-
get message() {
|
|
343
|
-
return JSON.stringify(this.issues, util.jsonStringifyReplacer, 2);
|
|
344
|
-
}
|
|
345
|
-
get isEmpty() {
|
|
346
|
-
return this.issues.length === 0;
|
|
347
|
-
}
|
|
348
|
-
flatten(mapper = (issue) => issue.message) {
|
|
349
|
-
const fieldErrors = {};
|
|
350
|
-
const formErrors = [];
|
|
351
|
-
for (const sub of this.issues) {
|
|
352
|
-
if (sub.path.length > 0) {
|
|
353
|
-
const firstEl = sub.path[0];
|
|
354
|
-
fieldErrors[firstEl] = fieldErrors[firstEl] || [];
|
|
355
|
-
fieldErrors[firstEl].push(mapper(sub));
|
|
356
|
-
} else {
|
|
357
|
-
formErrors.push(mapper(sub));
|
|
358
|
-
}
|
|
359
|
-
}
|
|
360
|
-
return { formErrors, fieldErrors };
|
|
361
|
-
}
|
|
362
|
-
get formErrors() {
|
|
363
|
-
return this.flatten();
|
|
364
|
-
}
|
|
365
|
-
};
|
|
366
|
-
ZodError.create = (issues) => {
|
|
367
|
-
const error = new ZodError(issues);
|
|
368
|
-
return error;
|
|
369
|
-
};
|
|
370
|
-
|
|
371
|
-
// node_modules/zod/v3/locales/en.js
|
|
372
|
-
var errorMap = (issue, _ctx) => {
|
|
373
|
-
let message;
|
|
374
|
-
switch (issue.code) {
|
|
375
|
-
case ZodIssueCode.invalid_type:
|
|
376
|
-
if (issue.received === ZodParsedType.undefined) {
|
|
377
|
-
message = "Required";
|
|
378
|
-
} else {
|
|
379
|
-
message = `Expected ${issue.expected}, received ${issue.received}`;
|
|
380
|
-
}
|
|
381
|
-
break;
|
|
382
|
-
case ZodIssueCode.invalid_literal:
|
|
383
|
-
message = `Invalid literal value, expected ${JSON.stringify(issue.expected, util.jsonStringifyReplacer)}`;
|
|
384
|
-
break;
|
|
385
|
-
case ZodIssueCode.unrecognized_keys:
|
|
386
|
-
message = `Unrecognized key(s) in object: ${util.joinValues(issue.keys, ", ")}`;
|
|
387
|
-
break;
|
|
388
|
-
case ZodIssueCode.invalid_union:
|
|
389
|
-
message = `Invalid input`;
|
|
390
|
-
break;
|
|
391
|
-
case ZodIssueCode.invalid_union_discriminator:
|
|
392
|
-
message = `Invalid discriminator value. Expected ${util.joinValues(issue.options)}`;
|
|
393
|
-
break;
|
|
394
|
-
case ZodIssueCode.invalid_enum_value:
|
|
395
|
-
message = `Invalid enum value. Expected ${util.joinValues(issue.options)}, received '${issue.received}'`;
|
|
396
|
-
break;
|
|
397
|
-
case ZodIssueCode.invalid_arguments:
|
|
398
|
-
message = `Invalid function arguments`;
|
|
399
|
-
break;
|
|
400
|
-
case ZodIssueCode.invalid_return_type:
|
|
401
|
-
message = `Invalid function return type`;
|
|
402
|
-
break;
|
|
403
|
-
case ZodIssueCode.invalid_date:
|
|
404
|
-
message = `Invalid date`;
|
|
405
|
-
break;
|
|
406
|
-
case ZodIssueCode.invalid_string:
|
|
407
|
-
if (typeof issue.validation === "object") {
|
|
408
|
-
if ("includes" in issue.validation) {
|
|
409
|
-
message = `Invalid input: must include "${issue.validation.includes}"`;
|
|
410
|
-
if (typeof issue.validation.position === "number") {
|
|
411
|
-
message = `${message} at one or more positions greater than or equal to ${issue.validation.position}`;
|
|
412
|
-
}
|
|
413
|
-
} else if ("startsWith" in issue.validation) {
|
|
414
|
-
message = `Invalid input: must start with "${issue.validation.startsWith}"`;
|
|
415
|
-
} else if ("endsWith" in issue.validation) {
|
|
416
|
-
message = `Invalid input: must end with "${issue.validation.endsWith}"`;
|
|
417
|
-
} else {
|
|
418
|
-
util.assertNever(issue.validation);
|
|
419
|
-
}
|
|
420
|
-
} else if (issue.validation !== "regex") {
|
|
421
|
-
message = `Invalid ${issue.validation}`;
|
|
422
|
-
} else {
|
|
423
|
-
message = "Invalid";
|
|
424
|
-
}
|
|
425
|
-
break;
|
|
426
|
-
case ZodIssueCode.too_small:
|
|
427
|
-
if (issue.type === "array")
|
|
428
|
-
message = `Array must contain ${issue.exact ? "exactly" : issue.inclusive ? `at least` : `more than`} ${issue.minimum} element(s)`;
|
|
429
|
-
else if (issue.type === "string")
|
|
430
|
-
message = `String must contain ${issue.exact ? "exactly" : issue.inclusive ? `at least` : `over`} ${issue.minimum} character(s)`;
|
|
431
|
-
else if (issue.type === "number")
|
|
432
|
-
message = `Number must be ${issue.exact ? `exactly equal to ` : issue.inclusive ? `greater than or equal to ` : `greater than `}${issue.minimum}`;
|
|
433
|
-
else if (issue.type === "bigint")
|
|
434
|
-
message = `Number must be ${issue.exact ? `exactly equal to ` : issue.inclusive ? `greater than or equal to ` : `greater than `}${issue.minimum}`;
|
|
435
|
-
else if (issue.type === "date")
|
|
436
|
-
message = `Date must be ${issue.exact ? `exactly equal to ` : issue.inclusive ? `greater than or equal to ` : `greater than `}${new Date(Number(issue.minimum))}`;
|
|
437
|
-
else
|
|
438
|
-
message = "Invalid input";
|
|
439
|
-
break;
|
|
440
|
-
case ZodIssueCode.too_big:
|
|
441
|
-
if (issue.type === "array")
|
|
442
|
-
message = `Array must contain ${issue.exact ? `exactly` : issue.inclusive ? `at most` : `less than`} ${issue.maximum} element(s)`;
|
|
443
|
-
else if (issue.type === "string")
|
|
444
|
-
message = `String must contain ${issue.exact ? `exactly` : issue.inclusive ? `at most` : `under`} ${issue.maximum} character(s)`;
|
|
445
|
-
else if (issue.type === "number")
|
|
446
|
-
message = `Number must be ${issue.exact ? `exactly` : issue.inclusive ? `less than or equal to` : `less than`} ${issue.maximum}`;
|
|
447
|
-
else if (issue.type === "bigint")
|
|
448
|
-
message = `BigInt must be ${issue.exact ? `exactly` : issue.inclusive ? `less than or equal to` : `less than`} ${issue.maximum}`;
|
|
449
|
-
else if (issue.type === "date")
|
|
450
|
-
message = `Date must be ${issue.exact ? `exactly` : issue.inclusive ? `smaller than or equal to` : `smaller than`} ${new Date(Number(issue.maximum))}`;
|
|
451
|
-
else
|
|
452
|
-
message = "Invalid input";
|
|
453
|
-
break;
|
|
454
|
-
case ZodIssueCode.custom:
|
|
455
|
-
message = `Invalid input`;
|
|
456
|
-
break;
|
|
457
|
-
case ZodIssueCode.invalid_intersection_types:
|
|
458
|
-
message = `Intersection results could not be merged`;
|
|
459
|
-
break;
|
|
460
|
-
case ZodIssueCode.not_multiple_of:
|
|
461
|
-
message = `Number must be a multiple of ${issue.multipleOf}`;
|
|
462
|
-
break;
|
|
463
|
-
case ZodIssueCode.not_finite:
|
|
464
|
-
message = "Number must be finite";
|
|
465
|
-
break;
|
|
466
|
-
default:
|
|
467
|
-
message = _ctx.defaultError;
|
|
468
|
-
util.assertNever(issue);
|
|
469
|
-
}
|
|
470
|
-
return { message };
|
|
471
|
-
};
|
|
472
|
-
var en_default = errorMap;
|
|
473
|
-
|
|
474
|
-
// node_modules/zod/v3/errors.js
|
|
475
|
-
var overrideErrorMap = en_default;
|
|
476
|
-
function setErrorMap(map) {
|
|
477
|
-
overrideErrorMap = map;
|
|
478
|
-
}
|
|
479
|
-
function getErrorMap() {
|
|
480
|
-
return overrideErrorMap;
|
|
481
|
-
}
|
|
482
|
-
|
|
483
|
-
// node_modules/zod/v3/helpers/parseUtil.js
|
|
484
|
-
var makeIssue = (params) => {
|
|
485
|
-
const { data, path, errorMaps, issueData } = params;
|
|
486
|
-
const fullPath = [...path, ...issueData.path || []];
|
|
487
|
-
const fullIssue = {
|
|
488
|
-
...issueData,
|
|
489
|
-
path: fullPath
|
|
490
|
-
};
|
|
491
|
-
if (issueData.message !== void 0) {
|
|
492
|
-
return {
|
|
493
|
-
...issueData,
|
|
494
|
-
path: fullPath,
|
|
495
|
-
message: issueData.message
|
|
496
|
-
};
|
|
497
|
-
}
|
|
498
|
-
let errorMessage = "";
|
|
499
|
-
const maps = errorMaps.filter((m) => !!m).slice().reverse();
|
|
500
|
-
for (const map of maps) {
|
|
501
|
-
errorMessage = map(fullIssue, { data, defaultError: errorMessage }).message;
|
|
502
|
-
}
|
|
503
|
-
return {
|
|
504
|
-
...issueData,
|
|
505
|
-
path: fullPath,
|
|
506
|
-
message: errorMessage
|
|
507
|
-
};
|
|
508
|
-
};
|
|
509
|
-
var EMPTY_PATH = [];
|
|
510
|
-
function addIssueToContext(ctx, issueData) {
|
|
511
|
-
const overrideMap = getErrorMap();
|
|
512
|
-
const issue = makeIssue({
|
|
513
|
-
issueData,
|
|
514
|
-
data: ctx.data,
|
|
515
|
-
path: ctx.path,
|
|
516
|
-
errorMaps: [
|
|
517
|
-
ctx.common.contextualErrorMap,
|
|
518
|
-
// contextual error map is first priority
|
|
519
|
-
ctx.schemaErrorMap,
|
|
520
|
-
// then schema-bound map if available
|
|
521
|
-
overrideMap,
|
|
522
|
-
// then global override map
|
|
523
|
-
overrideMap === en_default ? void 0 : en_default
|
|
524
|
-
// then global default map
|
|
525
|
-
].filter((x) => !!x)
|
|
526
|
-
});
|
|
527
|
-
ctx.common.issues.push(issue);
|
|
528
|
-
}
|
|
529
|
-
var ParseStatus = class _ParseStatus {
|
|
530
|
-
constructor() {
|
|
531
|
-
this.value = "valid";
|
|
532
|
-
}
|
|
533
|
-
dirty() {
|
|
534
|
-
if (this.value === "valid")
|
|
535
|
-
this.value = "dirty";
|
|
536
|
-
}
|
|
537
|
-
abort() {
|
|
538
|
-
if (this.value !== "aborted")
|
|
539
|
-
this.value = "aborted";
|
|
540
|
-
}
|
|
541
|
-
static mergeArray(status, results) {
|
|
542
|
-
const arrayValue = [];
|
|
543
|
-
for (const s of results) {
|
|
544
|
-
if (s.status === "aborted")
|
|
545
|
-
return INVALID;
|
|
546
|
-
if (s.status === "dirty")
|
|
547
|
-
status.dirty();
|
|
548
|
-
arrayValue.push(s.value);
|
|
549
|
-
}
|
|
550
|
-
return { status: status.value, value: arrayValue };
|
|
551
|
-
}
|
|
552
|
-
static async mergeObjectAsync(status, pairs) {
|
|
553
|
-
const syncPairs = [];
|
|
554
|
-
for (const pair of pairs) {
|
|
555
|
-
const key = await pair.key;
|
|
556
|
-
const value = await pair.value;
|
|
557
|
-
syncPairs.push({
|
|
558
|
-
key,
|
|
559
|
-
value
|
|
560
|
-
});
|
|
561
|
-
}
|
|
562
|
-
return _ParseStatus.mergeObjectSync(status, syncPairs);
|
|
563
|
-
}
|
|
564
|
-
static mergeObjectSync(status, pairs) {
|
|
565
|
-
const finalObject = {};
|
|
566
|
-
for (const pair of pairs) {
|
|
567
|
-
const { key, value } = pair;
|
|
568
|
-
if (key.status === "aborted")
|
|
569
|
-
return INVALID;
|
|
570
|
-
if (value.status === "aborted")
|
|
571
|
-
return INVALID;
|
|
572
|
-
if (key.status === "dirty")
|
|
573
|
-
status.dirty();
|
|
574
|
-
if (value.status === "dirty")
|
|
575
|
-
status.dirty();
|
|
576
|
-
if (key.value !== "__proto__" && (typeof value.value !== "undefined" || pair.alwaysSet)) {
|
|
577
|
-
finalObject[key.value] = value.value;
|
|
578
|
-
}
|
|
579
|
-
}
|
|
580
|
-
return { status: status.value, value: finalObject };
|
|
581
|
-
}
|
|
582
|
-
};
|
|
583
|
-
var INVALID = Object.freeze({
|
|
584
|
-
status: "aborted"
|
|
585
|
-
});
|
|
586
|
-
var DIRTY = (value) => ({ status: "dirty", value });
|
|
587
|
-
var OK = (value) => ({ status: "valid", value });
|
|
588
|
-
var isAborted = (x) => x.status === "aborted";
|
|
589
|
-
var isDirty = (x) => x.status === "dirty";
|
|
590
|
-
var isValid = (x) => x.status === "valid";
|
|
591
|
-
var isAsync = (x) => typeof Promise !== "undefined" && x instanceof Promise;
|
|
592
|
-
|
|
593
|
-
// node_modules/zod/v3/helpers/errorUtil.js
|
|
594
|
-
var errorUtil;
|
|
595
|
-
(function(errorUtil2) {
|
|
596
|
-
errorUtil2.errToObj = (message) => typeof message === "string" ? { message } : message || {};
|
|
597
|
-
errorUtil2.toString = (message) => typeof message === "string" ? message : message?.message;
|
|
598
|
-
})(errorUtil || (errorUtil = {}));
|
|
599
|
-
|
|
600
|
-
// node_modules/zod/v3/types.js
|
|
601
|
-
var ParseInputLazyPath = class {
|
|
602
|
-
constructor(parent, value, path, key) {
|
|
603
|
-
this._cachedPath = [];
|
|
604
|
-
this.parent = parent;
|
|
605
|
-
this.data = value;
|
|
606
|
-
this._path = path;
|
|
607
|
-
this._key = key;
|
|
608
|
-
}
|
|
609
|
-
get path() {
|
|
610
|
-
if (!this._cachedPath.length) {
|
|
611
|
-
if (Array.isArray(this._key)) {
|
|
612
|
-
this._cachedPath.push(...this._path, ...this._key);
|
|
613
|
-
} else {
|
|
614
|
-
this._cachedPath.push(...this._path, this._key);
|
|
615
|
-
}
|
|
616
|
-
}
|
|
617
|
-
return this._cachedPath;
|
|
618
|
-
}
|
|
619
|
-
};
|
|
620
|
-
var handleResult = (ctx, result) => {
|
|
621
|
-
if (isValid(result)) {
|
|
622
|
-
return { success: true, data: result.value };
|
|
623
|
-
} else {
|
|
624
|
-
if (!ctx.common.issues.length) {
|
|
625
|
-
throw new Error("Validation failed but no issues detected.");
|
|
626
|
-
}
|
|
627
|
-
return {
|
|
628
|
-
success: false,
|
|
629
|
-
get error() {
|
|
630
|
-
if (this._error)
|
|
631
|
-
return this._error;
|
|
632
|
-
const error = new ZodError(ctx.common.issues);
|
|
633
|
-
this._error = error;
|
|
634
|
-
return this._error;
|
|
635
|
-
}
|
|
636
|
-
};
|
|
637
|
-
}
|
|
638
|
-
};
|
|
639
|
-
function processCreateParams(params) {
|
|
640
|
-
if (!params)
|
|
641
|
-
return {};
|
|
642
|
-
const { errorMap: errorMap2, invalid_type_error, required_error, description } = params;
|
|
643
|
-
if (errorMap2 && (invalid_type_error || required_error)) {
|
|
644
|
-
throw new Error(`Can't use "invalid_type_error" or "required_error" in conjunction with custom error map.`);
|
|
645
|
-
}
|
|
646
|
-
if (errorMap2)
|
|
647
|
-
return { errorMap: errorMap2, description };
|
|
648
|
-
const customMap = (iss, ctx) => {
|
|
649
|
-
const { message } = params;
|
|
650
|
-
if (iss.code === "invalid_enum_value") {
|
|
651
|
-
return { message: message ?? ctx.defaultError };
|
|
652
|
-
}
|
|
653
|
-
if (typeof ctx.data === "undefined") {
|
|
654
|
-
return { message: message ?? required_error ?? ctx.defaultError };
|
|
655
|
-
}
|
|
656
|
-
if (iss.code !== "invalid_type")
|
|
657
|
-
return { message: ctx.defaultError };
|
|
658
|
-
return { message: message ?? invalid_type_error ?? ctx.defaultError };
|
|
659
|
-
};
|
|
660
|
-
return { errorMap: customMap, description };
|
|
661
|
-
}
|
|
662
|
-
var ZodType = class {
|
|
663
|
-
get description() {
|
|
664
|
-
return this._def.description;
|
|
665
|
-
}
|
|
666
|
-
_getType(input) {
|
|
667
|
-
return getParsedType(input.data);
|
|
668
|
-
}
|
|
669
|
-
_getOrReturnCtx(input, ctx) {
|
|
670
|
-
return ctx || {
|
|
671
|
-
common: input.parent.common,
|
|
672
|
-
data: input.data,
|
|
673
|
-
parsedType: getParsedType(input.data),
|
|
674
|
-
schemaErrorMap: this._def.errorMap,
|
|
675
|
-
path: input.path,
|
|
676
|
-
parent: input.parent
|
|
677
|
-
};
|
|
678
|
-
}
|
|
679
|
-
_processInputParams(input) {
|
|
680
|
-
return {
|
|
681
|
-
status: new ParseStatus(),
|
|
682
|
-
ctx: {
|
|
683
|
-
common: input.parent.common,
|
|
684
|
-
data: input.data,
|
|
685
|
-
parsedType: getParsedType(input.data),
|
|
686
|
-
schemaErrorMap: this._def.errorMap,
|
|
687
|
-
path: input.path,
|
|
688
|
-
parent: input.parent
|
|
689
|
-
}
|
|
690
|
-
};
|
|
691
|
-
}
|
|
692
|
-
_parseSync(input) {
|
|
693
|
-
const result = this._parse(input);
|
|
694
|
-
if (isAsync(result)) {
|
|
695
|
-
throw new Error("Synchronous parse encountered promise.");
|
|
696
|
-
}
|
|
697
|
-
return result;
|
|
698
|
-
}
|
|
699
|
-
_parseAsync(input) {
|
|
700
|
-
const result = this._parse(input);
|
|
701
|
-
return Promise.resolve(result);
|
|
702
|
-
}
|
|
703
|
-
parse(data, params) {
|
|
704
|
-
const result = this.safeParse(data, params);
|
|
705
|
-
if (result.success)
|
|
706
|
-
return result.data;
|
|
707
|
-
throw result.error;
|
|
708
|
-
}
|
|
709
|
-
safeParse(data, params) {
|
|
710
|
-
const ctx = {
|
|
711
|
-
common: {
|
|
712
|
-
issues: [],
|
|
713
|
-
async: params?.async ?? false,
|
|
714
|
-
contextualErrorMap: params?.errorMap
|
|
715
|
-
},
|
|
716
|
-
path: params?.path || [],
|
|
717
|
-
schemaErrorMap: this._def.errorMap,
|
|
718
|
-
parent: null,
|
|
719
|
-
data,
|
|
720
|
-
parsedType: getParsedType(data)
|
|
721
|
-
};
|
|
722
|
-
const result = this._parseSync({ data, path: ctx.path, parent: ctx });
|
|
723
|
-
return handleResult(ctx, result);
|
|
724
|
-
}
|
|
725
|
-
"~validate"(data) {
|
|
726
|
-
const ctx = {
|
|
727
|
-
common: {
|
|
728
|
-
issues: [],
|
|
729
|
-
async: !!this["~standard"].async
|
|
730
|
-
},
|
|
731
|
-
path: [],
|
|
732
|
-
schemaErrorMap: this._def.errorMap,
|
|
733
|
-
parent: null,
|
|
734
|
-
data,
|
|
735
|
-
parsedType: getParsedType(data)
|
|
736
|
-
};
|
|
737
|
-
if (!this["~standard"].async) {
|
|
738
|
-
try {
|
|
739
|
-
const result = this._parseSync({ data, path: [], parent: ctx });
|
|
740
|
-
return isValid(result) ? {
|
|
741
|
-
value: result.value
|
|
742
|
-
} : {
|
|
743
|
-
issues: ctx.common.issues
|
|
744
|
-
};
|
|
745
|
-
} catch (err) {
|
|
746
|
-
if (err?.message?.toLowerCase()?.includes("encountered")) {
|
|
747
|
-
this["~standard"].async = true;
|
|
748
|
-
}
|
|
749
|
-
ctx.common = {
|
|
750
|
-
issues: [],
|
|
751
|
-
async: true
|
|
752
|
-
};
|
|
753
|
-
}
|
|
754
|
-
}
|
|
755
|
-
return this._parseAsync({ data, path: [], parent: ctx }).then((result) => isValid(result) ? {
|
|
756
|
-
value: result.value
|
|
757
|
-
} : {
|
|
758
|
-
issues: ctx.common.issues
|
|
759
|
-
});
|
|
760
|
-
}
|
|
761
|
-
async parseAsync(data, params) {
|
|
762
|
-
const result = await this.safeParseAsync(data, params);
|
|
763
|
-
if (result.success)
|
|
764
|
-
return result.data;
|
|
765
|
-
throw result.error;
|
|
766
|
-
}
|
|
767
|
-
async safeParseAsync(data, params) {
|
|
768
|
-
const ctx = {
|
|
769
|
-
common: {
|
|
770
|
-
issues: [],
|
|
771
|
-
contextualErrorMap: params?.errorMap,
|
|
772
|
-
async: true
|
|
773
|
-
},
|
|
774
|
-
path: params?.path || [],
|
|
775
|
-
schemaErrorMap: this._def.errorMap,
|
|
776
|
-
parent: null,
|
|
777
|
-
data,
|
|
778
|
-
parsedType: getParsedType(data)
|
|
779
|
-
};
|
|
780
|
-
const maybeAsyncResult = this._parse({ data, path: ctx.path, parent: ctx });
|
|
781
|
-
const result = await (isAsync(maybeAsyncResult) ? maybeAsyncResult : Promise.resolve(maybeAsyncResult));
|
|
782
|
-
return handleResult(ctx, result);
|
|
783
|
-
}
|
|
784
|
-
refine(check, message) {
|
|
785
|
-
const getIssueProperties = (val) => {
|
|
786
|
-
if (typeof message === "string" || typeof message === "undefined") {
|
|
787
|
-
return { message };
|
|
788
|
-
} else if (typeof message === "function") {
|
|
789
|
-
return message(val);
|
|
790
|
-
} else {
|
|
791
|
-
return message;
|
|
792
|
-
}
|
|
793
|
-
};
|
|
794
|
-
return this._refinement((val, ctx) => {
|
|
795
|
-
const result = check(val);
|
|
796
|
-
const setError = () => ctx.addIssue({
|
|
797
|
-
code: ZodIssueCode.custom,
|
|
798
|
-
...getIssueProperties(val)
|
|
799
|
-
});
|
|
800
|
-
if (typeof Promise !== "undefined" && result instanceof Promise) {
|
|
801
|
-
return result.then((data) => {
|
|
802
|
-
if (!data) {
|
|
803
|
-
setError();
|
|
804
|
-
return false;
|
|
805
|
-
} else {
|
|
806
|
-
return true;
|
|
807
|
-
}
|
|
808
|
-
});
|
|
809
|
-
}
|
|
810
|
-
if (!result) {
|
|
811
|
-
setError();
|
|
812
|
-
return false;
|
|
813
|
-
} else {
|
|
814
|
-
return true;
|
|
815
|
-
}
|
|
816
|
-
});
|
|
817
|
-
}
|
|
818
|
-
refinement(check, refinementData) {
|
|
819
|
-
return this._refinement((val, ctx) => {
|
|
820
|
-
if (!check(val)) {
|
|
821
|
-
ctx.addIssue(typeof refinementData === "function" ? refinementData(val, ctx) : refinementData);
|
|
822
|
-
return false;
|
|
823
|
-
} else {
|
|
824
|
-
return true;
|
|
825
|
-
}
|
|
826
|
-
});
|
|
827
|
-
}
|
|
828
|
-
_refinement(refinement) {
|
|
829
|
-
return new ZodEffects({
|
|
830
|
-
schema: this,
|
|
831
|
-
typeName: ZodFirstPartyTypeKind.ZodEffects,
|
|
832
|
-
effect: { type: "refinement", refinement }
|
|
833
|
-
});
|
|
834
|
-
}
|
|
835
|
-
superRefine(refinement) {
|
|
836
|
-
return this._refinement(refinement);
|
|
837
|
-
}
|
|
838
|
-
constructor(def) {
|
|
839
|
-
this.spa = this.safeParseAsync;
|
|
840
|
-
this._def = def;
|
|
841
|
-
this.parse = this.parse.bind(this);
|
|
842
|
-
this.safeParse = this.safeParse.bind(this);
|
|
843
|
-
this.parseAsync = this.parseAsync.bind(this);
|
|
844
|
-
this.safeParseAsync = this.safeParseAsync.bind(this);
|
|
845
|
-
this.spa = this.spa.bind(this);
|
|
846
|
-
this.refine = this.refine.bind(this);
|
|
847
|
-
this.refinement = this.refinement.bind(this);
|
|
848
|
-
this.superRefine = this.superRefine.bind(this);
|
|
849
|
-
this.optional = this.optional.bind(this);
|
|
850
|
-
this.nullable = this.nullable.bind(this);
|
|
851
|
-
this.nullish = this.nullish.bind(this);
|
|
852
|
-
this.array = this.array.bind(this);
|
|
853
|
-
this.promise = this.promise.bind(this);
|
|
854
|
-
this.or = this.or.bind(this);
|
|
855
|
-
this.and = this.and.bind(this);
|
|
856
|
-
this.transform = this.transform.bind(this);
|
|
857
|
-
this.brand = this.brand.bind(this);
|
|
858
|
-
this.default = this.default.bind(this);
|
|
859
|
-
this.catch = this.catch.bind(this);
|
|
860
|
-
this.describe = this.describe.bind(this);
|
|
861
|
-
this.pipe = this.pipe.bind(this);
|
|
862
|
-
this.readonly = this.readonly.bind(this);
|
|
863
|
-
this.isNullable = this.isNullable.bind(this);
|
|
864
|
-
this.isOptional = this.isOptional.bind(this);
|
|
865
|
-
this["~standard"] = {
|
|
866
|
-
version: 1,
|
|
867
|
-
vendor: "zod",
|
|
868
|
-
validate: (data) => this["~validate"](data)
|
|
869
|
-
};
|
|
870
|
-
}
|
|
871
|
-
optional() {
|
|
872
|
-
return ZodOptional.create(this, this._def);
|
|
873
|
-
}
|
|
874
|
-
nullable() {
|
|
875
|
-
return ZodNullable.create(this, this._def);
|
|
876
|
-
}
|
|
877
|
-
nullish() {
|
|
878
|
-
return this.nullable().optional();
|
|
879
|
-
}
|
|
880
|
-
array() {
|
|
881
|
-
return ZodArray.create(this);
|
|
882
|
-
}
|
|
883
|
-
promise() {
|
|
884
|
-
return ZodPromise.create(this, this._def);
|
|
885
|
-
}
|
|
886
|
-
or(option) {
|
|
887
|
-
return ZodUnion.create([this, option], this._def);
|
|
888
|
-
}
|
|
889
|
-
and(incoming) {
|
|
890
|
-
return ZodIntersection.create(this, incoming, this._def);
|
|
891
|
-
}
|
|
892
|
-
transform(transform) {
|
|
893
|
-
return new ZodEffects({
|
|
894
|
-
...processCreateParams(this._def),
|
|
895
|
-
schema: this,
|
|
896
|
-
typeName: ZodFirstPartyTypeKind.ZodEffects,
|
|
897
|
-
effect: { type: "transform", transform }
|
|
898
|
-
});
|
|
899
|
-
}
|
|
900
|
-
default(def) {
|
|
901
|
-
const defaultValueFunc = typeof def === "function" ? def : () => def;
|
|
902
|
-
return new ZodDefault({
|
|
903
|
-
...processCreateParams(this._def),
|
|
904
|
-
innerType: this,
|
|
905
|
-
defaultValue: defaultValueFunc,
|
|
906
|
-
typeName: ZodFirstPartyTypeKind.ZodDefault
|
|
907
|
-
});
|
|
908
|
-
}
|
|
909
|
-
brand() {
|
|
910
|
-
return new ZodBranded({
|
|
911
|
-
typeName: ZodFirstPartyTypeKind.ZodBranded,
|
|
912
|
-
type: this,
|
|
913
|
-
...processCreateParams(this._def)
|
|
914
|
-
});
|
|
915
|
-
}
|
|
916
|
-
catch(def) {
|
|
917
|
-
const catchValueFunc = typeof def === "function" ? def : () => def;
|
|
918
|
-
return new ZodCatch({
|
|
919
|
-
...processCreateParams(this._def),
|
|
920
|
-
innerType: this,
|
|
921
|
-
catchValue: catchValueFunc,
|
|
922
|
-
typeName: ZodFirstPartyTypeKind.ZodCatch
|
|
923
|
-
});
|
|
924
|
-
}
|
|
925
|
-
describe(description) {
|
|
926
|
-
const This = this.constructor;
|
|
927
|
-
return new This({
|
|
928
|
-
...this._def,
|
|
929
|
-
description
|
|
930
|
-
});
|
|
931
|
-
}
|
|
932
|
-
pipe(target) {
|
|
933
|
-
return ZodPipeline.create(this, target);
|
|
934
|
-
}
|
|
935
|
-
readonly() {
|
|
936
|
-
return ZodReadonly.create(this);
|
|
937
|
-
}
|
|
938
|
-
isOptional() {
|
|
939
|
-
return this.safeParse(void 0).success;
|
|
940
|
-
}
|
|
941
|
-
isNullable() {
|
|
942
|
-
return this.safeParse(null).success;
|
|
943
|
-
}
|
|
944
|
-
};
|
|
945
|
-
var cuidRegex = /^c[^\s-]{8,}$/i;
|
|
946
|
-
var cuid2Regex = /^[0-9a-z]+$/;
|
|
947
|
-
var ulidRegex = /^[0-9A-HJKMNP-TV-Z]{26}$/i;
|
|
948
|
-
var 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;
|
|
949
|
-
var nanoidRegex = /^[a-z0-9_-]{21}$/i;
|
|
950
|
-
var jwtRegex = /^[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+\.[A-Za-z0-9-_]*$/;
|
|
951
|
-
var 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)?)??$/;
|
|
952
|
-
var emailRegex = /^(?!\.)(?!.*\.\.)([A-Z0-9_'+\-\.]*)[A-Z0-9_+-]@([A-Z0-9][A-Z0-9\-]*\.)+[A-Z]{2,}$/i;
|
|
953
|
-
var _emojiRegex = `^(\\p{Extended_Pictographic}|\\p{Emoji_Component})+$`;
|
|
954
|
-
var emojiRegex;
|
|
955
|
-
var 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])$/;
|
|
956
|
-
var 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])$/;
|
|
957
|
-
var 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]))$/;
|
|
958
|
-
var 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])$/;
|
|
959
|
-
var base64Regex = /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/;
|
|
960
|
-
var base64urlRegex = /^([0-9a-zA-Z-_]{4})*(([0-9a-zA-Z-_]{2}(==)?)|([0-9a-zA-Z-_]{3}(=)?))?$/;
|
|
961
|
-
var 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])))`;
|
|
962
|
-
var dateRegex = new RegExp(`^${dateRegexSource}$`);
|
|
963
|
-
function timeRegexSource(args) {
|
|
964
|
-
let secondsRegexSource = `[0-5]\\d`;
|
|
965
|
-
if (args.precision) {
|
|
966
|
-
secondsRegexSource = `${secondsRegexSource}\\.\\d{${args.precision}}`;
|
|
967
|
-
} else if (args.precision == null) {
|
|
968
|
-
secondsRegexSource = `${secondsRegexSource}(\\.\\d+)?`;
|
|
969
|
-
}
|
|
970
|
-
const secondsQuantifier = args.precision ? "+" : "?";
|
|
971
|
-
return `([01]\\d|2[0-3]):[0-5]\\d(:${secondsRegexSource})${secondsQuantifier}`;
|
|
972
|
-
}
|
|
973
|
-
function timeRegex(args) {
|
|
974
|
-
return new RegExp(`^${timeRegexSource(args)}$`);
|
|
975
|
-
}
|
|
976
|
-
function datetimeRegex(args) {
|
|
977
|
-
let regex = `${dateRegexSource}T${timeRegexSource(args)}`;
|
|
978
|
-
const opts = [];
|
|
979
|
-
opts.push(args.local ? `Z?` : `Z`);
|
|
980
|
-
if (args.offset)
|
|
981
|
-
opts.push(`([+-]\\d{2}:?\\d{2})`);
|
|
982
|
-
regex = `${regex}(${opts.join("|")})`;
|
|
983
|
-
return new RegExp(`^${regex}$`);
|
|
984
|
-
}
|
|
985
|
-
function isValidIP(ip, version) {
|
|
986
|
-
if ((version === "v4" || !version) && ipv4Regex.test(ip)) {
|
|
987
|
-
return true;
|
|
988
|
-
}
|
|
989
|
-
if ((version === "v6" || !version) && ipv6Regex.test(ip)) {
|
|
990
|
-
return true;
|
|
991
|
-
}
|
|
992
|
-
return false;
|
|
993
|
-
}
|
|
994
|
-
function isValidJWT(jwt, alg) {
|
|
995
|
-
if (!jwtRegex.test(jwt))
|
|
996
|
-
return false;
|
|
997
|
-
try {
|
|
998
|
-
const [header] = jwt.split(".");
|
|
999
|
-
if (!header)
|
|
1000
|
-
return false;
|
|
1001
|
-
const base64 = header.replace(/-/g, "+").replace(/_/g, "/").padEnd(header.length + (4 - header.length % 4) % 4, "=");
|
|
1002
|
-
const decoded = JSON.parse(atob(base64));
|
|
1003
|
-
if (typeof decoded !== "object" || decoded === null)
|
|
1004
|
-
return false;
|
|
1005
|
-
if ("typ" in decoded && decoded?.typ !== "JWT")
|
|
1006
|
-
return false;
|
|
1007
|
-
if (!decoded.alg)
|
|
1008
|
-
return false;
|
|
1009
|
-
if (alg && decoded.alg !== alg)
|
|
1010
|
-
return false;
|
|
1011
|
-
return true;
|
|
1012
|
-
} catch {
|
|
1013
|
-
return false;
|
|
1014
|
-
}
|
|
1015
|
-
}
|
|
1016
|
-
function isValidCidr(ip, version) {
|
|
1017
|
-
if ((version === "v4" || !version) && ipv4CidrRegex.test(ip)) {
|
|
1018
|
-
return true;
|
|
1019
|
-
}
|
|
1020
|
-
if ((version === "v6" || !version) && ipv6CidrRegex.test(ip)) {
|
|
1021
|
-
return true;
|
|
1022
|
-
}
|
|
1023
|
-
return false;
|
|
1024
|
-
}
|
|
1025
|
-
var ZodString = class _ZodString extends ZodType {
|
|
1026
|
-
_parse(input) {
|
|
1027
|
-
if (this._def.coerce) {
|
|
1028
|
-
input.data = String(input.data);
|
|
1029
|
-
}
|
|
1030
|
-
const parsedType = this._getType(input);
|
|
1031
|
-
if (parsedType !== ZodParsedType.string) {
|
|
1032
|
-
const ctx2 = this._getOrReturnCtx(input);
|
|
1033
|
-
addIssueToContext(ctx2, {
|
|
1034
|
-
code: ZodIssueCode.invalid_type,
|
|
1035
|
-
expected: ZodParsedType.string,
|
|
1036
|
-
received: ctx2.parsedType
|
|
1037
|
-
});
|
|
1038
|
-
return INVALID;
|
|
1039
|
-
}
|
|
1040
|
-
const status = new ParseStatus();
|
|
1041
|
-
let ctx = void 0;
|
|
1042
|
-
for (const check of this._def.checks) {
|
|
1043
|
-
if (check.kind === "min") {
|
|
1044
|
-
if (input.data.length < check.value) {
|
|
1045
|
-
ctx = this._getOrReturnCtx(input, ctx);
|
|
1046
|
-
addIssueToContext(ctx, {
|
|
1047
|
-
code: ZodIssueCode.too_small,
|
|
1048
|
-
minimum: check.value,
|
|
1049
|
-
type: "string",
|
|
1050
|
-
inclusive: true,
|
|
1051
|
-
exact: false,
|
|
1052
|
-
message: check.message
|
|
1053
|
-
});
|
|
1054
|
-
status.dirty();
|
|
1055
|
-
}
|
|
1056
|
-
} else if (check.kind === "max") {
|
|
1057
|
-
if (input.data.length > check.value) {
|
|
1058
|
-
ctx = this._getOrReturnCtx(input, ctx);
|
|
1059
|
-
addIssueToContext(ctx, {
|
|
1060
|
-
code: ZodIssueCode.too_big,
|
|
1061
|
-
maximum: check.value,
|
|
1062
|
-
type: "string",
|
|
1063
|
-
inclusive: true,
|
|
1064
|
-
exact: false,
|
|
1065
|
-
message: check.message
|
|
1066
|
-
});
|
|
1067
|
-
status.dirty();
|
|
1068
|
-
}
|
|
1069
|
-
} else if (check.kind === "length") {
|
|
1070
|
-
const tooBig = input.data.length > check.value;
|
|
1071
|
-
const tooSmall = input.data.length < check.value;
|
|
1072
|
-
if (tooBig || tooSmall) {
|
|
1073
|
-
ctx = this._getOrReturnCtx(input, ctx);
|
|
1074
|
-
if (tooBig) {
|
|
1075
|
-
addIssueToContext(ctx, {
|
|
1076
|
-
code: ZodIssueCode.too_big,
|
|
1077
|
-
maximum: check.value,
|
|
1078
|
-
type: "string",
|
|
1079
|
-
inclusive: true,
|
|
1080
|
-
exact: true,
|
|
1081
|
-
message: check.message
|
|
1082
|
-
});
|
|
1083
|
-
} else if (tooSmall) {
|
|
1084
|
-
addIssueToContext(ctx, {
|
|
1085
|
-
code: ZodIssueCode.too_small,
|
|
1086
|
-
minimum: check.value,
|
|
1087
|
-
type: "string",
|
|
1088
|
-
inclusive: true,
|
|
1089
|
-
exact: true,
|
|
1090
|
-
message: check.message
|
|
1091
|
-
});
|
|
1092
|
-
}
|
|
1093
|
-
status.dirty();
|
|
1094
|
-
}
|
|
1095
|
-
} else if (check.kind === "email") {
|
|
1096
|
-
if (!emailRegex.test(input.data)) {
|
|
1097
|
-
ctx = this._getOrReturnCtx(input, ctx);
|
|
1098
|
-
addIssueToContext(ctx, {
|
|
1099
|
-
validation: "email",
|
|
1100
|
-
code: ZodIssueCode.invalid_string,
|
|
1101
|
-
message: check.message
|
|
1102
|
-
});
|
|
1103
|
-
status.dirty();
|
|
1104
|
-
}
|
|
1105
|
-
} else if (check.kind === "emoji") {
|
|
1106
|
-
if (!emojiRegex) {
|
|
1107
|
-
emojiRegex = new RegExp(_emojiRegex, "u");
|
|
1108
|
-
}
|
|
1109
|
-
if (!emojiRegex.test(input.data)) {
|
|
1110
|
-
ctx = this._getOrReturnCtx(input, ctx);
|
|
1111
|
-
addIssueToContext(ctx, {
|
|
1112
|
-
validation: "emoji",
|
|
1113
|
-
code: ZodIssueCode.invalid_string,
|
|
1114
|
-
message: check.message
|
|
1115
|
-
});
|
|
1116
|
-
status.dirty();
|
|
1117
|
-
}
|
|
1118
|
-
} else if (check.kind === "uuid") {
|
|
1119
|
-
if (!uuidRegex.test(input.data)) {
|
|
1120
|
-
ctx = this._getOrReturnCtx(input, ctx);
|
|
1121
|
-
addIssueToContext(ctx, {
|
|
1122
|
-
validation: "uuid",
|
|
1123
|
-
code: ZodIssueCode.invalid_string,
|
|
1124
|
-
message: check.message
|
|
1125
|
-
});
|
|
1126
|
-
status.dirty();
|
|
1127
|
-
}
|
|
1128
|
-
} else if (check.kind === "nanoid") {
|
|
1129
|
-
if (!nanoidRegex.test(input.data)) {
|
|
1130
|
-
ctx = this._getOrReturnCtx(input, ctx);
|
|
1131
|
-
addIssueToContext(ctx, {
|
|
1132
|
-
validation: "nanoid",
|
|
1133
|
-
code: ZodIssueCode.invalid_string,
|
|
1134
|
-
message: check.message
|
|
1135
|
-
});
|
|
1136
|
-
status.dirty();
|
|
1137
|
-
}
|
|
1138
|
-
} else if (check.kind === "cuid") {
|
|
1139
|
-
if (!cuidRegex.test(input.data)) {
|
|
1140
|
-
ctx = this._getOrReturnCtx(input, ctx);
|
|
1141
|
-
addIssueToContext(ctx, {
|
|
1142
|
-
validation: "cuid",
|
|
1143
|
-
code: ZodIssueCode.invalid_string,
|
|
1144
|
-
message: check.message
|
|
1145
|
-
});
|
|
1146
|
-
status.dirty();
|
|
1147
|
-
}
|
|
1148
|
-
} else if (check.kind === "cuid2") {
|
|
1149
|
-
if (!cuid2Regex.test(input.data)) {
|
|
1150
|
-
ctx = this._getOrReturnCtx(input, ctx);
|
|
1151
|
-
addIssueToContext(ctx, {
|
|
1152
|
-
validation: "cuid2",
|
|
1153
|
-
code: ZodIssueCode.invalid_string,
|
|
1154
|
-
message: check.message
|
|
1155
|
-
});
|
|
1156
|
-
status.dirty();
|
|
1157
|
-
}
|
|
1158
|
-
} else if (check.kind === "ulid") {
|
|
1159
|
-
if (!ulidRegex.test(input.data)) {
|
|
1160
|
-
ctx = this._getOrReturnCtx(input, ctx);
|
|
1161
|
-
addIssueToContext(ctx, {
|
|
1162
|
-
validation: "ulid",
|
|
1163
|
-
code: ZodIssueCode.invalid_string,
|
|
1164
|
-
message: check.message
|
|
1165
|
-
});
|
|
1166
|
-
status.dirty();
|
|
1167
|
-
}
|
|
1168
|
-
} else if (check.kind === "url") {
|
|
1169
|
-
try {
|
|
1170
|
-
new URL(input.data);
|
|
1171
|
-
} catch {
|
|
1172
|
-
ctx = this._getOrReturnCtx(input, ctx);
|
|
1173
|
-
addIssueToContext(ctx, {
|
|
1174
|
-
validation: "url",
|
|
1175
|
-
code: ZodIssueCode.invalid_string,
|
|
1176
|
-
message: check.message
|
|
1177
|
-
});
|
|
1178
|
-
status.dirty();
|
|
1179
|
-
}
|
|
1180
|
-
} else if (check.kind === "regex") {
|
|
1181
|
-
check.regex.lastIndex = 0;
|
|
1182
|
-
const testResult = check.regex.test(input.data);
|
|
1183
|
-
if (!testResult) {
|
|
1184
|
-
ctx = this._getOrReturnCtx(input, ctx);
|
|
1185
|
-
addIssueToContext(ctx, {
|
|
1186
|
-
validation: "regex",
|
|
1187
|
-
code: ZodIssueCode.invalid_string,
|
|
1188
|
-
message: check.message
|
|
1189
|
-
});
|
|
1190
|
-
status.dirty();
|
|
1191
|
-
}
|
|
1192
|
-
} else if (check.kind === "trim") {
|
|
1193
|
-
input.data = input.data.trim();
|
|
1194
|
-
} else if (check.kind === "includes") {
|
|
1195
|
-
if (!input.data.includes(check.value, check.position)) {
|
|
1196
|
-
ctx = this._getOrReturnCtx(input, ctx);
|
|
1197
|
-
addIssueToContext(ctx, {
|
|
1198
|
-
code: ZodIssueCode.invalid_string,
|
|
1199
|
-
validation: { includes: check.value, position: check.position },
|
|
1200
|
-
message: check.message
|
|
1201
|
-
});
|
|
1202
|
-
status.dirty();
|
|
1203
|
-
}
|
|
1204
|
-
} else if (check.kind === "toLowerCase") {
|
|
1205
|
-
input.data = input.data.toLowerCase();
|
|
1206
|
-
} else if (check.kind === "toUpperCase") {
|
|
1207
|
-
input.data = input.data.toUpperCase();
|
|
1208
|
-
} else if (check.kind === "startsWith") {
|
|
1209
|
-
if (!input.data.startsWith(check.value)) {
|
|
1210
|
-
ctx = this._getOrReturnCtx(input, ctx);
|
|
1211
|
-
addIssueToContext(ctx, {
|
|
1212
|
-
code: ZodIssueCode.invalid_string,
|
|
1213
|
-
validation: { startsWith: check.value },
|
|
1214
|
-
message: check.message
|
|
1215
|
-
});
|
|
1216
|
-
status.dirty();
|
|
1217
|
-
}
|
|
1218
|
-
} else if (check.kind === "endsWith") {
|
|
1219
|
-
if (!input.data.endsWith(check.value)) {
|
|
1220
|
-
ctx = this._getOrReturnCtx(input, ctx);
|
|
1221
|
-
addIssueToContext(ctx, {
|
|
1222
|
-
code: ZodIssueCode.invalid_string,
|
|
1223
|
-
validation: { endsWith: check.value },
|
|
1224
|
-
message: check.message
|
|
1225
|
-
});
|
|
1226
|
-
status.dirty();
|
|
1227
|
-
}
|
|
1228
|
-
} else if (check.kind === "datetime") {
|
|
1229
|
-
const regex = datetimeRegex(check);
|
|
1230
|
-
if (!regex.test(input.data)) {
|
|
1231
|
-
ctx = this._getOrReturnCtx(input, ctx);
|
|
1232
|
-
addIssueToContext(ctx, {
|
|
1233
|
-
code: ZodIssueCode.invalid_string,
|
|
1234
|
-
validation: "datetime",
|
|
1235
|
-
message: check.message
|
|
1236
|
-
});
|
|
1237
|
-
status.dirty();
|
|
1238
|
-
}
|
|
1239
|
-
} else if (check.kind === "date") {
|
|
1240
|
-
const regex = dateRegex;
|
|
1241
|
-
if (!regex.test(input.data)) {
|
|
1242
|
-
ctx = this._getOrReturnCtx(input, ctx);
|
|
1243
|
-
addIssueToContext(ctx, {
|
|
1244
|
-
code: ZodIssueCode.invalid_string,
|
|
1245
|
-
validation: "date",
|
|
1246
|
-
message: check.message
|
|
1247
|
-
});
|
|
1248
|
-
status.dirty();
|
|
1249
|
-
}
|
|
1250
|
-
} else if (check.kind === "time") {
|
|
1251
|
-
const regex = timeRegex(check);
|
|
1252
|
-
if (!regex.test(input.data)) {
|
|
1253
|
-
ctx = this._getOrReturnCtx(input, ctx);
|
|
1254
|
-
addIssueToContext(ctx, {
|
|
1255
|
-
code: ZodIssueCode.invalid_string,
|
|
1256
|
-
validation: "time",
|
|
1257
|
-
message: check.message
|
|
1258
|
-
});
|
|
1259
|
-
status.dirty();
|
|
1260
|
-
}
|
|
1261
|
-
} else if (check.kind === "duration") {
|
|
1262
|
-
if (!durationRegex.test(input.data)) {
|
|
1263
|
-
ctx = this._getOrReturnCtx(input, ctx);
|
|
1264
|
-
addIssueToContext(ctx, {
|
|
1265
|
-
validation: "duration",
|
|
1266
|
-
code: ZodIssueCode.invalid_string,
|
|
1267
|
-
message: check.message
|
|
1268
|
-
});
|
|
1269
|
-
status.dirty();
|
|
1270
|
-
}
|
|
1271
|
-
} else if (check.kind === "ip") {
|
|
1272
|
-
if (!isValidIP(input.data, check.version)) {
|
|
1273
|
-
ctx = this._getOrReturnCtx(input, ctx);
|
|
1274
|
-
addIssueToContext(ctx, {
|
|
1275
|
-
validation: "ip",
|
|
1276
|
-
code: ZodIssueCode.invalid_string,
|
|
1277
|
-
message: check.message
|
|
1278
|
-
});
|
|
1279
|
-
status.dirty();
|
|
1280
|
-
}
|
|
1281
|
-
} else if (check.kind === "jwt") {
|
|
1282
|
-
if (!isValidJWT(input.data, check.alg)) {
|
|
1283
|
-
ctx = this._getOrReturnCtx(input, ctx);
|
|
1284
|
-
addIssueToContext(ctx, {
|
|
1285
|
-
validation: "jwt",
|
|
1286
|
-
code: ZodIssueCode.invalid_string,
|
|
1287
|
-
message: check.message
|
|
1288
|
-
});
|
|
1289
|
-
status.dirty();
|
|
1290
|
-
}
|
|
1291
|
-
} else if (check.kind === "cidr") {
|
|
1292
|
-
if (!isValidCidr(input.data, check.version)) {
|
|
1293
|
-
ctx = this._getOrReturnCtx(input, ctx);
|
|
1294
|
-
addIssueToContext(ctx, {
|
|
1295
|
-
validation: "cidr",
|
|
1296
|
-
code: ZodIssueCode.invalid_string,
|
|
1297
|
-
message: check.message
|
|
1298
|
-
});
|
|
1299
|
-
status.dirty();
|
|
1300
|
-
}
|
|
1301
|
-
} else if (check.kind === "base64") {
|
|
1302
|
-
if (!base64Regex.test(input.data)) {
|
|
1303
|
-
ctx = this._getOrReturnCtx(input, ctx);
|
|
1304
|
-
addIssueToContext(ctx, {
|
|
1305
|
-
validation: "base64",
|
|
1306
|
-
code: ZodIssueCode.invalid_string,
|
|
1307
|
-
message: check.message
|
|
1308
|
-
});
|
|
1309
|
-
status.dirty();
|
|
1310
|
-
}
|
|
1311
|
-
} else if (check.kind === "base64url") {
|
|
1312
|
-
if (!base64urlRegex.test(input.data)) {
|
|
1313
|
-
ctx = this._getOrReturnCtx(input, ctx);
|
|
1314
|
-
addIssueToContext(ctx, {
|
|
1315
|
-
validation: "base64url",
|
|
1316
|
-
code: ZodIssueCode.invalid_string,
|
|
1317
|
-
message: check.message
|
|
1318
|
-
});
|
|
1319
|
-
status.dirty();
|
|
1320
|
-
}
|
|
1321
|
-
} else {
|
|
1322
|
-
util.assertNever(check);
|
|
1323
|
-
}
|
|
1324
|
-
}
|
|
1325
|
-
return { status: status.value, value: input.data };
|
|
1326
|
-
}
|
|
1327
|
-
_regex(regex, validation, message) {
|
|
1328
|
-
return this.refinement((data) => regex.test(data), {
|
|
1329
|
-
validation,
|
|
1330
|
-
code: ZodIssueCode.invalid_string,
|
|
1331
|
-
...errorUtil.errToObj(message)
|
|
1332
|
-
});
|
|
1333
|
-
}
|
|
1334
|
-
_addCheck(check) {
|
|
1335
|
-
return new _ZodString({
|
|
1336
|
-
...this._def,
|
|
1337
|
-
checks: [...this._def.checks, check]
|
|
1338
|
-
});
|
|
1339
|
-
}
|
|
1340
|
-
email(message) {
|
|
1341
|
-
return this._addCheck({ kind: "email", ...errorUtil.errToObj(message) });
|
|
1342
|
-
}
|
|
1343
|
-
url(message) {
|
|
1344
|
-
return this._addCheck({ kind: "url", ...errorUtil.errToObj(message) });
|
|
1345
|
-
}
|
|
1346
|
-
emoji(message) {
|
|
1347
|
-
return this._addCheck({ kind: "emoji", ...errorUtil.errToObj(message) });
|
|
1348
|
-
}
|
|
1349
|
-
uuid(message) {
|
|
1350
|
-
return this._addCheck({ kind: "uuid", ...errorUtil.errToObj(message) });
|
|
1351
|
-
}
|
|
1352
|
-
nanoid(message) {
|
|
1353
|
-
return this._addCheck({ kind: "nanoid", ...errorUtil.errToObj(message) });
|
|
1354
|
-
}
|
|
1355
|
-
cuid(message) {
|
|
1356
|
-
return this._addCheck({ kind: "cuid", ...errorUtil.errToObj(message) });
|
|
1357
|
-
}
|
|
1358
|
-
cuid2(message) {
|
|
1359
|
-
return this._addCheck({ kind: "cuid2", ...errorUtil.errToObj(message) });
|
|
1360
|
-
}
|
|
1361
|
-
ulid(message) {
|
|
1362
|
-
return this._addCheck({ kind: "ulid", ...errorUtil.errToObj(message) });
|
|
1363
|
-
}
|
|
1364
|
-
base64(message) {
|
|
1365
|
-
return this._addCheck({ kind: "base64", ...errorUtil.errToObj(message) });
|
|
1366
|
-
}
|
|
1367
|
-
base64url(message) {
|
|
1368
|
-
return this._addCheck({
|
|
1369
|
-
kind: "base64url",
|
|
1370
|
-
...errorUtil.errToObj(message)
|
|
1371
|
-
});
|
|
1372
|
-
}
|
|
1373
|
-
jwt(options) {
|
|
1374
|
-
return this._addCheck({ kind: "jwt", ...errorUtil.errToObj(options) });
|
|
1375
|
-
}
|
|
1376
|
-
ip(options) {
|
|
1377
|
-
return this._addCheck({ kind: "ip", ...errorUtil.errToObj(options) });
|
|
1378
|
-
}
|
|
1379
|
-
cidr(options) {
|
|
1380
|
-
return this._addCheck({ kind: "cidr", ...errorUtil.errToObj(options) });
|
|
1381
|
-
}
|
|
1382
|
-
datetime(options) {
|
|
1383
|
-
if (typeof options === "string") {
|
|
1384
|
-
return this._addCheck({
|
|
1385
|
-
kind: "datetime",
|
|
1386
|
-
precision: null,
|
|
1387
|
-
offset: false,
|
|
1388
|
-
local: false,
|
|
1389
|
-
message: options
|
|
1390
|
-
});
|
|
1391
|
-
}
|
|
1392
|
-
return this._addCheck({
|
|
1393
|
-
kind: "datetime",
|
|
1394
|
-
precision: typeof options?.precision === "undefined" ? null : options?.precision,
|
|
1395
|
-
offset: options?.offset ?? false,
|
|
1396
|
-
local: options?.local ?? false,
|
|
1397
|
-
...errorUtil.errToObj(options?.message)
|
|
1398
|
-
});
|
|
1399
|
-
}
|
|
1400
|
-
date(message) {
|
|
1401
|
-
return this._addCheck({ kind: "date", message });
|
|
1402
|
-
}
|
|
1403
|
-
time(options) {
|
|
1404
|
-
if (typeof options === "string") {
|
|
1405
|
-
return this._addCheck({
|
|
1406
|
-
kind: "time",
|
|
1407
|
-
precision: null,
|
|
1408
|
-
message: options
|
|
1409
|
-
});
|
|
1410
|
-
}
|
|
1411
|
-
return this._addCheck({
|
|
1412
|
-
kind: "time",
|
|
1413
|
-
precision: typeof options?.precision === "undefined" ? null : options?.precision,
|
|
1414
|
-
...errorUtil.errToObj(options?.message)
|
|
1415
|
-
});
|
|
1416
|
-
}
|
|
1417
|
-
duration(message) {
|
|
1418
|
-
return this._addCheck({ kind: "duration", ...errorUtil.errToObj(message) });
|
|
1419
|
-
}
|
|
1420
|
-
regex(regex, message) {
|
|
1421
|
-
return this._addCheck({
|
|
1422
|
-
kind: "regex",
|
|
1423
|
-
regex,
|
|
1424
|
-
...errorUtil.errToObj(message)
|
|
1425
|
-
});
|
|
1426
|
-
}
|
|
1427
|
-
includes(value, options) {
|
|
1428
|
-
return this._addCheck({
|
|
1429
|
-
kind: "includes",
|
|
1430
|
-
value,
|
|
1431
|
-
position: options?.position,
|
|
1432
|
-
...errorUtil.errToObj(options?.message)
|
|
1433
|
-
});
|
|
1434
|
-
}
|
|
1435
|
-
startsWith(value, message) {
|
|
1436
|
-
return this._addCheck({
|
|
1437
|
-
kind: "startsWith",
|
|
1438
|
-
value,
|
|
1439
|
-
...errorUtil.errToObj(message)
|
|
1440
|
-
});
|
|
1441
|
-
}
|
|
1442
|
-
endsWith(value, message) {
|
|
1443
|
-
return this._addCheck({
|
|
1444
|
-
kind: "endsWith",
|
|
1445
|
-
value,
|
|
1446
|
-
...errorUtil.errToObj(message)
|
|
1447
|
-
});
|
|
1448
|
-
}
|
|
1449
|
-
min(minLength, message) {
|
|
1450
|
-
return this._addCheck({
|
|
1451
|
-
kind: "min",
|
|
1452
|
-
value: minLength,
|
|
1453
|
-
...errorUtil.errToObj(message)
|
|
1454
|
-
});
|
|
1455
|
-
}
|
|
1456
|
-
max(maxLength, message) {
|
|
1457
|
-
return this._addCheck({
|
|
1458
|
-
kind: "max",
|
|
1459
|
-
value: maxLength,
|
|
1460
|
-
...errorUtil.errToObj(message)
|
|
1461
|
-
});
|
|
1462
|
-
}
|
|
1463
|
-
length(len, message) {
|
|
1464
|
-
return this._addCheck({
|
|
1465
|
-
kind: "length",
|
|
1466
|
-
value: len,
|
|
1467
|
-
...errorUtil.errToObj(message)
|
|
1468
|
-
});
|
|
1469
|
-
}
|
|
1470
|
-
/**
|
|
1471
|
-
* Equivalent to `.min(1)`
|
|
1472
|
-
*/
|
|
1473
|
-
nonempty(message) {
|
|
1474
|
-
return this.min(1, errorUtil.errToObj(message));
|
|
1475
|
-
}
|
|
1476
|
-
trim() {
|
|
1477
|
-
return new _ZodString({
|
|
1478
|
-
...this._def,
|
|
1479
|
-
checks: [...this._def.checks, { kind: "trim" }]
|
|
1480
|
-
});
|
|
1481
|
-
}
|
|
1482
|
-
toLowerCase() {
|
|
1483
|
-
return new _ZodString({
|
|
1484
|
-
...this._def,
|
|
1485
|
-
checks: [...this._def.checks, { kind: "toLowerCase" }]
|
|
1486
|
-
});
|
|
1487
|
-
}
|
|
1488
|
-
toUpperCase() {
|
|
1489
|
-
return new _ZodString({
|
|
1490
|
-
...this._def,
|
|
1491
|
-
checks: [...this._def.checks, { kind: "toUpperCase" }]
|
|
1492
|
-
});
|
|
1493
|
-
}
|
|
1494
|
-
get isDatetime() {
|
|
1495
|
-
return !!this._def.checks.find((ch) => ch.kind === "datetime");
|
|
1496
|
-
}
|
|
1497
|
-
get isDate() {
|
|
1498
|
-
return !!this._def.checks.find((ch) => ch.kind === "date");
|
|
1499
|
-
}
|
|
1500
|
-
get isTime() {
|
|
1501
|
-
return !!this._def.checks.find((ch) => ch.kind === "time");
|
|
1502
|
-
}
|
|
1503
|
-
get isDuration() {
|
|
1504
|
-
return !!this._def.checks.find((ch) => ch.kind === "duration");
|
|
1505
|
-
}
|
|
1506
|
-
get isEmail() {
|
|
1507
|
-
return !!this._def.checks.find((ch) => ch.kind === "email");
|
|
1508
|
-
}
|
|
1509
|
-
get isURL() {
|
|
1510
|
-
return !!this._def.checks.find((ch) => ch.kind === "url");
|
|
1511
|
-
}
|
|
1512
|
-
get isEmoji() {
|
|
1513
|
-
return !!this._def.checks.find((ch) => ch.kind === "emoji");
|
|
1514
|
-
}
|
|
1515
|
-
get isUUID() {
|
|
1516
|
-
return !!this._def.checks.find((ch) => ch.kind === "uuid");
|
|
1517
|
-
}
|
|
1518
|
-
get isNANOID() {
|
|
1519
|
-
return !!this._def.checks.find((ch) => ch.kind === "nanoid");
|
|
1520
|
-
}
|
|
1521
|
-
get isCUID() {
|
|
1522
|
-
return !!this._def.checks.find((ch) => ch.kind === "cuid");
|
|
1523
|
-
}
|
|
1524
|
-
get isCUID2() {
|
|
1525
|
-
return !!this._def.checks.find((ch) => ch.kind === "cuid2");
|
|
1526
|
-
}
|
|
1527
|
-
get isULID() {
|
|
1528
|
-
return !!this._def.checks.find((ch) => ch.kind === "ulid");
|
|
1529
|
-
}
|
|
1530
|
-
get isIP() {
|
|
1531
|
-
return !!this._def.checks.find((ch) => ch.kind === "ip");
|
|
1532
|
-
}
|
|
1533
|
-
get isCIDR() {
|
|
1534
|
-
return !!this._def.checks.find((ch) => ch.kind === "cidr");
|
|
1535
|
-
}
|
|
1536
|
-
get isBase64() {
|
|
1537
|
-
return !!this._def.checks.find((ch) => ch.kind === "base64");
|
|
1538
|
-
}
|
|
1539
|
-
get isBase64url() {
|
|
1540
|
-
return !!this._def.checks.find((ch) => ch.kind === "base64url");
|
|
1541
|
-
}
|
|
1542
|
-
get minLength() {
|
|
1543
|
-
let min = null;
|
|
1544
|
-
for (const ch of this._def.checks) {
|
|
1545
|
-
if (ch.kind === "min") {
|
|
1546
|
-
if (min === null || ch.value > min)
|
|
1547
|
-
min = ch.value;
|
|
1548
|
-
}
|
|
1549
|
-
}
|
|
1550
|
-
return min;
|
|
1551
|
-
}
|
|
1552
|
-
get maxLength() {
|
|
1553
|
-
let max = null;
|
|
1554
|
-
for (const ch of this._def.checks) {
|
|
1555
|
-
if (ch.kind === "max") {
|
|
1556
|
-
if (max === null || ch.value < max)
|
|
1557
|
-
max = ch.value;
|
|
1558
|
-
}
|
|
1559
|
-
}
|
|
1560
|
-
return max;
|
|
1561
|
-
}
|
|
1562
|
-
};
|
|
1563
|
-
ZodString.create = (params) => {
|
|
1564
|
-
return new ZodString({
|
|
1565
|
-
checks: [],
|
|
1566
|
-
typeName: ZodFirstPartyTypeKind.ZodString,
|
|
1567
|
-
coerce: params?.coerce ?? false,
|
|
1568
|
-
...processCreateParams(params)
|
|
1569
|
-
});
|
|
1570
|
-
};
|
|
1571
|
-
function floatSafeRemainder(val, step) {
|
|
1572
|
-
const valDecCount = (val.toString().split(".")[1] || "").length;
|
|
1573
|
-
const stepDecCount = (step.toString().split(".")[1] || "").length;
|
|
1574
|
-
const decCount = valDecCount > stepDecCount ? valDecCount : stepDecCount;
|
|
1575
|
-
const valInt = Number.parseInt(val.toFixed(decCount).replace(".", ""));
|
|
1576
|
-
const stepInt = Number.parseInt(step.toFixed(decCount).replace(".", ""));
|
|
1577
|
-
return valInt % stepInt / 10 ** decCount;
|
|
1578
|
-
}
|
|
1579
|
-
var ZodNumber = class _ZodNumber extends ZodType {
|
|
1580
|
-
constructor() {
|
|
1581
|
-
super(...arguments);
|
|
1582
|
-
this.min = this.gte;
|
|
1583
|
-
this.max = this.lte;
|
|
1584
|
-
this.step = this.multipleOf;
|
|
1585
|
-
}
|
|
1586
|
-
_parse(input) {
|
|
1587
|
-
if (this._def.coerce) {
|
|
1588
|
-
input.data = Number(input.data);
|
|
1589
|
-
}
|
|
1590
|
-
const parsedType = this._getType(input);
|
|
1591
|
-
if (parsedType !== ZodParsedType.number) {
|
|
1592
|
-
const ctx2 = this._getOrReturnCtx(input);
|
|
1593
|
-
addIssueToContext(ctx2, {
|
|
1594
|
-
code: ZodIssueCode.invalid_type,
|
|
1595
|
-
expected: ZodParsedType.number,
|
|
1596
|
-
received: ctx2.parsedType
|
|
1597
|
-
});
|
|
1598
|
-
return INVALID;
|
|
1599
|
-
}
|
|
1600
|
-
let ctx = void 0;
|
|
1601
|
-
const status = new ParseStatus();
|
|
1602
|
-
for (const check of this._def.checks) {
|
|
1603
|
-
if (check.kind === "int") {
|
|
1604
|
-
if (!util.isInteger(input.data)) {
|
|
1605
|
-
ctx = this._getOrReturnCtx(input, ctx);
|
|
1606
|
-
addIssueToContext(ctx, {
|
|
1607
|
-
code: ZodIssueCode.invalid_type,
|
|
1608
|
-
expected: "integer",
|
|
1609
|
-
received: "float",
|
|
1610
|
-
message: check.message
|
|
1611
|
-
});
|
|
1612
|
-
status.dirty();
|
|
1613
|
-
}
|
|
1614
|
-
} else if (check.kind === "min") {
|
|
1615
|
-
const tooSmall = check.inclusive ? input.data < check.value : input.data <= check.value;
|
|
1616
|
-
if (tooSmall) {
|
|
1617
|
-
ctx = this._getOrReturnCtx(input, ctx);
|
|
1618
|
-
addIssueToContext(ctx, {
|
|
1619
|
-
code: ZodIssueCode.too_small,
|
|
1620
|
-
minimum: check.value,
|
|
1621
|
-
type: "number",
|
|
1622
|
-
inclusive: check.inclusive,
|
|
1623
|
-
exact: false,
|
|
1624
|
-
message: check.message
|
|
1625
|
-
});
|
|
1626
|
-
status.dirty();
|
|
1627
|
-
}
|
|
1628
|
-
} else if (check.kind === "max") {
|
|
1629
|
-
const tooBig = check.inclusive ? input.data > check.value : input.data >= check.value;
|
|
1630
|
-
if (tooBig) {
|
|
1631
|
-
ctx = this._getOrReturnCtx(input, ctx);
|
|
1632
|
-
addIssueToContext(ctx, {
|
|
1633
|
-
code: ZodIssueCode.too_big,
|
|
1634
|
-
maximum: check.value,
|
|
1635
|
-
type: "number",
|
|
1636
|
-
inclusive: check.inclusive,
|
|
1637
|
-
exact: false,
|
|
1638
|
-
message: check.message
|
|
1639
|
-
});
|
|
1640
|
-
status.dirty();
|
|
1641
|
-
}
|
|
1642
|
-
} else if (check.kind === "multipleOf") {
|
|
1643
|
-
if (floatSafeRemainder(input.data, check.value) !== 0) {
|
|
1644
|
-
ctx = this._getOrReturnCtx(input, ctx);
|
|
1645
|
-
addIssueToContext(ctx, {
|
|
1646
|
-
code: ZodIssueCode.not_multiple_of,
|
|
1647
|
-
multipleOf: check.value,
|
|
1648
|
-
message: check.message
|
|
1649
|
-
});
|
|
1650
|
-
status.dirty();
|
|
1651
|
-
}
|
|
1652
|
-
} else if (check.kind === "finite") {
|
|
1653
|
-
if (!Number.isFinite(input.data)) {
|
|
1654
|
-
ctx = this._getOrReturnCtx(input, ctx);
|
|
1655
|
-
addIssueToContext(ctx, {
|
|
1656
|
-
code: ZodIssueCode.not_finite,
|
|
1657
|
-
message: check.message
|
|
1658
|
-
});
|
|
1659
|
-
status.dirty();
|
|
1660
|
-
}
|
|
1661
|
-
} else {
|
|
1662
|
-
util.assertNever(check);
|
|
1663
|
-
}
|
|
1664
|
-
}
|
|
1665
|
-
return { status: status.value, value: input.data };
|
|
1666
|
-
}
|
|
1667
|
-
gte(value, message) {
|
|
1668
|
-
return this.setLimit("min", value, true, errorUtil.toString(message));
|
|
1669
|
-
}
|
|
1670
|
-
gt(value, message) {
|
|
1671
|
-
return this.setLimit("min", value, false, errorUtil.toString(message));
|
|
1672
|
-
}
|
|
1673
|
-
lte(value, message) {
|
|
1674
|
-
return this.setLimit("max", value, true, errorUtil.toString(message));
|
|
1675
|
-
}
|
|
1676
|
-
lt(value, message) {
|
|
1677
|
-
return this.setLimit("max", value, false, errorUtil.toString(message));
|
|
1678
|
-
}
|
|
1679
|
-
setLimit(kind, value, inclusive, message) {
|
|
1680
|
-
return new _ZodNumber({
|
|
1681
|
-
...this._def,
|
|
1682
|
-
checks: [
|
|
1683
|
-
...this._def.checks,
|
|
1684
|
-
{
|
|
1685
|
-
kind,
|
|
1686
|
-
value,
|
|
1687
|
-
inclusive,
|
|
1688
|
-
message: errorUtil.toString(message)
|
|
1689
|
-
}
|
|
1690
|
-
]
|
|
1691
|
-
});
|
|
1692
|
-
}
|
|
1693
|
-
_addCheck(check) {
|
|
1694
|
-
return new _ZodNumber({
|
|
1695
|
-
...this._def,
|
|
1696
|
-
checks: [...this._def.checks, check]
|
|
1697
|
-
});
|
|
1698
|
-
}
|
|
1699
|
-
int(message) {
|
|
1700
|
-
return this._addCheck({
|
|
1701
|
-
kind: "int",
|
|
1702
|
-
message: errorUtil.toString(message)
|
|
1703
|
-
});
|
|
1704
|
-
}
|
|
1705
|
-
positive(message) {
|
|
1706
|
-
return this._addCheck({
|
|
1707
|
-
kind: "min",
|
|
1708
|
-
value: 0,
|
|
1709
|
-
inclusive: false,
|
|
1710
|
-
message: errorUtil.toString(message)
|
|
1711
|
-
});
|
|
1712
|
-
}
|
|
1713
|
-
negative(message) {
|
|
1714
|
-
return this._addCheck({
|
|
1715
|
-
kind: "max",
|
|
1716
|
-
value: 0,
|
|
1717
|
-
inclusive: false,
|
|
1718
|
-
message: errorUtil.toString(message)
|
|
1719
|
-
});
|
|
1720
|
-
}
|
|
1721
|
-
nonpositive(message) {
|
|
1722
|
-
return this._addCheck({
|
|
1723
|
-
kind: "max",
|
|
1724
|
-
value: 0,
|
|
1725
|
-
inclusive: true,
|
|
1726
|
-
message: errorUtil.toString(message)
|
|
1727
|
-
});
|
|
1728
|
-
}
|
|
1729
|
-
nonnegative(message) {
|
|
1730
|
-
return this._addCheck({
|
|
1731
|
-
kind: "min",
|
|
1732
|
-
value: 0,
|
|
1733
|
-
inclusive: true,
|
|
1734
|
-
message: errorUtil.toString(message)
|
|
1735
|
-
});
|
|
1736
|
-
}
|
|
1737
|
-
multipleOf(value, message) {
|
|
1738
|
-
return this._addCheck({
|
|
1739
|
-
kind: "multipleOf",
|
|
1740
|
-
value,
|
|
1741
|
-
message: errorUtil.toString(message)
|
|
1742
|
-
});
|
|
1743
|
-
}
|
|
1744
|
-
finite(message) {
|
|
1745
|
-
return this._addCheck({
|
|
1746
|
-
kind: "finite",
|
|
1747
|
-
message: errorUtil.toString(message)
|
|
1748
|
-
});
|
|
1749
|
-
}
|
|
1750
|
-
safe(message) {
|
|
1751
|
-
return this._addCheck({
|
|
1752
|
-
kind: "min",
|
|
1753
|
-
inclusive: true,
|
|
1754
|
-
value: Number.MIN_SAFE_INTEGER,
|
|
1755
|
-
message: errorUtil.toString(message)
|
|
1756
|
-
})._addCheck({
|
|
1757
|
-
kind: "max",
|
|
1758
|
-
inclusive: true,
|
|
1759
|
-
value: Number.MAX_SAFE_INTEGER,
|
|
1760
|
-
message: errorUtil.toString(message)
|
|
1761
|
-
});
|
|
1762
|
-
}
|
|
1763
|
-
get minValue() {
|
|
1764
|
-
let min = null;
|
|
1765
|
-
for (const ch of this._def.checks) {
|
|
1766
|
-
if (ch.kind === "min") {
|
|
1767
|
-
if (min === null || ch.value > min)
|
|
1768
|
-
min = ch.value;
|
|
1769
|
-
}
|
|
1770
|
-
}
|
|
1771
|
-
return min;
|
|
1772
|
-
}
|
|
1773
|
-
get maxValue() {
|
|
1774
|
-
let max = null;
|
|
1775
|
-
for (const ch of this._def.checks) {
|
|
1776
|
-
if (ch.kind === "max") {
|
|
1777
|
-
if (max === null || ch.value < max)
|
|
1778
|
-
max = ch.value;
|
|
1779
|
-
}
|
|
1780
|
-
}
|
|
1781
|
-
return max;
|
|
1782
|
-
}
|
|
1783
|
-
get isInt() {
|
|
1784
|
-
return !!this._def.checks.find((ch) => ch.kind === "int" || ch.kind === "multipleOf" && util.isInteger(ch.value));
|
|
1785
|
-
}
|
|
1786
|
-
get isFinite() {
|
|
1787
|
-
let max = null;
|
|
1788
|
-
let min = null;
|
|
1789
|
-
for (const ch of this._def.checks) {
|
|
1790
|
-
if (ch.kind === "finite" || ch.kind === "int" || ch.kind === "multipleOf") {
|
|
1791
|
-
return true;
|
|
1792
|
-
} else if (ch.kind === "min") {
|
|
1793
|
-
if (min === null || ch.value > min)
|
|
1794
|
-
min = ch.value;
|
|
1795
|
-
} else if (ch.kind === "max") {
|
|
1796
|
-
if (max === null || ch.value < max)
|
|
1797
|
-
max = ch.value;
|
|
1798
|
-
}
|
|
1799
|
-
}
|
|
1800
|
-
return Number.isFinite(min) && Number.isFinite(max);
|
|
1801
|
-
}
|
|
1802
|
-
};
|
|
1803
|
-
ZodNumber.create = (params) => {
|
|
1804
|
-
return new ZodNumber({
|
|
1805
|
-
checks: [],
|
|
1806
|
-
typeName: ZodFirstPartyTypeKind.ZodNumber,
|
|
1807
|
-
coerce: params?.coerce || false,
|
|
1808
|
-
...processCreateParams(params)
|
|
1809
|
-
});
|
|
1810
|
-
};
|
|
1811
|
-
var ZodBigInt = class _ZodBigInt extends ZodType {
|
|
1812
|
-
constructor() {
|
|
1813
|
-
super(...arguments);
|
|
1814
|
-
this.min = this.gte;
|
|
1815
|
-
this.max = this.lte;
|
|
1816
|
-
}
|
|
1817
|
-
_parse(input) {
|
|
1818
|
-
if (this._def.coerce) {
|
|
1819
|
-
try {
|
|
1820
|
-
input.data = BigInt(input.data);
|
|
1821
|
-
} catch {
|
|
1822
|
-
return this._getInvalidInput(input);
|
|
1823
|
-
}
|
|
1824
|
-
}
|
|
1825
|
-
const parsedType = this._getType(input);
|
|
1826
|
-
if (parsedType !== ZodParsedType.bigint) {
|
|
1827
|
-
return this._getInvalidInput(input);
|
|
1828
|
-
}
|
|
1829
|
-
let ctx = void 0;
|
|
1830
|
-
const status = new ParseStatus();
|
|
1831
|
-
for (const check of this._def.checks) {
|
|
1832
|
-
if (check.kind === "min") {
|
|
1833
|
-
const tooSmall = check.inclusive ? input.data < check.value : input.data <= check.value;
|
|
1834
|
-
if (tooSmall) {
|
|
1835
|
-
ctx = this._getOrReturnCtx(input, ctx);
|
|
1836
|
-
addIssueToContext(ctx, {
|
|
1837
|
-
code: ZodIssueCode.too_small,
|
|
1838
|
-
type: "bigint",
|
|
1839
|
-
minimum: check.value,
|
|
1840
|
-
inclusive: check.inclusive,
|
|
1841
|
-
message: check.message
|
|
1842
|
-
});
|
|
1843
|
-
status.dirty();
|
|
1844
|
-
}
|
|
1845
|
-
} else if (check.kind === "max") {
|
|
1846
|
-
const tooBig = check.inclusive ? input.data > check.value : input.data >= check.value;
|
|
1847
|
-
if (tooBig) {
|
|
1848
|
-
ctx = this._getOrReturnCtx(input, ctx);
|
|
1849
|
-
addIssueToContext(ctx, {
|
|
1850
|
-
code: ZodIssueCode.too_big,
|
|
1851
|
-
type: "bigint",
|
|
1852
|
-
maximum: check.value,
|
|
1853
|
-
inclusive: check.inclusive,
|
|
1854
|
-
message: check.message
|
|
1855
|
-
});
|
|
1856
|
-
status.dirty();
|
|
1857
|
-
}
|
|
1858
|
-
} else if (check.kind === "multipleOf") {
|
|
1859
|
-
if (input.data % check.value !== BigInt(0)) {
|
|
1860
|
-
ctx = this._getOrReturnCtx(input, ctx);
|
|
1861
|
-
addIssueToContext(ctx, {
|
|
1862
|
-
code: ZodIssueCode.not_multiple_of,
|
|
1863
|
-
multipleOf: check.value,
|
|
1864
|
-
message: check.message
|
|
1865
|
-
});
|
|
1866
|
-
status.dirty();
|
|
1867
|
-
}
|
|
1868
|
-
} else {
|
|
1869
|
-
util.assertNever(check);
|
|
1870
|
-
}
|
|
1871
|
-
}
|
|
1872
|
-
return { status: status.value, value: input.data };
|
|
1873
|
-
}
|
|
1874
|
-
_getInvalidInput(input) {
|
|
1875
|
-
const ctx = this._getOrReturnCtx(input);
|
|
1876
|
-
addIssueToContext(ctx, {
|
|
1877
|
-
code: ZodIssueCode.invalid_type,
|
|
1878
|
-
expected: ZodParsedType.bigint,
|
|
1879
|
-
received: ctx.parsedType
|
|
1880
|
-
});
|
|
1881
|
-
return INVALID;
|
|
1882
|
-
}
|
|
1883
|
-
gte(value, message) {
|
|
1884
|
-
return this.setLimit("min", value, true, errorUtil.toString(message));
|
|
1885
|
-
}
|
|
1886
|
-
gt(value, message) {
|
|
1887
|
-
return this.setLimit("min", value, false, errorUtil.toString(message));
|
|
1888
|
-
}
|
|
1889
|
-
lte(value, message) {
|
|
1890
|
-
return this.setLimit("max", value, true, errorUtil.toString(message));
|
|
1891
|
-
}
|
|
1892
|
-
lt(value, message) {
|
|
1893
|
-
return this.setLimit("max", value, false, errorUtil.toString(message));
|
|
1894
|
-
}
|
|
1895
|
-
setLimit(kind, value, inclusive, message) {
|
|
1896
|
-
return new _ZodBigInt({
|
|
1897
|
-
...this._def,
|
|
1898
|
-
checks: [
|
|
1899
|
-
...this._def.checks,
|
|
1900
|
-
{
|
|
1901
|
-
kind,
|
|
1902
|
-
value,
|
|
1903
|
-
inclusive,
|
|
1904
|
-
message: errorUtil.toString(message)
|
|
1905
|
-
}
|
|
1906
|
-
]
|
|
1907
|
-
});
|
|
1908
|
-
}
|
|
1909
|
-
_addCheck(check) {
|
|
1910
|
-
return new _ZodBigInt({
|
|
1911
|
-
...this._def,
|
|
1912
|
-
checks: [...this._def.checks, check]
|
|
1913
|
-
});
|
|
1914
|
-
}
|
|
1915
|
-
positive(message) {
|
|
1916
|
-
return this._addCheck({
|
|
1917
|
-
kind: "min",
|
|
1918
|
-
value: BigInt(0),
|
|
1919
|
-
inclusive: false,
|
|
1920
|
-
message: errorUtil.toString(message)
|
|
1921
|
-
});
|
|
1922
|
-
}
|
|
1923
|
-
negative(message) {
|
|
1924
|
-
return this._addCheck({
|
|
1925
|
-
kind: "max",
|
|
1926
|
-
value: BigInt(0),
|
|
1927
|
-
inclusive: false,
|
|
1928
|
-
message: errorUtil.toString(message)
|
|
1929
|
-
});
|
|
1930
|
-
}
|
|
1931
|
-
nonpositive(message) {
|
|
1932
|
-
return this._addCheck({
|
|
1933
|
-
kind: "max",
|
|
1934
|
-
value: BigInt(0),
|
|
1935
|
-
inclusive: true,
|
|
1936
|
-
message: errorUtil.toString(message)
|
|
1937
|
-
});
|
|
1938
|
-
}
|
|
1939
|
-
nonnegative(message) {
|
|
1940
|
-
return this._addCheck({
|
|
1941
|
-
kind: "min",
|
|
1942
|
-
value: BigInt(0),
|
|
1943
|
-
inclusive: true,
|
|
1944
|
-
message: errorUtil.toString(message)
|
|
1945
|
-
});
|
|
1946
|
-
}
|
|
1947
|
-
multipleOf(value, message) {
|
|
1948
|
-
return this._addCheck({
|
|
1949
|
-
kind: "multipleOf",
|
|
1950
|
-
value,
|
|
1951
|
-
message: errorUtil.toString(message)
|
|
1952
|
-
});
|
|
1953
|
-
}
|
|
1954
|
-
get minValue() {
|
|
1955
|
-
let min = null;
|
|
1956
|
-
for (const ch of this._def.checks) {
|
|
1957
|
-
if (ch.kind === "min") {
|
|
1958
|
-
if (min === null || ch.value > min)
|
|
1959
|
-
min = ch.value;
|
|
1960
|
-
}
|
|
1961
|
-
}
|
|
1962
|
-
return min;
|
|
1963
|
-
}
|
|
1964
|
-
get maxValue() {
|
|
1965
|
-
let max = null;
|
|
1966
|
-
for (const ch of this._def.checks) {
|
|
1967
|
-
if (ch.kind === "max") {
|
|
1968
|
-
if (max === null || ch.value < max)
|
|
1969
|
-
max = ch.value;
|
|
1970
|
-
}
|
|
1971
|
-
}
|
|
1972
|
-
return max;
|
|
1973
|
-
}
|
|
1974
|
-
};
|
|
1975
|
-
ZodBigInt.create = (params) => {
|
|
1976
|
-
return new ZodBigInt({
|
|
1977
|
-
checks: [],
|
|
1978
|
-
typeName: ZodFirstPartyTypeKind.ZodBigInt,
|
|
1979
|
-
coerce: params?.coerce ?? false,
|
|
1980
|
-
...processCreateParams(params)
|
|
1981
|
-
});
|
|
1982
|
-
};
|
|
1983
|
-
var ZodBoolean = class extends ZodType {
|
|
1984
|
-
_parse(input) {
|
|
1985
|
-
if (this._def.coerce) {
|
|
1986
|
-
input.data = Boolean(input.data);
|
|
1987
|
-
}
|
|
1988
|
-
const parsedType = this._getType(input);
|
|
1989
|
-
if (parsedType !== ZodParsedType.boolean) {
|
|
1990
|
-
const ctx = this._getOrReturnCtx(input);
|
|
1991
|
-
addIssueToContext(ctx, {
|
|
1992
|
-
code: ZodIssueCode.invalid_type,
|
|
1993
|
-
expected: ZodParsedType.boolean,
|
|
1994
|
-
received: ctx.parsedType
|
|
1995
|
-
});
|
|
1996
|
-
return INVALID;
|
|
1997
|
-
}
|
|
1998
|
-
return OK(input.data);
|
|
1999
|
-
}
|
|
2000
|
-
};
|
|
2001
|
-
ZodBoolean.create = (params) => {
|
|
2002
|
-
return new ZodBoolean({
|
|
2003
|
-
typeName: ZodFirstPartyTypeKind.ZodBoolean,
|
|
2004
|
-
coerce: params?.coerce || false,
|
|
2005
|
-
...processCreateParams(params)
|
|
2006
|
-
});
|
|
2007
|
-
};
|
|
2008
|
-
var ZodDate = class _ZodDate extends ZodType {
|
|
2009
|
-
_parse(input) {
|
|
2010
|
-
if (this._def.coerce) {
|
|
2011
|
-
input.data = new Date(input.data);
|
|
2012
|
-
}
|
|
2013
|
-
const parsedType = this._getType(input);
|
|
2014
|
-
if (parsedType !== ZodParsedType.date) {
|
|
2015
|
-
const ctx2 = this._getOrReturnCtx(input);
|
|
2016
|
-
addIssueToContext(ctx2, {
|
|
2017
|
-
code: ZodIssueCode.invalid_type,
|
|
2018
|
-
expected: ZodParsedType.date,
|
|
2019
|
-
received: ctx2.parsedType
|
|
2020
|
-
});
|
|
2021
|
-
return INVALID;
|
|
2022
|
-
}
|
|
2023
|
-
if (Number.isNaN(input.data.getTime())) {
|
|
2024
|
-
const ctx2 = this._getOrReturnCtx(input);
|
|
2025
|
-
addIssueToContext(ctx2, {
|
|
2026
|
-
code: ZodIssueCode.invalid_date
|
|
2027
|
-
});
|
|
2028
|
-
return INVALID;
|
|
2029
|
-
}
|
|
2030
|
-
const status = new ParseStatus();
|
|
2031
|
-
let ctx = void 0;
|
|
2032
|
-
for (const check of this._def.checks) {
|
|
2033
|
-
if (check.kind === "min") {
|
|
2034
|
-
if (input.data.getTime() < check.value) {
|
|
2035
|
-
ctx = this._getOrReturnCtx(input, ctx);
|
|
2036
|
-
addIssueToContext(ctx, {
|
|
2037
|
-
code: ZodIssueCode.too_small,
|
|
2038
|
-
message: check.message,
|
|
2039
|
-
inclusive: true,
|
|
2040
|
-
exact: false,
|
|
2041
|
-
minimum: check.value,
|
|
2042
|
-
type: "date"
|
|
2043
|
-
});
|
|
2044
|
-
status.dirty();
|
|
2045
|
-
}
|
|
2046
|
-
} else if (check.kind === "max") {
|
|
2047
|
-
if (input.data.getTime() > check.value) {
|
|
2048
|
-
ctx = this._getOrReturnCtx(input, ctx);
|
|
2049
|
-
addIssueToContext(ctx, {
|
|
2050
|
-
code: ZodIssueCode.too_big,
|
|
2051
|
-
message: check.message,
|
|
2052
|
-
inclusive: true,
|
|
2053
|
-
exact: false,
|
|
2054
|
-
maximum: check.value,
|
|
2055
|
-
type: "date"
|
|
2056
|
-
});
|
|
2057
|
-
status.dirty();
|
|
2058
|
-
}
|
|
2059
|
-
} else {
|
|
2060
|
-
util.assertNever(check);
|
|
2061
|
-
}
|
|
2062
|
-
}
|
|
2063
|
-
return {
|
|
2064
|
-
status: status.value,
|
|
2065
|
-
value: new Date(input.data.getTime())
|
|
2066
|
-
};
|
|
2067
|
-
}
|
|
2068
|
-
_addCheck(check) {
|
|
2069
|
-
return new _ZodDate({
|
|
2070
|
-
...this._def,
|
|
2071
|
-
checks: [...this._def.checks, check]
|
|
2072
|
-
});
|
|
2073
|
-
}
|
|
2074
|
-
min(minDate, message) {
|
|
2075
|
-
return this._addCheck({
|
|
2076
|
-
kind: "min",
|
|
2077
|
-
value: minDate.getTime(),
|
|
2078
|
-
message: errorUtil.toString(message)
|
|
2079
|
-
});
|
|
2080
|
-
}
|
|
2081
|
-
max(maxDate, message) {
|
|
2082
|
-
return this._addCheck({
|
|
2083
|
-
kind: "max",
|
|
2084
|
-
value: maxDate.getTime(),
|
|
2085
|
-
message: errorUtil.toString(message)
|
|
2086
|
-
});
|
|
2087
|
-
}
|
|
2088
|
-
get minDate() {
|
|
2089
|
-
let min = null;
|
|
2090
|
-
for (const ch of this._def.checks) {
|
|
2091
|
-
if (ch.kind === "min") {
|
|
2092
|
-
if (min === null || ch.value > min)
|
|
2093
|
-
min = ch.value;
|
|
2094
|
-
}
|
|
2095
|
-
}
|
|
2096
|
-
return min != null ? new Date(min) : null;
|
|
2097
|
-
}
|
|
2098
|
-
get maxDate() {
|
|
2099
|
-
let max = null;
|
|
2100
|
-
for (const ch of this._def.checks) {
|
|
2101
|
-
if (ch.kind === "max") {
|
|
2102
|
-
if (max === null || ch.value < max)
|
|
2103
|
-
max = ch.value;
|
|
2104
|
-
}
|
|
2105
|
-
}
|
|
2106
|
-
return max != null ? new Date(max) : null;
|
|
2107
|
-
}
|
|
2108
|
-
};
|
|
2109
|
-
ZodDate.create = (params) => {
|
|
2110
|
-
return new ZodDate({
|
|
2111
|
-
checks: [],
|
|
2112
|
-
coerce: params?.coerce || false,
|
|
2113
|
-
typeName: ZodFirstPartyTypeKind.ZodDate,
|
|
2114
|
-
...processCreateParams(params)
|
|
2115
|
-
});
|
|
2116
|
-
};
|
|
2117
|
-
var ZodSymbol = class extends ZodType {
|
|
2118
|
-
_parse(input) {
|
|
2119
|
-
const parsedType = this._getType(input);
|
|
2120
|
-
if (parsedType !== ZodParsedType.symbol) {
|
|
2121
|
-
const ctx = this._getOrReturnCtx(input);
|
|
2122
|
-
addIssueToContext(ctx, {
|
|
2123
|
-
code: ZodIssueCode.invalid_type,
|
|
2124
|
-
expected: ZodParsedType.symbol,
|
|
2125
|
-
received: ctx.parsedType
|
|
2126
|
-
});
|
|
2127
|
-
return INVALID;
|
|
2128
|
-
}
|
|
2129
|
-
return OK(input.data);
|
|
2130
|
-
}
|
|
2131
|
-
};
|
|
2132
|
-
ZodSymbol.create = (params) => {
|
|
2133
|
-
return new ZodSymbol({
|
|
2134
|
-
typeName: ZodFirstPartyTypeKind.ZodSymbol,
|
|
2135
|
-
...processCreateParams(params)
|
|
2136
|
-
});
|
|
2137
|
-
};
|
|
2138
|
-
var ZodUndefined = class extends ZodType {
|
|
2139
|
-
_parse(input) {
|
|
2140
|
-
const parsedType = this._getType(input);
|
|
2141
|
-
if (parsedType !== ZodParsedType.undefined) {
|
|
2142
|
-
const ctx = this._getOrReturnCtx(input);
|
|
2143
|
-
addIssueToContext(ctx, {
|
|
2144
|
-
code: ZodIssueCode.invalid_type,
|
|
2145
|
-
expected: ZodParsedType.undefined,
|
|
2146
|
-
received: ctx.parsedType
|
|
2147
|
-
});
|
|
2148
|
-
return INVALID;
|
|
2149
|
-
}
|
|
2150
|
-
return OK(input.data);
|
|
2151
|
-
}
|
|
2152
|
-
};
|
|
2153
|
-
ZodUndefined.create = (params) => {
|
|
2154
|
-
return new ZodUndefined({
|
|
2155
|
-
typeName: ZodFirstPartyTypeKind.ZodUndefined,
|
|
2156
|
-
...processCreateParams(params)
|
|
2157
|
-
});
|
|
2158
|
-
};
|
|
2159
|
-
var ZodNull = class extends ZodType {
|
|
2160
|
-
_parse(input) {
|
|
2161
|
-
const parsedType = this._getType(input);
|
|
2162
|
-
if (parsedType !== ZodParsedType.null) {
|
|
2163
|
-
const ctx = this._getOrReturnCtx(input);
|
|
2164
|
-
addIssueToContext(ctx, {
|
|
2165
|
-
code: ZodIssueCode.invalid_type,
|
|
2166
|
-
expected: ZodParsedType.null,
|
|
2167
|
-
received: ctx.parsedType
|
|
2168
|
-
});
|
|
2169
|
-
return INVALID;
|
|
2170
|
-
}
|
|
2171
|
-
return OK(input.data);
|
|
2172
|
-
}
|
|
2173
|
-
};
|
|
2174
|
-
ZodNull.create = (params) => {
|
|
2175
|
-
return new ZodNull({
|
|
2176
|
-
typeName: ZodFirstPartyTypeKind.ZodNull,
|
|
2177
|
-
...processCreateParams(params)
|
|
2178
|
-
});
|
|
2179
|
-
};
|
|
2180
|
-
var ZodAny = class extends ZodType {
|
|
2181
|
-
constructor() {
|
|
2182
|
-
super(...arguments);
|
|
2183
|
-
this._any = true;
|
|
2184
|
-
}
|
|
2185
|
-
_parse(input) {
|
|
2186
|
-
return OK(input.data);
|
|
2187
|
-
}
|
|
2188
|
-
};
|
|
2189
|
-
ZodAny.create = (params) => {
|
|
2190
|
-
return new ZodAny({
|
|
2191
|
-
typeName: ZodFirstPartyTypeKind.ZodAny,
|
|
2192
|
-
...processCreateParams(params)
|
|
2193
|
-
});
|
|
2194
|
-
};
|
|
2195
|
-
var ZodUnknown = class extends ZodType {
|
|
2196
|
-
constructor() {
|
|
2197
|
-
super(...arguments);
|
|
2198
|
-
this._unknown = true;
|
|
2199
|
-
}
|
|
2200
|
-
_parse(input) {
|
|
2201
|
-
return OK(input.data);
|
|
2202
|
-
}
|
|
2203
|
-
};
|
|
2204
|
-
ZodUnknown.create = (params) => {
|
|
2205
|
-
return new ZodUnknown({
|
|
2206
|
-
typeName: ZodFirstPartyTypeKind.ZodUnknown,
|
|
2207
|
-
...processCreateParams(params)
|
|
2208
|
-
});
|
|
2209
|
-
};
|
|
2210
|
-
var ZodNever = class extends ZodType {
|
|
2211
|
-
_parse(input) {
|
|
2212
|
-
const ctx = this._getOrReturnCtx(input);
|
|
2213
|
-
addIssueToContext(ctx, {
|
|
2214
|
-
code: ZodIssueCode.invalid_type,
|
|
2215
|
-
expected: ZodParsedType.never,
|
|
2216
|
-
received: ctx.parsedType
|
|
2217
|
-
});
|
|
2218
|
-
return INVALID;
|
|
2219
|
-
}
|
|
2220
|
-
};
|
|
2221
|
-
ZodNever.create = (params) => {
|
|
2222
|
-
return new ZodNever({
|
|
2223
|
-
typeName: ZodFirstPartyTypeKind.ZodNever,
|
|
2224
|
-
...processCreateParams(params)
|
|
2225
|
-
});
|
|
2226
|
-
};
|
|
2227
|
-
var ZodVoid = class extends ZodType {
|
|
2228
|
-
_parse(input) {
|
|
2229
|
-
const parsedType = this._getType(input);
|
|
2230
|
-
if (parsedType !== ZodParsedType.undefined) {
|
|
2231
|
-
const ctx = this._getOrReturnCtx(input);
|
|
2232
|
-
addIssueToContext(ctx, {
|
|
2233
|
-
code: ZodIssueCode.invalid_type,
|
|
2234
|
-
expected: ZodParsedType.void,
|
|
2235
|
-
received: ctx.parsedType
|
|
2236
|
-
});
|
|
2237
|
-
return INVALID;
|
|
2238
|
-
}
|
|
2239
|
-
return OK(input.data);
|
|
2240
|
-
}
|
|
2241
|
-
};
|
|
2242
|
-
ZodVoid.create = (params) => {
|
|
2243
|
-
return new ZodVoid({
|
|
2244
|
-
typeName: ZodFirstPartyTypeKind.ZodVoid,
|
|
2245
|
-
...processCreateParams(params)
|
|
2246
|
-
});
|
|
2247
|
-
};
|
|
2248
|
-
var ZodArray = class _ZodArray extends ZodType {
|
|
2249
|
-
_parse(input) {
|
|
2250
|
-
const { ctx, status } = this._processInputParams(input);
|
|
2251
|
-
const def = this._def;
|
|
2252
|
-
if (ctx.parsedType !== ZodParsedType.array) {
|
|
2253
|
-
addIssueToContext(ctx, {
|
|
2254
|
-
code: ZodIssueCode.invalid_type,
|
|
2255
|
-
expected: ZodParsedType.array,
|
|
2256
|
-
received: ctx.parsedType
|
|
2257
|
-
});
|
|
2258
|
-
return INVALID;
|
|
2259
|
-
}
|
|
2260
|
-
if (def.exactLength !== null) {
|
|
2261
|
-
const tooBig = ctx.data.length > def.exactLength.value;
|
|
2262
|
-
const tooSmall = ctx.data.length < def.exactLength.value;
|
|
2263
|
-
if (tooBig || tooSmall) {
|
|
2264
|
-
addIssueToContext(ctx, {
|
|
2265
|
-
code: tooBig ? ZodIssueCode.too_big : ZodIssueCode.too_small,
|
|
2266
|
-
minimum: tooSmall ? def.exactLength.value : void 0,
|
|
2267
|
-
maximum: tooBig ? def.exactLength.value : void 0,
|
|
2268
|
-
type: "array",
|
|
2269
|
-
inclusive: true,
|
|
2270
|
-
exact: true,
|
|
2271
|
-
message: def.exactLength.message
|
|
2272
|
-
});
|
|
2273
|
-
status.dirty();
|
|
2274
|
-
}
|
|
2275
|
-
}
|
|
2276
|
-
if (def.minLength !== null) {
|
|
2277
|
-
if (ctx.data.length < def.minLength.value) {
|
|
2278
|
-
addIssueToContext(ctx, {
|
|
2279
|
-
code: ZodIssueCode.too_small,
|
|
2280
|
-
minimum: def.minLength.value,
|
|
2281
|
-
type: "array",
|
|
2282
|
-
inclusive: true,
|
|
2283
|
-
exact: false,
|
|
2284
|
-
message: def.minLength.message
|
|
2285
|
-
});
|
|
2286
|
-
status.dirty();
|
|
2287
|
-
}
|
|
2288
|
-
}
|
|
2289
|
-
if (def.maxLength !== null) {
|
|
2290
|
-
if (ctx.data.length > def.maxLength.value) {
|
|
2291
|
-
addIssueToContext(ctx, {
|
|
2292
|
-
code: ZodIssueCode.too_big,
|
|
2293
|
-
maximum: def.maxLength.value,
|
|
2294
|
-
type: "array",
|
|
2295
|
-
inclusive: true,
|
|
2296
|
-
exact: false,
|
|
2297
|
-
message: def.maxLength.message
|
|
2298
|
-
});
|
|
2299
|
-
status.dirty();
|
|
2300
|
-
}
|
|
2301
|
-
}
|
|
2302
|
-
if (ctx.common.async) {
|
|
2303
|
-
return Promise.all([...ctx.data].map((item, i) => {
|
|
2304
|
-
return def.type._parseAsync(new ParseInputLazyPath(ctx, item, ctx.path, i));
|
|
2305
|
-
})).then((result2) => {
|
|
2306
|
-
return ParseStatus.mergeArray(status, result2);
|
|
2307
|
-
});
|
|
2308
|
-
}
|
|
2309
|
-
const result = [...ctx.data].map((item, i) => {
|
|
2310
|
-
return def.type._parseSync(new ParseInputLazyPath(ctx, item, ctx.path, i));
|
|
2311
|
-
});
|
|
2312
|
-
return ParseStatus.mergeArray(status, result);
|
|
2313
|
-
}
|
|
2314
|
-
get element() {
|
|
2315
|
-
return this._def.type;
|
|
2316
|
-
}
|
|
2317
|
-
min(minLength, message) {
|
|
2318
|
-
return new _ZodArray({
|
|
2319
|
-
...this._def,
|
|
2320
|
-
minLength: { value: minLength, message: errorUtil.toString(message) }
|
|
2321
|
-
});
|
|
2322
|
-
}
|
|
2323
|
-
max(maxLength, message) {
|
|
2324
|
-
return new _ZodArray({
|
|
2325
|
-
...this._def,
|
|
2326
|
-
maxLength: { value: maxLength, message: errorUtil.toString(message) }
|
|
2327
|
-
});
|
|
2328
|
-
}
|
|
2329
|
-
length(len, message) {
|
|
2330
|
-
return new _ZodArray({
|
|
2331
|
-
...this._def,
|
|
2332
|
-
exactLength: { value: len, message: errorUtil.toString(message) }
|
|
2333
|
-
});
|
|
2334
|
-
}
|
|
2335
|
-
nonempty(message) {
|
|
2336
|
-
return this.min(1, message);
|
|
2337
|
-
}
|
|
2338
|
-
};
|
|
2339
|
-
ZodArray.create = (schema, params) => {
|
|
2340
|
-
return new ZodArray({
|
|
2341
|
-
type: schema,
|
|
2342
|
-
minLength: null,
|
|
2343
|
-
maxLength: null,
|
|
2344
|
-
exactLength: null,
|
|
2345
|
-
typeName: ZodFirstPartyTypeKind.ZodArray,
|
|
2346
|
-
...processCreateParams(params)
|
|
2347
|
-
});
|
|
2348
|
-
};
|
|
2349
|
-
function deepPartialify(schema) {
|
|
2350
|
-
if (schema instanceof ZodObject) {
|
|
2351
|
-
const newShape = {};
|
|
2352
|
-
for (const key in schema.shape) {
|
|
2353
|
-
const fieldSchema = schema.shape[key];
|
|
2354
|
-
newShape[key] = ZodOptional.create(deepPartialify(fieldSchema));
|
|
2355
|
-
}
|
|
2356
|
-
return new ZodObject({
|
|
2357
|
-
...schema._def,
|
|
2358
|
-
shape: () => newShape
|
|
2359
|
-
});
|
|
2360
|
-
} else if (schema instanceof ZodArray) {
|
|
2361
|
-
return new ZodArray({
|
|
2362
|
-
...schema._def,
|
|
2363
|
-
type: deepPartialify(schema.element)
|
|
2364
|
-
});
|
|
2365
|
-
} else if (schema instanceof ZodOptional) {
|
|
2366
|
-
return ZodOptional.create(deepPartialify(schema.unwrap()));
|
|
2367
|
-
} else if (schema instanceof ZodNullable) {
|
|
2368
|
-
return ZodNullable.create(deepPartialify(schema.unwrap()));
|
|
2369
|
-
} else if (schema instanceof ZodTuple) {
|
|
2370
|
-
return ZodTuple.create(schema.items.map((item) => deepPartialify(item)));
|
|
2371
|
-
} else {
|
|
2372
|
-
return schema;
|
|
2373
|
-
}
|
|
2374
|
-
}
|
|
2375
|
-
var ZodObject = class _ZodObject extends ZodType {
|
|
2376
|
-
constructor() {
|
|
2377
|
-
super(...arguments);
|
|
2378
|
-
this._cached = null;
|
|
2379
|
-
this.nonstrict = this.passthrough;
|
|
2380
|
-
this.augment = this.extend;
|
|
2381
|
-
}
|
|
2382
|
-
_getCached() {
|
|
2383
|
-
if (this._cached !== null)
|
|
2384
|
-
return this._cached;
|
|
2385
|
-
const shape = this._def.shape();
|
|
2386
|
-
const keys = util.objectKeys(shape);
|
|
2387
|
-
this._cached = { shape, keys };
|
|
2388
|
-
return this._cached;
|
|
2389
|
-
}
|
|
2390
|
-
_parse(input) {
|
|
2391
|
-
const parsedType = this._getType(input);
|
|
2392
|
-
if (parsedType !== ZodParsedType.object) {
|
|
2393
|
-
const ctx2 = this._getOrReturnCtx(input);
|
|
2394
|
-
addIssueToContext(ctx2, {
|
|
2395
|
-
code: ZodIssueCode.invalid_type,
|
|
2396
|
-
expected: ZodParsedType.object,
|
|
2397
|
-
received: ctx2.parsedType
|
|
2398
|
-
});
|
|
2399
|
-
return INVALID;
|
|
2400
|
-
}
|
|
2401
|
-
const { status, ctx } = this._processInputParams(input);
|
|
2402
|
-
const { shape, keys: shapeKeys } = this._getCached();
|
|
2403
|
-
const extraKeys = [];
|
|
2404
|
-
if (!(this._def.catchall instanceof ZodNever && this._def.unknownKeys === "strip")) {
|
|
2405
|
-
for (const key in ctx.data) {
|
|
2406
|
-
if (!shapeKeys.includes(key)) {
|
|
2407
|
-
extraKeys.push(key);
|
|
2408
|
-
}
|
|
2409
|
-
}
|
|
2410
|
-
}
|
|
2411
|
-
const pairs = [];
|
|
2412
|
-
for (const key of shapeKeys) {
|
|
2413
|
-
const keyValidator = shape[key];
|
|
2414
|
-
const value = ctx.data[key];
|
|
2415
|
-
pairs.push({
|
|
2416
|
-
key: { status: "valid", value: key },
|
|
2417
|
-
value: keyValidator._parse(new ParseInputLazyPath(ctx, value, ctx.path, key)),
|
|
2418
|
-
alwaysSet: key in ctx.data
|
|
2419
|
-
});
|
|
2420
|
-
}
|
|
2421
|
-
if (this._def.catchall instanceof ZodNever) {
|
|
2422
|
-
const unknownKeys = this._def.unknownKeys;
|
|
2423
|
-
if (unknownKeys === "passthrough") {
|
|
2424
|
-
for (const key of extraKeys) {
|
|
2425
|
-
pairs.push({
|
|
2426
|
-
key: { status: "valid", value: key },
|
|
2427
|
-
value: { status: "valid", value: ctx.data[key] }
|
|
2428
|
-
});
|
|
2429
|
-
}
|
|
2430
|
-
} else if (unknownKeys === "strict") {
|
|
2431
|
-
if (extraKeys.length > 0) {
|
|
2432
|
-
addIssueToContext(ctx, {
|
|
2433
|
-
code: ZodIssueCode.unrecognized_keys,
|
|
2434
|
-
keys: extraKeys
|
|
2435
|
-
});
|
|
2436
|
-
status.dirty();
|
|
2437
|
-
}
|
|
2438
|
-
} else if (unknownKeys === "strip") {
|
|
2439
|
-
} else {
|
|
2440
|
-
throw new Error(`Internal ZodObject error: invalid unknownKeys value.`);
|
|
2441
|
-
}
|
|
2442
|
-
} else {
|
|
2443
|
-
const catchall = this._def.catchall;
|
|
2444
|
-
for (const key of extraKeys) {
|
|
2445
|
-
const value = ctx.data[key];
|
|
2446
|
-
pairs.push({
|
|
2447
|
-
key: { status: "valid", value: key },
|
|
2448
|
-
value: catchall._parse(
|
|
2449
|
-
new ParseInputLazyPath(ctx, value, ctx.path, key)
|
|
2450
|
-
//, ctx.child(key), value, getParsedType(value)
|
|
2451
|
-
),
|
|
2452
|
-
alwaysSet: key in ctx.data
|
|
2453
|
-
});
|
|
2454
|
-
}
|
|
2455
|
-
}
|
|
2456
|
-
if (ctx.common.async) {
|
|
2457
|
-
return Promise.resolve().then(async () => {
|
|
2458
|
-
const syncPairs = [];
|
|
2459
|
-
for (const pair of pairs) {
|
|
2460
|
-
const key = await pair.key;
|
|
2461
|
-
const value = await pair.value;
|
|
2462
|
-
syncPairs.push({
|
|
2463
|
-
key,
|
|
2464
|
-
value,
|
|
2465
|
-
alwaysSet: pair.alwaysSet
|
|
2466
|
-
});
|
|
2467
|
-
}
|
|
2468
|
-
return syncPairs;
|
|
2469
|
-
}).then((syncPairs) => {
|
|
2470
|
-
return ParseStatus.mergeObjectSync(status, syncPairs);
|
|
2471
|
-
});
|
|
2472
|
-
} else {
|
|
2473
|
-
return ParseStatus.mergeObjectSync(status, pairs);
|
|
2474
|
-
}
|
|
2475
|
-
}
|
|
2476
|
-
get shape() {
|
|
2477
|
-
return this._def.shape();
|
|
2478
|
-
}
|
|
2479
|
-
strict(message) {
|
|
2480
|
-
errorUtil.errToObj;
|
|
2481
|
-
return new _ZodObject({
|
|
2482
|
-
...this._def,
|
|
2483
|
-
unknownKeys: "strict",
|
|
2484
|
-
...message !== void 0 ? {
|
|
2485
|
-
errorMap: (issue, ctx) => {
|
|
2486
|
-
const defaultError = this._def.errorMap?.(issue, ctx).message ?? ctx.defaultError;
|
|
2487
|
-
if (issue.code === "unrecognized_keys")
|
|
2488
|
-
return {
|
|
2489
|
-
message: errorUtil.errToObj(message).message ?? defaultError
|
|
2490
|
-
};
|
|
2491
|
-
return {
|
|
2492
|
-
message: defaultError
|
|
2493
|
-
};
|
|
2494
|
-
}
|
|
2495
|
-
} : {}
|
|
2496
|
-
});
|
|
2497
|
-
}
|
|
2498
|
-
strip() {
|
|
2499
|
-
return new _ZodObject({
|
|
2500
|
-
...this._def,
|
|
2501
|
-
unknownKeys: "strip"
|
|
2502
|
-
});
|
|
2503
|
-
}
|
|
2504
|
-
passthrough() {
|
|
2505
|
-
return new _ZodObject({
|
|
2506
|
-
...this._def,
|
|
2507
|
-
unknownKeys: "passthrough"
|
|
2508
|
-
});
|
|
2509
|
-
}
|
|
2510
|
-
// const AugmentFactory =
|
|
2511
|
-
// <Def extends ZodObjectDef>(def: Def) =>
|
|
2512
|
-
// <Augmentation extends ZodRawShape>(
|
|
2513
|
-
// augmentation: Augmentation
|
|
2514
|
-
// ): ZodObject<
|
|
2515
|
-
// extendShape<ReturnType<Def["shape"]>, Augmentation>,
|
|
2516
|
-
// Def["unknownKeys"],
|
|
2517
|
-
// Def["catchall"]
|
|
2518
|
-
// > => {
|
|
2519
|
-
// return new ZodObject({
|
|
2520
|
-
// ...def,
|
|
2521
|
-
// shape: () => ({
|
|
2522
|
-
// ...def.shape(),
|
|
2523
|
-
// ...augmentation,
|
|
2524
|
-
// }),
|
|
2525
|
-
// }) as any;
|
|
2526
|
-
// };
|
|
2527
|
-
extend(augmentation) {
|
|
2528
|
-
return new _ZodObject({
|
|
2529
|
-
...this._def,
|
|
2530
|
-
shape: () => ({
|
|
2531
|
-
...this._def.shape(),
|
|
2532
|
-
...augmentation
|
|
2533
|
-
})
|
|
2534
|
-
});
|
|
2535
|
-
}
|
|
2536
|
-
/**
|
|
2537
|
-
* Prior to zod@1.0.12 there was a bug in the
|
|
2538
|
-
* inferred type of merged objects. Please
|
|
2539
|
-
* upgrade if you are experiencing issues.
|
|
2540
|
-
*/
|
|
2541
|
-
merge(merging) {
|
|
2542
|
-
const merged = new _ZodObject({
|
|
2543
|
-
unknownKeys: merging._def.unknownKeys,
|
|
2544
|
-
catchall: merging._def.catchall,
|
|
2545
|
-
shape: () => ({
|
|
2546
|
-
...this._def.shape(),
|
|
2547
|
-
...merging._def.shape()
|
|
2548
|
-
}),
|
|
2549
|
-
typeName: ZodFirstPartyTypeKind.ZodObject
|
|
2550
|
-
});
|
|
2551
|
-
return merged;
|
|
2552
|
-
}
|
|
2553
|
-
// merge<
|
|
2554
|
-
// Incoming extends AnyZodObject,
|
|
2555
|
-
// Augmentation extends Incoming["shape"],
|
|
2556
|
-
// NewOutput extends {
|
|
2557
|
-
// [k in keyof Augmentation | keyof Output]: k extends keyof Augmentation
|
|
2558
|
-
// ? Augmentation[k]["_output"]
|
|
2559
|
-
// : k extends keyof Output
|
|
2560
|
-
// ? Output[k]
|
|
2561
|
-
// : never;
|
|
2562
|
-
// },
|
|
2563
|
-
// NewInput extends {
|
|
2564
|
-
// [k in keyof Augmentation | keyof Input]: k extends keyof Augmentation
|
|
2565
|
-
// ? Augmentation[k]["_input"]
|
|
2566
|
-
// : k extends keyof Input
|
|
2567
|
-
// ? Input[k]
|
|
2568
|
-
// : never;
|
|
2569
|
-
// }
|
|
2570
|
-
// >(
|
|
2571
|
-
// merging: Incoming
|
|
2572
|
-
// ): ZodObject<
|
|
2573
|
-
// extendShape<T, ReturnType<Incoming["_def"]["shape"]>>,
|
|
2574
|
-
// Incoming["_def"]["unknownKeys"],
|
|
2575
|
-
// Incoming["_def"]["catchall"],
|
|
2576
|
-
// NewOutput,
|
|
2577
|
-
// NewInput
|
|
2578
|
-
// > {
|
|
2579
|
-
// const merged: any = new ZodObject({
|
|
2580
|
-
// unknownKeys: merging._def.unknownKeys,
|
|
2581
|
-
// catchall: merging._def.catchall,
|
|
2582
|
-
// shape: () =>
|
|
2583
|
-
// objectUtil.mergeShapes(this._def.shape(), merging._def.shape()),
|
|
2584
|
-
// typeName: ZodFirstPartyTypeKind.ZodObject,
|
|
2585
|
-
// }) as any;
|
|
2586
|
-
// return merged;
|
|
2587
|
-
// }
|
|
2588
|
-
setKey(key, schema) {
|
|
2589
|
-
return this.augment({ [key]: schema });
|
|
2590
|
-
}
|
|
2591
|
-
// merge<Incoming extends AnyZodObject>(
|
|
2592
|
-
// merging: Incoming
|
|
2593
|
-
// ): //ZodObject<T & Incoming["_shape"], UnknownKeys, Catchall> = (merging) => {
|
|
2594
|
-
// ZodObject<
|
|
2595
|
-
// extendShape<T, ReturnType<Incoming["_def"]["shape"]>>,
|
|
2596
|
-
// Incoming["_def"]["unknownKeys"],
|
|
2597
|
-
// Incoming["_def"]["catchall"]
|
|
2598
|
-
// > {
|
|
2599
|
-
// // const mergedShape = objectUtil.mergeShapes(
|
|
2600
|
-
// // this._def.shape(),
|
|
2601
|
-
// // merging._def.shape()
|
|
2602
|
-
// // );
|
|
2603
|
-
// const merged: any = new ZodObject({
|
|
2604
|
-
// unknownKeys: merging._def.unknownKeys,
|
|
2605
|
-
// catchall: merging._def.catchall,
|
|
2606
|
-
// shape: () =>
|
|
2607
|
-
// objectUtil.mergeShapes(this._def.shape(), merging._def.shape()),
|
|
2608
|
-
// typeName: ZodFirstPartyTypeKind.ZodObject,
|
|
2609
|
-
// }) as any;
|
|
2610
|
-
// return merged;
|
|
2611
|
-
// }
|
|
2612
|
-
catchall(index) {
|
|
2613
|
-
return new _ZodObject({
|
|
2614
|
-
...this._def,
|
|
2615
|
-
catchall: index
|
|
2616
|
-
});
|
|
2617
|
-
}
|
|
2618
|
-
pick(mask) {
|
|
2619
|
-
const shape = {};
|
|
2620
|
-
for (const key of util.objectKeys(mask)) {
|
|
2621
|
-
if (mask[key] && this.shape[key]) {
|
|
2622
|
-
shape[key] = this.shape[key];
|
|
2623
|
-
}
|
|
2624
|
-
}
|
|
2625
|
-
return new _ZodObject({
|
|
2626
|
-
...this._def,
|
|
2627
|
-
shape: () => shape
|
|
2628
|
-
});
|
|
2629
|
-
}
|
|
2630
|
-
omit(mask) {
|
|
2631
|
-
const shape = {};
|
|
2632
|
-
for (const key of util.objectKeys(this.shape)) {
|
|
2633
|
-
if (!mask[key]) {
|
|
2634
|
-
shape[key] = this.shape[key];
|
|
2635
|
-
}
|
|
2636
|
-
}
|
|
2637
|
-
return new _ZodObject({
|
|
2638
|
-
...this._def,
|
|
2639
|
-
shape: () => shape
|
|
2640
|
-
});
|
|
2641
|
-
}
|
|
2642
|
-
/**
|
|
2643
|
-
* @deprecated
|
|
2644
|
-
*/
|
|
2645
|
-
deepPartial() {
|
|
2646
|
-
return deepPartialify(this);
|
|
2647
|
-
}
|
|
2648
|
-
partial(mask) {
|
|
2649
|
-
const newShape = {};
|
|
2650
|
-
for (const key of util.objectKeys(this.shape)) {
|
|
2651
|
-
const fieldSchema = this.shape[key];
|
|
2652
|
-
if (mask && !mask[key]) {
|
|
2653
|
-
newShape[key] = fieldSchema;
|
|
2654
|
-
} else {
|
|
2655
|
-
newShape[key] = fieldSchema.optional();
|
|
2656
|
-
}
|
|
2657
|
-
}
|
|
2658
|
-
return new _ZodObject({
|
|
2659
|
-
...this._def,
|
|
2660
|
-
shape: () => newShape
|
|
2661
|
-
});
|
|
2662
|
-
}
|
|
2663
|
-
required(mask) {
|
|
2664
|
-
const newShape = {};
|
|
2665
|
-
for (const key of util.objectKeys(this.shape)) {
|
|
2666
|
-
if (mask && !mask[key]) {
|
|
2667
|
-
newShape[key] = this.shape[key];
|
|
2668
|
-
} else {
|
|
2669
|
-
const fieldSchema = this.shape[key];
|
|
2670
|
-
let newField = fieldSchema;
|
|
2671
|
-
while (newField instanceof ZodOptional) {
|
|
2672
|
-
newField = newField._def.innerType;
|
|
2673
|
-
}
|
|
2674
|
-
newShape[key] = newField;
|
|
2675
|
-
}
|
|
2676
|
-
}
|
|
2677
|
-
return new _ZodObject({
|
|
2678
|
-
...this._def,
|
|
2679
|
-
shape: () => newShape
|
|
2680
|
-
});
|
|
2681
|
-
}
|
|
2682
|
-
keyof() {
|
|
2683
|
-
return createZodEnum(util.objectKeys(this.shape));
|
|
2684
|
-
}
|
|
2685
|
-
};
|
|
2686
|
-
ZodObject.create = (shape, params) => {
|
|
2687
|
-
return new ZodObject({
|
|
2688
|
-
shape: () => shape,
|
|
2689
|
-
unknownKeys: "strip",
|
|
2690
|
-
catchall: ZodNever.create(),
|
|
2691
|
-
typeName: ZodFirstPartyTypeKind.ZodObject,
|
|
2692
|
-
...processCreateParams(params)
|
|
2693
|
-
});
|
|
2694
|
-
};
|
|
2695
|
-
ZodObject.strictCreate = (shape, params) => {
|
|
2696
|
-
return new ZodObject({
|
|
2697
|
-
shape: () => shape,
|
|
2698
|
-
unknownKeys: "strict",
|
|
2699
|
-
catchall: ZodNever.create(),
|
|
2700
|
-
typeName: ZodFirstPartyTypeKind.ZodObject,
|
|
2701
|
-
...processCreateParams(params)
|
|
2702
|
-
});
|
|
2703
|
-
};
|
|
2704
|
-
ZodObject.lazycreate = (shape, params) => {
|
|
2705
|
-
return new ZodObject({
|
|
2706
|
-
shape,
|
|
2707
|
-
unknownKeys: "strip",
|
|
2708
|
-
catchall: ZodNever.create(),
|
|
2709
|
-
typeName: ZodFirstPartyTypeKind.ZodObject,
|
|
2710
|
-
...processCreateParams(params)
|
|
2711
|
-
});
|
|
2712
|
-
};
|
|
2713
|
-
var ZodUnion = class extends ZodType {
|
|
2714
|
-
_parse(input) {
|
|
2715
|
-
const { ctx } = this._processInputParams(input);
|
|
2716
|
-
const options = this._def.options;
|
|
2717
|
-
function handleResults(results) {
|
|
2718
|
-
for (const result of results) {
|
|
2719
|
-
if (result.result.status === "valid") {
|
|
2720
|
-
return result.result;
|
|
2721
|
-
}
|
|
2722
|
-
}
|
|
2723
|
-
for (const result of results) {
|
|
2724
|
-
if (result.result.status === "dirty") {
|
|
2725
|
-
ctx.common.issues.push(...result.ctx.common.issues);
|
|
2726
|
-
return result.result;
|
|
2727
|
-
}
|
|
2728
|
-
}
|
|
2729
|
-
const unionErrors = results.map((result) => new ZodError(result.ctx.common.issues));
|
|
2730
|
-
addIssueToContext(ctx, {
|
|
2731
|
-
code: ZodIssueCode.invalid_union,
|
|
2732
|
-
unionErrors
|
|
2733
|
-
});
|
|
2734
|
-
return INVALID;
|
|
2735
|
-
}
|
|
2736
|
-
if (ctx.common.async) {
|
|
2737
|
-
return Promise.all(options.map(async (option) => {
|
|
2738
|
-
const childCtx = {
|
|
2739
|
-
...ctx,
|
|
2740
|
-
common: {
|
|
2741
|
-
...ctx.common,
|
|
2742
|
-
issues: []
|
|
2743
|
-
},
|
|
2744
|
-
parent: null
|
|
2745
|
-
};
|
|
2746
|
-
return {
|
|
2747
|
-
result: await option._parseAsync({
|
|
2748
|
-
data: ctx.data,
|
|
2749
|
-
path: ctx.path,
|
|
2750
|
-
parent: childCtx
|
|
2751
|
-
}),
|
|
2752
|
-
ctx: childCtx
|
|
2753
|
-
};
|
|
2754
|
-
})).then(handleResults);
|
|
2755
|
-
} else {
|
|
2756
|
-
let dirty = void 0;
|
|
2757
|
-
const issues = [];
|
|
2758
|
-
for (const option of options) {
|
|
2759
|
-
const childCtx = {
|
|
2760
|
-
...ctx,
|
|
2761
|
-
common: {
|
|
2762
|
-
...ctx.common,
|
|
2763
|
-
issues: []
|
|
2764
|
-
},
|
|
2765
|
-
parent: null
|
|
2766
|
-
};
|
|
2767
|
-
const result = option._parseSync({
|
|
2768
|
-
data: ctx.data,
|
|
2769
|
-
path: ctx.path,
|
|
2770
|
-
parent: childCtx
|
|
2771
|
-
});
|
|
2772
|
-
if (result.status === "valid") {
|
|
2773
|
-
return result;
|
|
2774
|
-
} else if (result.status === "dirty" && !dirty) {
|
|
2775
|
-
dirty = { result, ctx: childCtx };
|
|
2776
|
-
}
|
|
2777
|
-
if (childCtx.common.issues.length) {
|
|
2778
|
-
issues.push(childCtx.common.issues);
|
|
2779
|
-
}
|
|
2780
|
-
}
|
|
2781
|
-
if (dirty) {
|
|
2782
|
-
ctx.common.issues.push(...dirty.ctx.common.issues);
|
|
2783
|
-
return dirty.result;
|
|
2784
|
-
}
|
|
2785
|
-
const unionErrors = issues.map((issues2) => new ZodError(issues2));
|
|
2786
|
-
addIssueToContext(ctx, {
|
|
2787
|
-
code: ZodIssueCode.invalid_union,
|
|
2788
|
-
unionErrors
|
|
2789
|
-
});
|
|
2790
|
-
return INVALID;
|
|
2791
|
-
}
|
|
2792
|
-
}
|
|
2793
|
-
get options() {
|
|
2794
|
-
return this._def.options;
|
|
2795
|
-
}
|
|
2796
|
-
};
|
|
2797
|
-
ZodUnion.create = (types, params) => {
|
|
2798
|
-
return new ZodUnion({
|
|
2799
|
-
options: types,
|
|
2800
|
-
typeName: ZodFirstPartyTypeKind.ZodUnion,
|
|
2801
|
-
...processCreateParams(params)
|
|
2802
|
-
});
|
|
2803
|
-
};
|
|
2804
|
-
var getDiscriminator = (type) => {
|
|
2805
|
-
if (type instanceof ZodLazy) {
|
|
2806
|
-
return getDiscriminator(type.schema);
|
|
2807
|
-
} else if (type instanceof ZodEffects) {
|
|
2808
|
-
return getDiscriminator(type.innerType());
|
|
2809
|
-
} else if (type instanceof ZodLiteral) {
|
|
2810
|
-
return [type.value];
|
|
2811
|
-
} else if (type instanceof ZodEnum) {
|
|
2812
|
-
return type.options;
|
|
2813
|
-
} else if (type instanceof ZodNativeEnum) {
|
|
2814
|
-
return util.objectValues(type.enum);
|
|
2815
|
-
} else if (type instanceof ZodDefault) {
|
|
2816
|
-
return getDiscriminator(type._def.innerType);
|
|
2817
|
-
} else if (type instanceof ZodUndefined) {
|
|
2818
|
-
return [void 0];
|
|
2819
|
-
} else if (type instanceof ZodNull) {
|
|
2820
|
-
return [null];
|
|
2821
|
-
} else if (type instanceof ZodOptional) {
|
|
2822
|
-
return [void 0, ...getDiscriminator(type.unwrap())];
|
|
2823
|
-
} else if (type instanceof ZodNullable) {
|
|
2824
|
-
return [null, ...getDiscriminator(type.unwrap())];
|
|
2825
|
-
} else if (type instanceof ZodBranded) {
|
|
2826
|
-
return getDiscriminator(type.unwrap());
|
|
2827
|
-
} else if (type instanceof ZodReadonly) {
|
|
2828
|
-
return getDiscriminator(type.unwrap());
|
|
2829
|
-
} else if (type instanceof ZodCatch) {
|
|
2830
|
-
return getDiscriminator(type._def.innerType);
|
|
2831
|
-
} else {
|
|
2832
|
-
return [];
|
|
2833
|
-
}
|
|
2834
|
-
};
|
|
2835
|
-
var ZodDiscriminatedUnion = class _ZodDiscriminatedUnion extends ZodType {
|
|
2836
|
-
_parse(input) {
|
|
2837
|
-
const { ctx } = this._processInputParams(input);
|
|
2838
|
-
if (ctx.parsedType !== ZodParsedType.object) {
|
|
2839
|
-
addIssueToContext(ctx, {
|
|
2840
|
-
code: ZodIssueCode.invalid_type,
|
|
2841
|
-
expected: ZodParsedType.object,
|
|
2842
|
-
received: ctx.parsedType
|
|
2843
|
-
});
|
|
2844
|
-
return INVALID;
|
|
2845
|
-
}
|
|
2846
|
-
const discriminator = this.discriminator;
|
|
2847
|
-
const discriminatorValue = ctx.data[discriminator];
|
|
2848
|
-
const option = this.optionsMap.get(discriminatorValue);
|
|
2849
|
-
if (!option) {
|
|
2850
|
-
addIssueToContext(ctx, {
|
|
2851
|
-
code: ZodIssueCode.invalid_union_discriminator,
|
|
2852
|
-
options: Array.from(this.optionsMap.keys()),
|
|
2853
|
-
path: [discriminator]
|
|
2854
|
-
});
|
|
2855
|
-
return INVALID;
|
|
2856
|
-
}
|
|
2857
|
-
if (ctx.common.async) {
|
|
2858
|
-
return option._parseAsync({
|
|
2859
|
-
data: ctx.data,
|
|
2860
|
-
path: ctx.path,
|
|
2861
|
-
parent: ctx
|
|
2862
|
-
});
|
|
2863
|
-
} else {
|
|
2864
|
-
return option._parseSync({
|
|
2865
|
-
data: ctx.data,
|
|
2866
|
-
path: ctx.path,
|
|
2867
|
-
parent: ctx
|
|
2868
|
-
});
|
|
2869
|
-
}
|
|
2870
|
-
}
|
|
2871
|
-
get discriminator() {
|
|
2872
|
-
return this._def.discriminator;
|
|
2873
|
-
}
|
|
2874
|
-
get options() {
|
|
2875
|
-
return this._def.options;
|
|
2876
|
-
}
|
|
2877
|
-
get optionsMap() {
|
|
2878
|
-
return this._def.optionsMap;
|
|
2879
|
-
}
|
|
2880
|
-
/**
|
|
2881
|
-
* The constructor of the discriminated union schema. Its behaviour is very similar to that of the normal z.union() constructor.
|
|
2882
|
-
* However, it only allows a union of objects, all of which need to share a discriminator property. This property must
|
|
2883
|
-
* have a different value for each object in the union.
|
|
2884
|
-
* @param discriminator the name of the discriminator property
|
|
2885
|
-
* @param types an array of object schemas
|
|
2886
|
-
* @param params
|
|
2887
|
-
*/
|
|
2888
|
-
static create(discriminator, options, params) {
|
|
2889
|
-
const optionsMap = /* @__PURE__ */ new Map();
|
|
2890
|
-
for (const type of options) {
|
|
2891
|
-
const discriminatorValues = getDiscriminator(type.shape[discriminator]);
|
|
2892
|
-
if (!discriminatorValues.length) {
|
|
2893
|
-
throw new Error(`A discriminator value for key \`${discriminator}\` could not be extracted from all schema options`);
|
|
2894
|
-
}
|
|
2895
|
-
for (const value of discriminatorValues) {
|
|
2896
|
-
if (optionsMap.has(value)) {
|
|
2897
|
-
throw new Error(`Discriminator property ${String(discriminator)} has duplicate value ${String(value)}`);
|
|
2898
|
-
}
|
|
2899
|
-
optionsMap.set(value, type);
|
|
2900
|
-
}
|
|
2901
|
-
}
|
|
2902
|
-
return new _ZodDiscriminatedUnion({
|
|
2903
|
-
typeName: ZodFirstPartyTypeKind.ZodDiscriminatedUnion,
|
|
2904
|
-
discriminator,
|
|
2905
|
-
options,
|
|
2906
|
-
optionsMap,
|
|
2907
|
-
...processCreateParams(params)
|
|
2908
|
-
});
|
|
2909
|
-
}
|
|
2910
|
-
};
|
|
2911
|
-
function mergeValues(a, b) {
|
|
2912
|
-
const aType = getParsedType(a);
|
|
2913
|
-
const bType = getParsedType(b);
|
|
2914
|
-
if (a === b) {
|
|
2915
|
-
return { valid: true, data: a };
|
|
2916
|
-
} else if (aType === ZodParsedType.object && bType === ZodParsedType.object) {
|
|
2917
|
-
const bKeys = util.objectKeys(b);
|
|
2918
|
-
const sharedKeys = util.objectKeys(a).filter((key) => bKeys.indexOf(key) !== -1);
|
|
2919
|
-
const newObj = { ...a, ...b };
|
|
2920
|
-
for (const key of sharedKeys) {
|
|
2921
|
-
const sharedValue = mergeValues(a[key], b[key]);
|
|
2922
|
-
if (!sharedValue.valid) {
|
|
2923
|
-
return { valid: false };
|
|
2924
|
-
}
|
|
2925
|
-
newObj[key] = sharedValue.data;
|
|
2926
|
-
}
|
|
2927
|
-
return { valid: true, data: newObj };
|
|
2928
|
-
} else if (aType === ZodParsedType.array && bType === ZodParsedType.array) {
|
|
2929
|
-
if (a.length !== b.length) {
|
|
2930
|
-
return { valid: false };
|
|
2931
|
-
}
|
|
2932
|
-
const newArray = [];
|
|
2933
|
-
for (let index = 0; index < a.length; index++) {
|
|
2934
|
-
const itemA = a[index];
|
|
2935
|
-
const itemB = b[index];
|
|
2936
|
-
const sharedValue = mergeValues(itemA, itemB);
|
|
2937
|
-
if (!sharedValue.valid) {
|
|
2938
|
-
return { valid: false };
|
|
2939
|
-
}
|
|
2940
|
-
newArray.push(sharedValue.data);
|
|
2941
|
-
}
|
|
2942
|
-
return { valid: true, data: newArray };
|
|
2943
|
-
} else if (aType === ZodParsedType.date && bType === ZodParsedType.date && +a === +b) {
|
|
2944
|
-
return { valid: true, data: a };
|
|
2945
|
-
} else {
|
|
2946
|
-
return { valid: false };
|
|
2947
|
-
}
|
|
2948
|
-
}
|
|
2949
|
-
var ZodIntersection = class extends ZodType {
|
|
2950
|
-
_parse(input) {
|
|
2951
|
-
const { status, ctx } = this._processInputParams(input);
|
|
2952
|
-
const handleParsed = (parsedLeft, parsedRight) => {
|
|
2953
|
-
if (isAborted(parsedLeft) || isAborted(parsedRight)) {
|
|
2954
|
-
return INVALID;
|
|
2955
|
-
}
|
|
2956
|
-
const merged = mergeValues(parsedLeft.value, parsedRight.value);
|
|
2957
|
-
if (!merged.valid) {
|
|
2958
|
-
addIssueToContext(ctx, {
|
|
2959
|
-
code: ZodIssueCode.invalid_intersection_types
|
|
2960
|
-
});
|
|
2961
|
-
return INVALID;
|
|
2962
|
-
}
|
|
2963
|
-
if (isDirty(parsedLeft) || isDirty(parsedRight)) {
|
|
2964
|
-
status.dirty();
|
|
2965
|
-
}
|
|
2966
|
-
return { status: status.value, value: merged.data };
|
|
2967
|
-
};
|
|
2968
|
-
if (ctx.common.async) {
|
|
2969
|
-
return Promise.all([
|
|
2970
|
-
this._def.left._parseAsync({
|
|
2971
|
-
data: ctx.data,
|
|
2972
|
-
path: ctx.path,
|
|
2973
|
-
parent: ctx
|
|
2974
|
-
}),
|
|
2975
|
-
this._def.right._parseAsync({
|
|
2976
|
-
data: ctx.data,
|
|
2977
|
-
path: ctx.path,
|
|
2978
|
-
parent: ctx
|
|
2979
|
-
})
|
|
2980
|
-
]).then(([left, right]) => handleParsed(left, right));
|
|
2981
|
-
} else {
|
|
2982
|
-
return handleParsed(this._def.left._parseSync({
|
|
2983
|
-
data: ctx.data,
|
|
2984
|
-
path: ctx.path,
|
|
2985
|
-
parent: ctx
|
|
2986
|
-
}), this._def.right._parseSync({
|
|
2987
|
-
data: ctx.data,
|
|
2988
|
-
path: ctx.path,
|
|
2989
|
-
parent: ctx
|
|
2990
|
-
}));
|
|
2991
|
-
}
|
|
2992
|
-
}
|
|
2993
|
-
};
|
|
2994
|
-
ZodIntersection.create = (left, right, params) => {
|
|
2995
|
-
return new ZodIntersection({
|
|
2996
|
-
left,
|
|
2997
|
-
right,
|
|
2998
|
-
typeName: ZodFirstPartyTypeKind.ZodIntersection,
|
|
2999
|
-
...processCreateParams(params)
|
|
3000
|
-
});
|
|
3001
|
-
};
|
|
3002
|
-
var ZodTuple = class _ZodTuple extends ZodType {
|
|
3003
|
-
_parse(input) {
|
|
3004
|
-
const { status, ctx } = this._processInputParams(input);
|
|
3005
|
-
if (ctx.parsedType !== ZodParsedType.array) {
|
|
3006
|
-
addIssueToContext(ctx, {
|
|
3007
|
-
code: ZodIssueCode.invalid_type,
|
|
3008
|
-
expected: ZodParsedType.array,
|
|
3009
|
-
received: ctx.parsedType
|
|
3010
|
-
});
|
|
3011
|
-
return INVALID;
|
|
3012
|
-
}
|
|
3013
|
-
if (ctx.data.length < this._def.items.length) {
|
|
3014
|
-
addIssueToContext(ctx, {
|
|
3015
|
-
code: ZodIssueCode.too_small,
|
|
3016
|
-
minimum: this._def.items.length,
|
|
3017
|
-
inclusive: true,
|
|
3018
|
-
exact: false,
|
|
3019
|
-
type: "array"
|
|
3020
|
-
});
|
|
3021
|
-
return INVALID;
|
|
3022
|
-
}
|
|
3023
|
-
const rest = this._def.rest;
|
|
3024
|
-
if (!rest && ctx.data.length > this._def.items.length) {
|
|
3025
|
-
addIssueToContext(ctx, {
|
|
3026
|
-
code: ZodIssueCode.too_big,
|
|
3027
|
-
maximum: this._def.items.length,
|
|
3028
|
-
inclusive: true,
|
|
3029
|
-
exact: false,
|
|
3030
|
-
type: "array"
|
|
3031
|
-
});
|
|
3032
|
-
status.dirty();
|
|
3033
|
-
}
|
|
3034
|
-
const items = [...ctx.data].map((item, itemIndex) => {
|
|
3035
|
-
const schema = this._def.items[itemIndex] || this._def.rest;
|
|
3036
|
-
if (!schema)
|
|
3037
|
-
return null;
|
|
3038
|
-
return schema._parse(new ParseInputLazyPath(ctx, item, ctx.path, itemIndex));
|
|
3039
|
-
}).filter((x) => !!x);
|
|
3040
|
-
if (ctx.common.async) {
|
|
3041
|
-
return Promise.all(items).then((results) => {
|
|
3042
|
-
return ParseStatus.mergeArray(status, results);
|
|
3043
|
-
});
|
|
3044
|
-
} else {
|
|
3045
|
-
return ParseStatus.mergeArray(status, items);
|
|
3046
|
-
}
|
|
3047
|
-
}
|
|
3048
|
-
get items() {
|
|
3049
|
-
return this._def.items;
|
|
3050
|
-
}
|
|
3051
|
-
rest(rest) {
|
|
3052
|
-
return new _ZodTuple({
|
|
3053
|
-
...this._def,
|
|
3054
|
-
rest
|
|
3055
|
-
});
|
|
3056
|
-
}
|
|
3057
|
-
};
|
|
3058
|
-
ZodTuple.create = (schemas, params) => {
|
|
3059
|
-
if (!Array.isArray(schemas)) {
|
|
3060
|
-
throw new Error("You must pass an array of schemas to z.tuple([ ... ])");
|
|
3061
|
-
}
|
|
3062
|
-
return new ZodTuple({
|
|
3063
|
-
items: schemas,
|
|
3064
|
-
typeName: ZodFirstPartyTypeKind.ZodTuple,
|
|
3065
|
-
rest: null,
|
|
3066
|
-
...processCreateParams(params)
|
|
3067
|
-
});
|
|
3068
|
-
};
|
|
3069
|
-
var ZodRecord = class _ZodRecord extends ZodType {
|
|
3070
|
-
get keySchema() {
|
|
3071
|
-
return this._def.keyType;
|
|
3072
|
-
}
|
|
3073
|
-
get valueSchema() {
|
|
3074
|
-
return this._def.valueType;
|
|
3075
|
-
}
|
|
3076
|
-
_parse(input) {
|
|
3077
|
-
const { status, ctx } = this._processInputParams(input);
|
|
3078
|
-
if (ctx.parsedType !== ZodParsedType.object) {
|
|
3079
|
-
addIssueToContext(ctx, {
|
|
3080
|
-
code: ZodIssueCode.invalid_type,
|
|
3081
|
-
expected: ZodParsedType.object,
|
|
3082
|
-
received: ctx.parsedType
|
|
3083
|
-
});
|
|
3084
|
-
return INVALID;
|
|
3085
|
-
}
|
|
3086
|
-
const pairs = [];
|
|
3087
|
-
const keyType = this._def.keyType;
|
|
3088
|
-
const valueType = this._def.valueType;
|
|
3089
|
-
for (const key in ctx.data) {
|
|
3090
|
-
pairs.push({
|
|
3091
|
-
key: keyType._parse(new ParseInputLazyPath(ctx, key, ctx.path, key)),
|
|
3092
|
-
value: valueType._parse(new ParseInputLazyPath(ctx, ctx.data[key], ctx.path, key)),
|
|
3093
|
-
alwaysSet: key in ctx.data
|
|
3094
|
-
});
|
|
3095
|
-
}
|
|
3096
|
-
if (ctx.common.async) {
|
|
3097
|
-
return ParseStatus.mergeObjectAsync(status, pairs);
|
|
3098
|
-
} else {
|
|
3099
|
-
return ParseStatus.mergeObjectSync(status, pairs);
|
|
3100
|
-
}
|
|
3101
|
-
}
|
|
3102
|
-
get element() {
|
|
3103
|
-
return this._def.valueType;
|
|
3104
|
-
}
|
|
3105
|
-
static create(first, second, third) {
|
|
3106
|
-
if (second instanceof ZodType) {
|
|
3107
|
-
return new _ZodRecord({
|
|
3108
|
-
keyType: first,
|
|
3109
|
-
valueType: second,
|
|
3110
|
-
typeName: ZodFirstPartyTypeKind.ZodRecord,
|
|
3111
|
-
...processCreateParams(third)
|
|
3112
|
-
});
|
|
3113
|
-
}
|
|
3114
|
-
return new _ZodRecord({
|
|
3115
|
-
keyType: ZodString.create(),
|
|
3116
|
-
valueType: first,
|
|
3117
|
-
typeName: ZodFirstPartyTypeKind.ZodRecord,
|
|
3118
|
-
...processCreateParams(second)
|
|
3119
|
-
});
|
|
3120
|
-
}
|
|
3121
|
-
};
|
|
3122
|
-
var ZodMap = class extends ZodType {
|
|
3123
|
-
get keySchema() {
|
|
3124
|
-
return this._def.keyType;
|
|
3125
|
-
}
|
|
3126
|
-
get valueSchema() {
|
|
3127
|
-
return this._def.valueType;
|
|
3128
|
-
}
|
|
3129
|
-
_parse(input) {
|
|
3130
|
-
const { status, ctx } = this._processInputParams(input);
|
|
3131
|
-
if (ctx.parsedType !== ZodParsedType.map) {
|
|
3132
|
-
addIssueToContext(ctx, {
|
|
3133
|
-
code: ZodIssueCode.invalid_type,
|
|
3134
|
-
expected: ZodParsedType.map,
|
|
3135
|
-
received: ctx.parsedType
|
|
3136
|
-
});
|
|
3137
|
-
return INVALID;
|
|
3138
|
-
}
|
|
3139
|
-
const keyType = this._def.keyType;
|
|
3140
|
-
const valueType = this._def.valueType;
|
|
3141
|
-
const pairs = [...ctx.data.entries()].map(([key, value], index) => {
|
|
3142
|
-
return {
|
|
3143
|
-
key: keyType._parse(new ParseInputLazyPath(ctx, key, ctx.path, [index, "key"])),
|
|
3144
|
-
value: valueType._parse(new ParseInputLazyPath(ctx, value, ctx.path, [index, "value"]))
|
|
3145
|
-
};
|
|
3146
|
-
});
|
|
3147
|
-
if (ctx.common.async) {
|
|
3148
|
-
const finalMap = /* @__PURE__ */ new Map();
|
|
3149
|
-
return Promise.resolve().then(async () => {
|
|
3150
|
-
for (const pair of pairs) {
|
|
3151
|
-
const key = await pair.key;
|
|
3152
|
-
const value = await pair.value;
|
|
3153
|
-
if (key.status === "aborted" || value.status === "aborted") {
|
|
3154
|
-
return INVALID;
|
|
3155
|
-
}
|
|
3156
|
-
if (key.status === "dirty" || value.status === "dirty") {
|
|
3157
|
-
status.dirty();
|
|
3158
|
-
}
|
|
3159
|
-
finalMap.set(key.value, value.value);
|
|
3160
|
-
}
|
|
3161
|
-
return { status: status.value, value: finalMap };
|
|
3162
|
-
});
|
|
3163
|
-
} else {
|
|
3164
|
-
const finalMap = /* @__PURE__ */ new Map();
|
|
3165
|
-
for (const pair of pairs) {
|
|
3166
|
-
const key = pair.key;
|
|
3167
|
-
const value = pair.value;
|
|
3168
|
-
if (key.status === "aborted" || value.status === "aborted") {
|
|
3169
|
-
return INVALID;
|
|
3170
|
-
}
|
|
3171
|
-
if (key.status === "dirty" || value.status === "dirty") {
|
|
3172
|
-
status.dirty();
|
|
3173
|
-
}
|
|
3174
|
-
finalMap.set(key.value, value.value);
|
|
3175
|
-
}
|
|
3176
|
-
return { status: status.value, value: finalMap };
|
|
3177
|
-
}
|
|
3178
|
-
}
|
|
3179
|
-
};
|
|
3180
|
-
ZodMap.create = (keyType, valueType, params) => {
|
|
3181
|
-
return new ZodMap({
|
|
3182
|
-
valueType,
|
|
3183
|
-
keyType,
|
|
3184
|
-
typeName: ZodFirstPartyTypeKind.ZodMap,
|
|
3185
|
-
...processCreateParams(params)
|
|
3186
|
-
});
|
|
3187
|
-
};
|
|
3188
|
-
var ZodSet = class _ZodSet extends ZodType {
|
|
3189
|
-
_parse(input) {
|
|
3190
|
-
const { status, ctx } = this._processInputParams(input);
|
|
3191
|
-
if (ctx.parsedType !== ZodParsedType.set) {
|
|
3192
|
-
addIssueToContext(ctx, {
|
|
3193
|
-
code: ZodIssueCode.invalid_type,
|
|
3194
|
-
expected: ZodParsedType.set,
|
|
3195
|
-
received: ctx.parsedType
|
|
3196
|
-
});
|
|
3197
|
-
return INVALID;
|
|
3198
|
-
}
|
|
3199
|
-
const def = this._def;
|
|
3200
|
-
if (def.minSize !== null) {
|
|
3201
|
-
if (ctx.data.size < def.minSize.value) {
|
|
3202
|
-
addIssueToContext(ctx, {
|
|
3203
|
-
code: ZodIssueCode.too_small,
|
|
3204
|
-
minimum: def.minSize.value,
|
|
3205
|
-
type: "set",
|
|
3206
|
-
inclusive: true,
|
|
3207
|
-
exact: false,
|
|
3208
|
-
message: def.minSize.message
|
|
3209
|
-
});
|
|
3210
|
-
status.dirty();
|
|
3211
|
-
}
|
|
3212
|
-
}
|
|
3213
|
-
if (def.maxSize !== null) {
|
|
3214
|
-
if (ctx.data.size > def.maxSize.value) {
|
|
3215
|
-
addIssueToContext(ctx, {
|
|
3216
|
-
code: ZodIssueCode.too_big,
|
|
3217
|
-
maximum: def.maxSize.value,
|
|
3218
|
-
type: "set",
|
|
3219
|
-
inclusive: true,
|
|
3220
|
-
exact: false,
|
|
3221
|
-
message: def.maxSize.message
|
|
3222
|
-
});
|
|
3223
|
-
status.dirty();
|
|
3224
|
-
}
|
|
3225
|
-
}
|
|
3226
|
-
const valueType = this._def.valueType;
|
|
3227
|
-
function finalizeSet(elements2) {
|
|
3228
|
-
const parsedSet = /* @__PURE__ */ new Set();
|
|
3229
|
-
for (const element of elements2) {
|
|
3230
|
-
if (element.status === "aborted")
|
|
3231
|
-
return INVALID;
|
|
3232
|
-
if (element.status === "dirty")
|
|
3233
|
-
status.dirty();
|
|
3234
|
-
parsedSet.add(element.value);
|
|
3235
|
-
}
|
|
3236
|
-
return { status: status.value, value: parsedSet };
|
|
3237
|
-
}
|
|
3238
|
-
const elements = [...ctx.data.values()].map((item, i) => valueType._parse(new ParseInputLazyPath(ctx, item, ctx.path, i)));
|
|
3239
|
-
if (ctx.common.async) {
|
|
3240
|
-
return Promise.all(elements).then((elements2) => finalizeSet(elements2));
|
|
3241
|
-
} else {
|
|
3242
|
-
return finalizeSet(elements);
|
|
3243
|
-
}
|
|
3244
|
-
}
|
|
3245
|
-
min(minSize, message) {
|
|
3246
|
-
return new _ZodSet({
|
|
3247
|
-
...this._def,
|
|
3248
|
-
minSize: { value: minSize, message: errorUtil.toString(message) }
|
|
3249
|
-
});
|
|
3250
|
-
}
|
|
3251
|
-
max(maxSize, message) {
|
|
3252
|
-
return new _ZodSet({
|
|
3253
|
-
...this._def,
|
|
3254
|
-
maxSize: { value: maxSize, message: errorUtil.toString(message) }
|
|
3255
|
-
});
|
|
3256
|
-
}
|
|
3257
|
-
size(size, message) {
|
|
3258
|
-
return this.min(size, message).max(size, message);
|
|
3259
|
-
}
|
|
3260
|
-
nonempty(message) {
|
|
3261
|
-
return this.min(1, message);
|
|
3262
|
-
}
|
|
3263
|
-
};
|
|
3264
|
-
ZodSet.create = (valueType, params) => {
|
|
3265
|
-
return new ZodSet({
|
|
3266
|
-
valueType,
|
|
3267
|
-
minSize: null,
|
|
3268
|
-
maxSize: null,
|
|
3269
|
-
typeName: ZodFirstPartyTypeKind.ZodSet,
|
|
3270
|
-
...processCreateParams(params)
|
|
3271
|
-
});
|
|
3272
|
-
};
|
|
3273
|
-
var ZodFunction = class _ZodFunction extends ZodType {
|
|
3274
|
-
constructor() {
|
|
3275
|
-
super(...arguments);
|
|
3276
|
-
this.validate = this.implement;
|
|
3277
|
-
}
|
|
3278
|
-
_parse(input) {
|
|
3279
|
-
const { ctx } = this._processInputParams(input);
|
|
3280
|
-
if (ctx.parsedType !== ZodParsedType.function) {
|
|
3281
|
-
addIssueToContext(ctx, {
|
|
3282
|
-
code: ZodIssueCode.invalid_type,
|
|
3283
|
-
expected: ZodParsedType.function,
|
|
3284
|
-
received: ctx.parsedType
|
|
3285
|
-
});
|
|
3286
|
-
return INVALID;
|
|
3287
|
-
}
|
|
3288
|
-
function makeArgsIssue(args, error) {
|
|
3289
|
-
return makeIssue({
|
|
3290
|
-
data: args,
|
|
3291
|
-
path: ctx.path,
|
|
3292
|
-
errorMaps: [ctx.common.contextualErrorMap, ctx.schemaErrorMap, getErrorMap(), en_default].filter((x) => !!x),
|
|
3293
|
-
issueData: {
|
|
3294
|
-
code: ZodIssueCode.invalid_arguments,
|
|
3295
|
-
argumentsError: error
|
|
3296
|
-
}
|
|
3297
|
-
});
|
|
3298
|
-
}
|
|
3299
|
-
function makeReturnsIssue(returns, error) {
|
|
3300
|
-
return makeIssue({
|
|
3301
|
-
data: returns,
|
|
3302
|
-
path: ctx.path,
|
|
3303
|
-
errorMaps: [ctx.common.contextualErrorMap, ctx.schemaErrorMap, getErrorMap(), en_default].filter((x) => !!x),
|
|
3304
|
-
issueData: {
|
|
3305
|
-
code: ZodIssueCode.invalid_return_type,
|
|
3306
|
-
returnTypeError: error
|
|
3307
|
-
}
|
|
3308
|
-
});
|
|
3309
|
-
}
|
|
3310
|
-
const params = { errorMap: ctx.common.contextualErrorMap };
|
|
3311
|
-
const fn = ctx.data;
|
|
3312
|
-
if (this._def.returns instanceof ZodPromise) {
|
|
3313
|
-
const me = this;
|
|
3314
|
-
return OK(async function(...args) {
|
|
3315
|
-
const error = new ZodError([]);
|
|
3316
|
-
const parsedArgs = await me._def.args.parseAsync(args, params).catch((e) => {
|
|
3317
|
-
error.addIssue(makeArgsIssue(args, e));
|
|
3318
|
-
throw error;
|
|
3319
|
-
});
|
|
3320
|
-
const result = await Reflect.apply(fn, this, parsedArgs);
|
|
3321
|
-
const parsedReturns = await me._def.returns._def.type.parseAsync(result, params).catch((e) => {
|
|
3322
|
-
error.addIssue(makeReturnsIssue(result, e));
|
|
3323
|
-
throw error;
|
|
3324
|
-
});
|
|
3325
|
-
return parsedReturns;
|
|
3326
|
-
});
|
|
3327
|
-
} else {
|
|
3328
|
-
const me = this;
|
|
3329
|
-
return OK(function(...args) {
|
|
3330
|
-
const parsedArgs = me._def.args.safeParse(args, params);
|
|
3331
|
-
if (!parsedArgs.success) {
|
|
3332
|
-
throw new ZodError([makeArgsIssue(args, parsedArgs.error)]);
|
|
3333
|
-
}
|
|
3334
|
-
const result = Reflect.apply(fn, this, parsedArgs.data);
|
|
3335
|
-
const parsedReturns = me._def.returns.safeParse(result, params);
|
|
3336
|
-
if (!parsedReturns.success) {
|
|
3337
|
-
throw new ZodError([makeReturnsIssue(result, parsedReturns.error)]);
|
|
3338
|
-
}
|
|
3339
|
-
return parsedReturns.data;
|
|
3340
|
-
});
|
|
3341
|
-
}
|
|
3342
|
-
}
|
|
3343
|
-
parameters() {
|
|
3344
|
-
return this._def.args;
|
|
3345
|
-
}
|
|
3346
|
-
returnType() {
|
|
3347
|
-
return this._def.returns;
|
|
3348
|
-
}
|
|
3349
|
-
args(...items) {
|
|
3350
|
-
return new _ZodFunction({
|
|
3351
|
-
...this._def,
|
|
3352
|
-
args: ZodTuple.create(items).rest(ZodUnknown.create())
|
|
3353
|
-
});
|
|
3354
|
-
}
|
|
3355
|
-
returns(returnType) {
|
|
3356
|
-
return new _ZodFunction({
|
|
3357
|
-
...this._def,
|
|
3358
|
-
returns: returnType
|
|
3359
|
-
});
|
|
3360
|
-
}
|
|
3361
|
-
implement(func) {
|
|
3362
|
-
const validatedFunc = this.parse(func);
|
|
3363
|
-
return validatedFunc;
|
|
3364
|
-
}
|
|
3365
|
-
strictImplement(func) {
|
|
3366
|
-
const validatedFunc = this.parse(func);
|
|
3367
|
-
return validatedFunc;
|
|
3368
|
-
}
|
|
3369
|
-
static create(args, returns, params) {
|
|
3370
|
-
return new _ZodFunction({
|
|
3371
|
-
args: args ? args : ZodTuple.create([]).rest(ZodUnknown.create()),
|
|
3372
|
-
returns: returns || ZodUnknown.create(),
|
|
3373
|
-
typeName: ZodFirstPartyTypeKind.ZodFunction,
|
|
3374
|
-
...processCreateParams(params)
|
|
3375
|
-
});
|
|
3376
|
-
}
|
|
3377
|
-
};
|
|
3378
|
-
var ZodLazy = class extends ZodType {
|
|
3379
|
-
get schema() {
|
|
3380
|
-
return this._def.getter();
|
|
3381
|
-
}
|
|
3382
|
-
_parse(input) {
|
|
3383
|
-
const { ctx } = this._processInputParams(input);
|
|
3384
|
-
const lazySchema = this._def.getter();
|
|
3385
|
-
return lazySchema._parse({ data: ctx.data, path: ctx.path, parent: ctx });
|
|
3386
|
-
}
|
|
3387
|
-
};
|
|
3388
|
-
ZodLazy.create = (getter, params) => {
|
|
3389
|
-
return new ZodLazy({
|
|
3390
|
-
getter,
|
|
3391
|
-
typeName: ZodFirstPartyTypeKind.ZodLazy,
|
|
3392
|
-
...processCreateParams(params)
|
|
3393
|
-
});
|
|
3394
|
-
};
|
|
3395
|
-
var ZodLiteral = class extends ZodType {
|
|
3396
|
-
_parse(input) {
|
|
3397
|
-
if (input.data !== this._def.value) {
|
|
3398
|
-
const ctx = this._getOrReturnCtx(input);
|
|
3399
|
-
addIssueToContext(ctx, {
|
|
3400
|
-
received: ctx.data,
|
|
3401
|
-
code: ZodIssueCode.invalid_literal,
|
|
3402
|
-
expected: this._def.value
|
|
3403
|
-
});
|
|
3404
|
-
return INVALID;
|
|
3405
|
-
}
|
|
3406
|
-
return { status: "valid", value: input.data };
|
|
3407
|
-
}
|
|
3408
|
-
get value() {
|
|
3409
|
-
return this._def.value;
|
|
3410
|
-
}
|
|
3411
|
-
};
|
|
3412
|
-
ZodLiteral.create = (value, params) => {
|
|
3413
|
-
return new ZodLiteral({
|
|
3414
|
-
value,
|
|
3415
|
-
typeName: ZodFirstPartyTypeKind.ZodLiteral,
|
|
3416
|
-
...processCreateParams(params)
|
|
3417
|
-
});
|
|
3418
|
-
};
|
|
3419
|
-
function createZodEnum(values, params) {
|
|
3420
|
-
return new ZodEnum({
|
|
3421
|
-
values,
|
|
3422
|
-
typeName: ZodFirstPartyTypeKind.ZodEnum,
|
|
3423
|
-
...processCreateParams(params)
|
|
3424
|
-
});
|
|
3425
|
-
}
|
|
3426
|
-
var ZodEnum = class _ZodEnum extends ZodType {
|
|
3427
|
-
_parse(input) {
|
|
3428
|
-
if (typeof input.data !== "string") {
|
|
3429
|
-
const ctx = this._getOrReturnCtx(input);
|
|
3430
|
-
const expectedValues = this._def.values;
|
|
3431
|
-
addIssueToContext(ctx, {
|
|
3432
|
-
expected: util.joinValues(expectedValues),
|
|
3433
|
-
received: ctx.parsedType,
|
|
3434
|
-
code: ZodIssueCode.invalid_type
|
|
3435
|
-
});
|
|
3436
|
-
return INVALID;
|
|
3437
|
-
}
|
|
3438
|
-
if (!this._cache) {
|
|
3439
|
-
this._cache = new Set(this._def.values);
|
|
3440
|
-
}
|
|
3441
|
-
if (!this._cache.has(input.data)) {
|
|
3442
|
-
const ctx = this._getOrReturnCtx(input);
|
|
3443
|
-
const expectedValues = this._def.values;
|
|
3444
|
-
addIssueToContext(ctx, {
|
|
3445
|
-
received: ctx.data,
|
|
3446
|
-
code: ZodIssueCode.invalid_enum_value,
|
|
3447
|
-
options: expectedValues
|
|
3448
|
-
});
|
|
3449
|
-
return INVALID;
|
|
3450
|
-
}
|
|
3451
|
-
return OK(input.data);
|
|
3452
|
-
}
|
|
3453
|
-
get options() {
|
|
3454
|
-
return this._def.values;
|
|
3455
|
-
}
|
|
3456
|
-
get enum() {
|
|
3457
|
-
const enumValues = {};
|
|
3458
|
-
for (const val of this._def.values) {
|
|
3459
|
-
enumValues[val] = val;
|
|
3460
|
-
}
|
|
3461
|
-
return enumValues;
|
|
3462
|
-
}
|
|
3463
|
-
get Values() {
|
|
3464
|
-
const enumValues = {};
|
|
3465
|
-
for (const val of this._def.values) {
|
|
3466
|
-
enumValues[val] = val;
|
|
3467
|
-
}
|
|
3468
|
-
return enumValues;
|
|
3469
|
-
}
|
|
3470
|
-
get Enum() {
|
|
3471
|
-
const enumValues = {};
|
|
3472
|
-
for (const val of this._def.values) {
|
|
3473
|
-
enumValues[val] = val;
|
|
3474
|
-
}
|
|
3475
|
-
return enumValues;
|
|
3476
|
-
}
|
|
3477
|
-
extract(values, newDef = this._def) {
|
|
3478
|
-
return _ZodEnum.create(values, {
|
|
3479
|
-
...this._def,
|
|
3480
|
-
...newDef
|
|
3481
|
-
});
|
|
3482
|
-
}
|
|
3483
|
-
exclude(values, newDef = this._def) {
|
|
3484
|
-
return _ZodEnum.create(this.options.filter((opt) => !values.includes(opt)), {
|
|
3485
|
-
...this._def,
|
|
3486
|
-
...newDef
|
|
3487
|
-
});
|
|
3488
|
-
}
|
|
3489
|
-
};
|
|
3490
|
-
ZodEnum.create = createZodEnum;
|
|
3491
|
-
var ZodNativeEnum = class extends ZodType {
|
|
3492
|
-
_parse(input) {
|
|
3493
|
-
const nativeEnumValues = util.getValidEnumValues(this._def.values);
|
|
3494
|
-
const ctx = this._getOrReturnCtx(input);
|
|
3495
|
-
if (ctx.parsedType !== ZodParsedType.string && ctx.parsedType !== ZodParsedType.number) {
|
|
3496
|
-
const expectedValues = util.objectValues(nativeEnumValues);
|
|
3497
|
-
addIssueToContext(ctx, {
|
|
3498
|
-
expected: util.joinValues(expectedValues),
|
|
3499
|
-
received: ctx.parsedType,
|
|
3500
|
-
code: ZodIssueCode.invalid_type
|
|
3501
|
-
});
|
|
3502
|
-
return INVALID;
|
|
3503
|
-
}
|
|
3504
|
-
if (!this._cache) {
|
|
3505
|
-
this._cache = new Set(util.getValidEnumValues(this._def.values));
|
|
3506
|
-
}
|
|
3507
|
-
if (!this._cache.has(input.data)) {
|
|
3508
|
-
const expectedValues = util.objectValues(nativeEnumValues);
|
|
3509
|
-
addIssueToContext(ctx, {
|
|
3510
|
-
received: ctx.data,
|
|
3511
|
-
code: ZodIssueCode.invalid_enum_value,
|
|
3512
|
-
options: expectedValues
|
|
3513
|
-
});
|
|
3514
|
-
return INVALID;
|
|
3515
|
-
}
|
|
3516
|
-
return OK(input.data);
|
|
3517
|
-
}
|
|
3518
|
-
get enum() {
|
|
3519
|
-
return this._def.values;
|
|
3520
|
-
}
|
|
3521
|
-
};
|
|
3522
|
-
ZodNativeEnum.create = (values, params) => {
|
|
3523
|
-
return new ZodNativeEnum({
|
|
3524
|
-
values,
|
|
3525
|
-
typeName: ZodFirstPartyTypeKind.ZodNativeEnum,
|
|
3526
|
-
...processCreateParams(params)
|
|
3527
|
-
});
|
|
3528
|
-
};
|
|
3529
|
-
var ZodPromise = class extends ZodType {
|
|
3530
|
-
unwrap() {
|
|
3531
|
-
return this._def.type;
|
|
3532
|
-
}
|
|
3533
|
-
_parse(input) {
|
|
3534
|
-
const { ctx } = this._processInputParams(input);
|
|
3535
|
-
if (ctx.parsedType !== ZodParsedType.promise && ctx.common.async === false) {
|
|
3536
|
-
addIssueToContext(ctx, {
|
|
3537
|
-
code: ZodIssueCode.invalid_type,
|
|
3538
|
-
expected: ZodParsedType.promise,
|
|
3539
|
-
received: ctx.parsedType
|
|
3540
|
-
});
|
|
3541
|
-
return INVALID;
|
|
3542
|
-
}
|
|
3543
|
-
const promisified = ctx.parsedType === ZodParsedType.promise ? ctx.data : Promise.resolve(ctx.data);
|
|
3544
|
-
return OK(promisified.then((data) => {
|
|
3545
|
-
return this._def.type.parseAsync(data, {
|
|
3546
|
-
path: ctx.path,
|
|
3547
|
-
errorMap: ctx.common.contextualErrorMap
|
|
3548
|
-
});
|
|
3549
|
-
}));
|
|
3550
|
-
}
|
|
3551
|
-
};
|
|
3552
|
-
ZodPromise.create = (schema, params) => {
|
|
3553
|
-
return new ZodPromise({
|
|
3554
|
-
type: schema,
|
|
3555
|
-
typeName: ZodFirstPartyTypeKind.ZodPromise,
|
|
3556
|
-
...processCreateParams(params)
|
|
3557
|
-
});
|
|
3558
|
-
};
|
|
3559
|
-
var ZodEffects = class extends ZodType {
|
|
3560
|
-
innerType() {
|
|
3561
|
-
return this._def.schema;
|
|
3562
|
-
}
|
|
3563
|
-
sourceType() {
|
|
3564
|
-
return this._def.schema._def.typeName === ZodFirstPartyTypeKind.ZodEffects ? this._def.schema.sourceType() : this._def.schema;
|
|
3565
|
-
}
|
|
3566
|
-
_parse(input) {
|
|
3567
|
-
const { status, ctx } = this._processInputParams(input);
|
|
3568
|
-
const effect = this._def.effect || null;
|
|
3569
|
-
const checkCtx = {
|
|
3570
|
-
addIssue: (arg) => {
|
|
3571
|
-
addIssueToContext(ctx, arg);
|
|
3572
|
-
if (arg.fatal) {
|
|
3573
|
-
status.abort();
|
|
3574
|
-
} else {
|
|
3575
|
-
status.dirty();
|
|
3576
|
-
}
|
|
3577
|
-
},
|
|
3578
|
-
get path() {
|
|
3579
|
-
return ctx.path;
|
|
3580
|
-
}
|
|
3581
|
-
};
|
|
3582
|
-
checkCtx.addIssue = checkCtx.addIssue.bind(checkCtx);
|
|
3583
|
-
if (effect.type === "preprocess") {
|
|
3584
|
-
const processed = effect.transform(ctx.data, checkCtx);
|
|
3585
|
-
if (ctx.common.async) {
|
|
3586
|
-
return Promise.resolve(processed).then(async (processed2) => {
|
|
3587
|
-
if (status.value === "aborted")
|
|
3588
|
-
return INVALID;
|
|
3589
|
-
const result = await this._def.schema._parseAsync({
|
|
3590
|
-
data: processed2,
|
|
3591
|
-
path: ctx.path,
|
|
3592
|
-
parent: ctx
|
|
3593
|
-
});
|
|
3594
|
-
if (result.status === "aborted")
|
|
3595
|
-
return INVALID;
|
|
3596
|
-
if (result.status === "dirty")
|
|
3597
|
-
return DIRTY(result.value);
|
|
3598
|
-
if (status.value === "dirty")
|
|
3599
|
-
return DIRTY(result.value);
|
|
3600
|
-
return result;
|
|
3601
|
-
});
|
|
3602
|
-
} else {
|
|
3603
|
-
if (status.value === "aborted")
|
|
3604
|
-
return INVALID;
|
|
3605
|
-
const result = this._def.schema._parseSync({
|
|
3606
|
-
data: processed,
|
|
3607
|
-
path: ctx.path,
|
|
3608
|
-
parent: ctx
|
|
3609
|
-
});
|
|
3610
|
-
if (result.status === "aborted")
|
|
3611
|
-
return INVALID;
|
|
3612
|
-
if (result.status === "dirty")
|
|
3613
|
-
return DIRTY(result.value);
|
|
3614
|
-
if (status.value === "dirty")
|
|
3615
|
-
return DIRTY(result.value);
|
|
3616
|
-
return result;
|
|
3617
|
-
}
|
|
3618
|
-
}
|
|
3619
|
-
if (effect.type === "refinement") {
|
|
3620
|
-
const executeRefinement = (acc) => {
|
|
3621
|
-
const result = effect.refinement(acc, checkCtx);
|
|
3622
|
-
if (ctx.common.async) {
|
|
3623
|
-
return Promise.resolve(result);
|
|
3624
|
-
}
|
|
3625
|
-
if (result instanceof Promise) {
|
|
3626
|
-
throw new Error("Async refinement encountered during synchronous parse operation. Use .parseAsync instead.");
|
|
3627
|
-
}
|
|
3628
|
-
return acc;
|
|
3629
|
-
};
|
|
3630
|
-
if (ctx.common.async === false) {
|
|
3631
|
-
const inner = this._def.schema._parseSync({
|
|
3632
|
-
data: ctx.data,
|
|
3633
|
-
path: ctx.path,
|
|
3634
|
-
parent: ctx
|
|
3635
|
-
});
|
|
3636
|
-
if (inner.status === "aborted")
|
|
3637
|
-
return INVALID;
|
|
3638
|
-
if (inner.status === "dirty")
|
|
3639
|
-
status.dirty();
|
|
3640
|
-
executeRefinement(inner.value);
|
|
3641
|
-
return { status: status.value, value: inner.value };
|
|
3642
|
-
} else {
|
|
3643
|
-
return this._def.schema._parseAsync({ data: ctx.data, path: ctx.path, parent: ctx }).then((inner) => {
|
|
3644
|
-
if (inner.status === "aborted")
|
|
3645
|
-
return INVALID;
|
|
3646
|
-
if (inner.status === "dirty")
|
|
3647
|
-
status.dirty();
|
|
3648
|
-
return executeRefinement(inner.value).then(() => {
|
|
3649
|
-
return { status: status.value, value: inner.value };
|
|
3650
|
-
});
|
|
3651
|
-
});
|
|
3652
|
-
}
|
|
3653
|
-
}
|
|
3654
|
-
if (effect.type === "transform") {
|
|
3655
|
-
if (ctx.common.async === false) {
|
|
3656
|
-
const base = this._def.schema._parseSync({
|
|
3657
|
-
data: ctx.data,
|
|
3658
|
-
path: ctx.path,
|
|
3659
|
-
parent: ctx
|
|
3660
|
-
});
|
|
3661
|
-
if (!isValid(base))
|
|
3662
|
-
return INVALID;
|
|
3663
|
-
const result = effect.transform(base.value, checkCtx);
|
|
3664
|
-
if (result instanceof Promise) {
|
|
3665
|
-
throw new Error(`Asynchronous transform encountered during synchronous parse operation. Use .parseAsync instead.`);
|
|
3666
|
-
}
|
|
3667
|
-
return { status: status.value, value: result };
|
|
3668
|
-
} else {
|
|
3669
|
-
return this._def.schema._parseAsync({ data: ctx.data, path: ctx.path, parent: ctx }).then((base) => {
|
|
3670
|
-
if (!isValid(base))
|
|
3671
|
-
return INVALID;
|
|
3672
|
-
return Promise.resolve(effect.transform(base.value, checkCtx)).then((result) => ({
|
|
3673
|
-
status: status.value,
|
|
3674
|
-
value: result
|
|
3675
|
-
}));
|
|
3676
|
-
});
|
|
3677
|
-
}
|
|
3678
|
-
}
|
|
3679
|
-
util.assertNever(effect);
|
|
3680
|
-
}
|
|
3681
|
-
};
|
|
3682
|
-
ZodEffects.create = (schema, effect, params) => {
|
|
3683
|
-
return new ZodEffects({
|
|
3684
|
-
schema,
|
|
3685
|
-
typeName: ZodFirstPartyTypeKind.ZodEffects,
|
|
3686
|
-
effect,
|
|
3687
|
-
...processCreateParams(params)
|
|
3688
|
-
});
|
|
3689
|
-
};
|
|
3690
|
-
ZodEffects.createWithPreprocess = (preprocess, schema, params) => {
|
|
3691
|
-
return new ZodEffects({
|
|
3692
|
-
schema,
|
|
3693
|
-
effect: { type: "preprocess", transform: preprocess },
|
|
3694
|
-
typeName: ZodFirstPartyTypeKind.ZodEffects,
|
|
3695
|
-
...processCreateParams(params)
|
|
3696
|
-
});
|
|
3697
|
-
};
|
|
3698
|
-
var ZodOptional = class extends ZodType {
|
|
3699
|
-
_parse(input) {
|
|
3700
|
-
const parsedType = this._getType(input);
|
|
3701
|
-
if (parsedType === ZodParsedType.undefined) {
|
|
3702
|
-
return OK(void 0);
|
|
3703
|
-
}
|
|
3704
|
-
return this._def.innerType._parse(input);
|
|
3705
|
-
}
|
|
3706
|
-
unwrap() {
|
|
3707
|
-
return this._def.innerType;
|
|
3708
|
-
}
|
|
3709
|
-
};
|
|
3710
|
-
ZodOptional.create = (type, params) => {
|
|
3711
|
-
return new ZodOptional({
|
|
3712
|
-
innerType: type,
|
|
3713
|
-
typeName: ZodFirstPartyTypeKind.ZodOptional,
|
|
3714
|
-
...processCreateParams(params)
|
|
3715
|
-
});
|
|
3716
|
-
};
|
|
3717
|
-
var ZodNullable = class extends ZodType {
|
|
3718
|
-
_parse(input) {
|
|
3719
|
-
const parsedType = this._getType(input);
|
|
3720
|
-
if (parsedType === ZodParsedType.null) {
|
|
3721
|
-
return OK(null);
|
|
3722
|
-
}
|
|
3723
|
-
return this._def.innerType._parse(input);
|
|
3724
|
-
}
|
|
3725
|
-
unwrap() {
|
|
3726
|
-
return this._def.innerType;
|
|
3727
|
-
}
|
|
3728
|
-
};
|
|
3729
|
-
ZodNullable.create = (type, params) => {
|
|
3730
|
-
return new ZodNullable({
|
|
3731
|
-
innerType: type,
|
|
3732
|
-
typeName: ZodFirstPartyTypeKind.ZodNullable,
|
|
3733
|
-
...processCreateParams(params)
|
|
3734
|
-
});
|
|
3735
|
-
};
|
|
3736
|
-
var ZodDefault = class extends ZodType {
|
|
3737
|
-
_parse(input) {
|
|
3738
|
-
const { ctx } = this._processInputParams(input);
|
|
3739
|
-
let data = ctx.data;
|
|
3740
|
-
if (ctx.parsedType === ZodParsedType.undefined) {
|
|
3741
|
-
data = this._def.defaultValue();
|
|
3742
|
-
}
|
|
3743
|
-
return this._def.innerType._parse({
|
|
3744
|
-
data,
|
|
3745
|
-
path: ctx.path,
|
|
3746
|
-
parent: ctx
|
|
3747
|
-
});
|
|
3748
|
-
}
|
|
3749
|
-
removeDefault() {
|
|
3750
|
-
return this._def.innerType;
|
|
3751
|
-
}
|
|
3752
|
-
};
|
|
3753
|
-
ZodDefault.create = (type, params) => {
|
|
3754
|
-
return new ZodDefault({
|
|
3755
|
-
innerType: type,
|
|
3756
|
-
typeName: ZodFirstPartyTypeKind.ZodDefault,
|
|
3757
|
-
defaultValue: typeof params.default === "function" ? params.default : () => params.default,
|
|
3758
|
-
...processCreateParams(params)
|
|
3759
|
-
});
|
|
3760
|
-
};
|
|
3761
|
-
var ZodCatch = class extends ZodType {
|
|
3762
|
-
_parse(input) {
|
|
3763
|
-
const { ctx } = this._processInputParams(input);
|
|
3764
|
-
const newCtx = {
|
|
3765
|
-
...ctx,
|
|
3766
|
-
common: {
|
|
3767
|
-
...ctx.common,
|
|
3768
|
-
issues: []
|
|
3769
|
-
}
|
|
3770
|
-
};
|
|
3771
|
-
const result = this._def.innerType._parse({
|
|
3772
|
-
data: newCtx.data,
|
|
3773
|
-
path: newCtx.path,
|
|
3774
|
-
parent: {
|
|
3775
|
-
...newCtx
|
|
3776
|
-
}
|
|
3777
|
-
});
|
|
3778
|
-
if (isAsync(result)) {
|
|
3779
|
-
return result.then((result2) => {
|
|
3780
|
-
return {
|
|
3781
|
-
status: "valid",
|
|
3782
|
-
value: result2.status === "valid" ? result2.value : this._def.catchValue({
|
|
3783
|
-
get error() {
|
|
3784
|
-
return new ZodError(newCtx.common.issues);
|
|
3785
|
-
},
|
|
3786
|
-
input: newCtx.data
|
|
3787
|
-
})
|
|
3788
|
-
};
|
|
3789
|
-
});
|
|
3790
|
-
} else {
|
|
3791
|
-
return {
|
|
3792
|
-
status: "valid",
|
|
3793
|
-
value: result.status === "valid" ? result.value : this._def.catchValue({
|
|
3794
|
-
get error() {
|
|
3795
|
-
return new ZodError(newCtx.common.issues);
|
|
3796
|
-
},
|
|
3797
|
-
input: newCtx.data
|
|
3798
|
-
})
|
|
3799
|
-
};
|
|
3800
|
-
}
|
|
3801
|
-
}
|
|
3802
|
-
removeCatch() {
|
|
3803
|
-
return this._def.innerType;
|
|
3804
|
-
}
|
|
3805
|
-
};
|
|
3806
|
-
ZodCatch.create = (type, params) => {
|
|
3807
|
-
return new ZodCatch({
|
|
3808
|
-
innerType: type,
|
|
3809
|
-
typeName: ZodFirstPartyTypeKind.ZodCatch,
|
|
3810
|
-
catchValue: typeof params.catch === "function" ? params.catch : () => params.catch,
|
|
3811
|
-
...processCreateParams(params)
|
|
3812
|
-
});
|
|
3813
|
-
};
|
|
3814
|
-
var ZodNaN = class extends ZodType {
|
|
3815
|
-
_parse(input) {
|
|
3816
|
-
const parsedType = this._getType(input);
|
|
3817
|
-
if (parsedType !== ZodParsedType.nan) {
|
|
3818
|
-
const ctx = this._getOrReturnCtx(input);
|
|
3819
|
-
addIssueToContext(ctx, {
|
|
3820
|
-
code: ZodIssueCode.invalid_type,
|
|
3821
|
-
expected: ZodParsedType.nan,
|
|
3822
|
-
received: ctx.parsedType
|
|
3823
|
-
});
|
|
3824
|
-
return INVALID;
|
|
3825
|
-
}
|
|
3826
|
-
return { status: "valid", value: input.data };
|
|
3827
|
-
}
|
|
3828
|
-
};
|
|
3829
|
-
ZodNaN.create = (params) => {
|
|
3830
|
-
return new ZodNaN({
|
|
3831
|
-
typeName: ZodFirstPartyTypeKind.ZodNaN,
|
|
3832
|
-
...processCreateParams(params)
|
|
3833
|
-
});
|
|
3834
|
-
};
|
|
3835
|
-
var BRAND = Symbol("zod_brand");
|
|
3836
|
-
var ZodBranded = class extends ZodType {
|
|
3837
|
-
_parse(input) {
|
|
3838
|
-
const { ctx } = this._processInputParams(input);
|
|
3839
|
-
const data = ctx.data;
|
|
3840
|
-
return this._def.type._parse({
|
|
3841
|
-
data,
|
|
3842
|
-
path: ctx.path,
|
|
3843
|
-
parent: ctx
|
|
3844
|
-
});
|
|
3845
|
-
}
|
|
3846
|
-
unwrap() {
|
|
3847
|
-
return this._def.type;
|
|
3848
|
-
}
|
|
3849
|
-
};
|
|
3850
|
-
var ZodPipeline = class _ZodPipeline extends ZodType {
|
|
3851
|
-
_parse(input) {
|
|
3852
|
-
const { status, ctx } = this._processInputParams(input);
|
|
3853
|
-
if (ctx.common.async) {
|
|
3854
|
-
const handleAsync = async () => {
|
|
3855
|
-
const inResult = await this._def.in._parseAsync({
|
|
3856
|
-
data: ctx.data,
|
|
3857
|
-
path: ctx.path,
|
|
3858
|
-
parent: ctx
|
|
3859
|
-
});
|
|
3860
|
-
if (inResult.status === "aborted")
|
|
3861
|
-
return INVALID;
|
|
3862
|
-
if (inResult.status === "dirty") {
|
|
3863
|
-
status.dirty();
|
|
3864
|
-
return DIRTY(inResult.value);
|
|
3865
|
-
} else {
|
|
3866
|
-
return this._def.out._parseAsync({
|
|
3867
|
-
data: inResult.value,
|
|
3868
|
-
path: ctx.path,
|
|
3869
|
-
parent: ctx
|
|
3870
|
-
});
|
|
3871
|
-
}
|
|
3872
|
-
};
|
|
3873
|
-
return handleAsync();
|
|
3874
|
-
} else {
|
|
3875
|
-
const inResult = this._def.in._parseSync({
|
|
3876
|
-
data: ctx.data,
|
|
3877
|
-
path: ctx.path,
|
|
3878
|
-
parent: ctx
|
|
3879
|
-
});
|
|
3880
|
-
if (inResult.status === "aborted")
|
|
3881
|
-
return INVALID;
|
|
3882
|
-
if (inResult.status === "dirty") {
|
|
3883
|
-
status.dirty();
|
|
3884
|
-
return {
|
|
3885
|
-
status: "dirty",
|
|
3886
|
-
value: inResult.value
|
|
3887
|
-
};
|
|
3888
|
-
} else {
|
|
3889
|
-
return this._def.out._parseSync({
|
|
3890
|
-
data: inResult.value,
|
|
3891
|
-
path: ctx.path,
|
|
3892
|
-
parent: ctx
|
|
3893
|
-
});
|
|
3894
|
-
}
|
|
3895
|
-
}
|
|
3896
|
-
}
|
|
3897
|
-
static create(a, b) {
|
|
3898
|
-
return new _ZodPipeline({
|
|
3899
|
-
in: a,
|
|
3900
|
-
out: b,
|
|
3901
|
-
typeName: ZodFirstPartyTypeKind.ZodPipeline
|
|
3902
|
-
});
|
|
3903
|
-
}
|
|
3904
|
-
};
|
|
3905
|
-
var ZodReadonly = class extends ZodType {
|
|
3906
|
-
_parse(input) {
|
|
3907
|
-
const result = this._def.innerType._parse(input);
|
|
3908
|
-
const freeze = (data) => {
|
|
3909
|
-
if (isValid(data)) {
|
|
3910
|
-
data.value = Object.freeze(data.value);
|
|
3911
|
-
}
|
|
3912
|
-
return data;
|
|
3913
|
-
};
|
|
3914
|
-
return isAsync(result) ? result.then((data) => freeze(data)) : freeze(result);
|
|
3915
|
-
}
|
|
3916
|
-
unwrap() {
|
|
3917
|
-
return this._def.innerType;
|
|
3918
|
-
}
|
|
3919
|
-
};
|
|
3920
|
-
ZodReadonly.create = (type, params) => {
|
|
3921
|
-
return new ZodReadonly({
|
|
3922
|
-
innerType: type,
|
|
3923
|
-
typeName: ZodFirstPartyTypeKind.ZodReadonly,
|
|
3924
|
-
...processCreateParams(params)
|
|
3925
|
-
});
|
|
3926
|
-
};
|
|
3927
|
-
function cleanParams(params, data) {
|
|
3928
|
-
const p = typeof params === "function" ? params(data) : typeof params === "string" ? { message: params } : params;
|
|
3929
|
-
const p2 = typeof p === "string" ? { message: p } : p;
|
|
3930
|
-
return p2;
|
|
3931
|
-
}
|
|
3932
|
-
function custom(check, _params = {}, fatal) {
|
|
3933
|
-
if (check)
|
|
3934
|
-
return ZodAny.create().superRefine((data, ctx) => {
|
|
3935
|
-
const r = check(data);
|
|
3936
|
-
if (r instanceof Promise) {
|
|
3937
|
-
return r.then((r2) => {
|
|
3938
|
-
if (!r2) {
|
|
3939
|
-
const params = cleanParams(_params, data);
|
|
3940
|
-
const _fatal = params.fatal ?? fatal ?? true;
|
|
3941
|
-
ctx.addIssue({ code: "custom", ...params, fatal: _fatal });
|
|
3942
|
-
}
|
|
3943
|
-
});
|
|
3944
|
-
}
|
|
3945
|
-
if (!r) {
|
|
3946
|
-
const params = cleanParams(_params, data);
|
|
3947
|
-
const _fatal = params.fatal ?? fatal ?? true;
|
|
3948
|
-
ctx.addIssue({ code: "custom", ...params, fatal: _fatal });
|
|
3949
|
-
}
|
|
3950
|
-
return;
|
|
3951
|
-
});
|
|
3952
|
-
return ZodAny.create();
|
|
3953
|
-
}
|
|
3954
|
-
var late = {
|
|
3955
|
-
object: ZodObject.lazycreate
|
|
3956
|
-
};
|
|
3957
|
-
var ZodFirstPartyTypeKind;
|
|
3958
|
-
(function(ZodFirstPartyTypeKind2) {
|
|
3959
|
-
ZodFirstPartyTypeKind2["ZodString"] = "ZodString";
|
|
3960
|
-
ZodFirstPartyTypeKind2["ZodNumber"] = "ZodNumber";
|
|
3961
|
-
ZodFirstPartyTypeKind2["ZodNaN"] = "ZodNaN";
|
|
3962
|
-
ZodFirstPartyTypeKind2["ZodBigInt"] = "ZodBigInt";
|
|
3963
|
-
ZodFirstPartyTypeKind2["ZodBoolean"] = "ZodBoolean";
|
|
3964
|
-
ZodFirstPartyTypeKind2["ZodDate"] = "ZodDate";
|
|
3965
|
-
ZodFirstPartyTypeKind2["ZodSymbol"] = "ZodSymbol";
|
|
3966
|
-
ZodFirstPartyTypeKind2["ZodUndefined"] = "ZodUndefined";
|
|
3967
|
-
ZodFirstPartyTypeKind2["ZodNull"] = "ZodNull";
|
|
3968
|
-
ZodFirstPartyTypeKind2["ZodAny"] = "ZodAny";
|
|
3969
|
-
ZodFirstPartyTypeKind2["ZodUnknown"] = "ZodUnknown";
|
|
3970
|
-
ZodFirstPartyTypeKind2["ZodNever"] = "ZodNever";
|
|
3971
|
-
ZodFirstPartyTypeKind2["ZodVoid"] = "ZodVoid";
|
|
3972
|
-
ZodFirstPartyTypeKind2["ZodArray"] = "ZodArray";
|
|
3973
|
-
ZodFirstPartyTypeKind2["ZodObject"] = "ZodObject";
|
|
3974
|
-
ZodFirstPartyTypeKind2["ZodUnion"] = "ZodUnion";
|
|
3975
|
-
ZodFirstPartyTypeKind2["ZodDiscriminatedUnion"] = "ZodDiscriminatedUnion";
|
|
3976
|
-
ZodFirstPartyTypeKind2["ZodIntersection"] = "ZodIntersection";
|
|
3977
|
-
ZodFirstPartyTypeKind2["ZodTuple"] = "ZodTuple";
|
|
3978
|
-
ZodFirstPartyTypeKind2["ZodRecord"] = "ZodRecord";
|
|
3979
|
-
ZodFirstPartyTypeKind2["ZodMap"] = "ZodMap";
|
|
3980
|
-
ZodFirstPartyTypeKind2["ZodSet"] = "ZodSet";
|
|
3981
|
-
ZodFirstPartyTypeKind2["ZodFunction"] = "ZodFunction";
|
|
3982
|
-
ZodFirstPartyTypeKind2["ZodLazy"] = "ZodLazy";
|
|
3983
|
-
ZodFirstPartyTypeKind2["ZodLiteral"] = "ZodLiteral";
|
|
3984
|
-
ZodFirstPartyTypeKind2["ZodEnum"] = "ZodEnum";
|
|
3985
|
-
ZodFirstPartyTypeKind2["ZodEffects"] = "ZodEffects";
|
|
3986
|
-
ZodFirstPartyTypeKind2["ZodNativeEnum"] = "ZodNativeEnum";
|
|
3987
|
-
ZodFirstPartyTypeKind2["ZodOptional"] = "ZodOptional";
|
|
3988
|
-
ZodFirstPartyTypeKind2["ZodNullable"] = "ZodNullable";
|
|
3989
|
-
ZodFirstPartyTypeKind2["ZodDefault"] = "ZodDefault";
|
|
3990
|
-
ZodFirstPartyTypeKind2["ZodCatch"] = "ZodCatch";
|
|
3991
|
-
ZodFirstPartyTypeKind2["ZodPromise"] = "ZodPromise";
|
|
3992
|
-
ZodFirstPartyTypeKind2["ZodBranded"] = "ZodBranded";
|
|
3993
|
-
ZodFirstPartyTypeKind2["ZodPipeline"] = "ZodPipeline";
|
|
3994
|
-
ZodFirstPartyTypeKind2["ZodReadonly"] = "ZodReadonly";
|
|
3995
|
-
})(ZodFirstPartyTypeKind || (ZodFirstPartyTypeKind = {}));
|
|
3996
|
-
var instanceOfType = (cls, params = {
|
|
3997
|
-
message: `Input not instance of ${cls.name}`
|
|
3998
|
-
}) => custom((data) => data instanceof cls, params);
|
|
3999
|
-
var stringType = ZodString.create;
|
|
4000
|
-
var numberType = ZodNumber.create;
|
|
4001
|
-
var nanType = ZodNaN.create;
|
|
4002
|
-
var bigIntType = ZodBigInt.create;
|
|
4003
|
-
var booleanType = ZodBoolean.create;
|
|
4004
|
-
var dateType = ZodDate.create;
|
|
4005
|
-
var symbolType = ZodSymbol.create;
|
|
4006
|
-
var undefinedType = ZodUndefined.create;
|
|
4007
|
-
var nullType = ZodNull.create;
|
|
4008
|
-
var anyType = ZodAny.create;
|
|
4009
|
-
var unknownType = ZodUnknown.create;
|
|
4010
|
-
var neverType = ZodNever.create;
|
|
4011
|
-
var voidType = ZodVoid.create;
|
|
4012
|
-
var arrayType = ZodArray.create;
|
|
4013
|
-
var objectType = ZodObject.create;
|
|
4014
|
-
var strictObjectType = ZodObject.strictCreate;
|
|
4015
|
-
var unionType = ZodUnion.create;
|
|
4016
|
-
var discriminatedUnionType = ZodDiscriminatedUnion.create;
|
|
4017
|
-
var intersectionType = ZodIntersection.create;
|
|
4018
|
-
var tupleType = ZodTuple.create;
|
|
4019
|
-
var recordType = ZodRecord.create;
|
|
4020
|
-
var mapType = ZodMap.create;
|
|
4021
|
-
var setType = ZodSet.create;
|
|
4022
|
-
var functionType = ZodFunction.create;
|
|
4023
|
-
var lazyType = ZodLazy.create;
|
|
4024
|
-
var literalType = ZodLiteral.create;
|
|
4025
|
-
var enumType = ZodEnum.create;
|
|
4026
|
-
var nativeEnumType = ZodNativeEnum.create;
|
|
4027
|
-
var promiseType = ZodPromise.create;
|
|
4028
|
-
var effectsType = ZodEffects.create;
|
|
4029
|
-
var optionalType = ZodOptional.create;
|
|
4030
|
-
var nullableType = ZodNullable.create;
|
|
4031
|
-
var preprocessType = ZodEffects.createWithPreprocess;
|
|
4032
|
-
var pipelineType = ZodPipeline.create;
|
|
4033
|
-
var ostring = () => stringType().optional();
|
|
4034
|
-
var onumber = () => numberType().optional();
|
|
4035
|
-
var oboolean = () => booleanType().optional();
|
|
4036
|
-
var coerce = {
|
|
4037
|
-
string: ((arg) => ZodString.create({ ...arg, coerce: true })),
|
|
4038
|
-
number: ((arg) => ZodNumber.create({ ...arg, coerce: true })),
|
|
4039
|
-
boolean: ((arg) => ZodBoolean.create({
|
|
4040
|
-
...arg,
|
|
4041
|
-
coerce: true
|
|
4042
|
-
})),
|
|
4043
|
-
bigint: ((arg) => ZodBigInt.create({ ...arg, coerce: true })),
|
|
4044
|
-
date: ((arg) => ZodDate.create({ ...arg, coerce: true }))
|
|
4045
|
-
};
|
|
4046
|
-
var NEVER = INVALID;
|
|
4047
|
-
|
|
4048
1
|
// src/client.ts
|
|
2
|
+
import { z } from "zod";
|
|
4049
3
|
var RichError = class extends Error {
|
|
4050
4
|
status;
|
|
4051
|
-
// HTTP status code (e.g. 404, 500)
|
|
4052
5
|
code;
|
|
4053
|
-
// Application-level error code (e.g. "USER_NOT_FOUND")
|
|
4054
6
|
title;
|
|
4055
|
-
// Optional title or short summary
|
|
4056
7
|
detail;
|
|
4057
|
-
// Additional human-readable explanation
|
|
4058
8
|
errors;
|
|
4059
|
-
// Field-specific validation errors
|
|
4060
9
|
constructor(error) {
|
|
4061
10
|
super(error.message);
|
|
4062
11
|
Object.assign(this, error);
|
|
4063
12
|
}
|
|
4064
13
|
};
|
|
14
|
+
var REQUEST_PART_KEYS = /* @__PURE__ */ new Set([
|
|
15
|
+
"path",
|
|
16
|
+
"query",
|
|
17
|
+
"body",
|
|
18
|
+
"headers",
|
|
19
|
+
"header"
|
|
20
|
+
]);
|
|
4065
21
|
var ApiClient = class {
|
|
4066
22
|
constructor(config, contracts) {
|
|
4067
23
|
this.config = config;
|
|
@@ -4070,28 +26,17 @@ var ApiClient = class {
|
|
|
4070
26
|
this.mockDelay = config.mockDelay || { min: 100, max: 1e3 };
|
|
4071
27
|
this.tokenProvider = config.tokenProvider;
|
|
4072
28
|
}
|
|
29
|
+
config;
|
|
30
|
+
contracts;
|
|
4073
31
|
middlewares = [];
|
|
4074
|
-
// Registered middlewares
|
|
4075
32
|
errorHandler;
|
|
4076
|
-
// Global error handler
|
|
4077
33
|
responseTransform = (d) => d;
|
|
4078
|
-
// Post-parse data transformer
|
|
4079
34
|
useMockData = false;
|
|
4080
|
-
// Whether mock responses are enabled
|
|
4081
35
|
mockDelay = { min: 100, max: 1e3 };
|
|
4082
|
-
// Mock latency range (ms)
|
|
4083
36
|
responseWrapper;
|
|
4084
|
-
// Wrapper for APIs with envelope structures
|
|
4085
37
|
tokenProvider;
|
|
4086
|
-
// Optional async token supplier
|
|
4087
|
-
// Configuration for retry behavior
|
|
4088
38
|
retryConfig;
|
|
4089
|
-
// Holds generated API endpoint methods
|
|
4090
39
|
_modules;
|
|
4091
|
-
/**
|
|
4092
|
-
* Builds all API methods (`modules`) dynamically from the Zod contract definition.
|
|
4093
|
-
* After `init()` is called, each endpoint can be invoked through `client.modules.moduleName.endpointName()`.
|
|
4094
|
-
*/
|
|
4095
40
|
init() {
|
|
4096
41
|
const modules = {};
|
|
4097
42
|
for (const moduleName in this.contracts) {
|
|
@@ -4104,92 +49,83 @@ var ApiClient = class {
|
|
|
4104
49
|
}
|
|
4105
50
|
this._modules = modules;
|
|
4106
51
|
}
|
|
4107
|
-
/** Provides access to initialized modules after calling `init()`. */
|
|
4108
52
|
get modules() {
|
|
4109
53
|
return this._modules;
|
|
4110
54
|
}
|
|
4111
|
-
/** Registers a new middleware in the client’s pipeline. */
|
|
4112
55
|
use(middleware, options) {
|
|
4113
56
|
this.middlewares.push({ fn: middleware, options });
|
|
4114
57
|
}
|
|
4115
|
-
/** Sets a global error handler function to unify error behavior. */
|
|
4116
58
|
onError(handler) {
|
|
4117
59
|
this.errorHandler = handler;
|
|
4118
60
|
}
|
|
4119
|
-
/** Defines a global transform function applied to all validated responses. */
|
|
4120
61
|
useResponseTransform(fn) {
|
|
4121
62
|
this.responseTransform = fn;
|
|
4122
63
|
}
|
|
4123
|
-
/** Configures the retry logic (max attempts, backoff mode, etc.). */
|
|
4124
64
|
setRetryConfig(config) {
|
|
4125
65
|
this.retryConfig = config;
|
|
4126
66
|
}
|
|
4127
|
-
/** Provides a custom token provider that returns tokens dynamically. */
|
|
4128
67
|
setTokenProvider(provider) {
|
|
4129
68
|
this.tokenProvider = provider;
|
|
4130
69
|
}
|
|
4131
|
-
/** Enables mock responses instead of network requests. */
|
|
4132
70
|
setMockMode(enabled, delay) {
|
|
4133
71
|
this.useMockData = enabled;
|
|
4134
72
|
if (delay) this.mockDelay = delay;
|
|
4135
73
|
}
|
|
4136
|
-
/** Registers a wrapper schema for APIs that nest response data (e.g. `{ data, success, message }`). */
|
|
4137
74
|
setResponseWrapper(wrapper) {
|
|
4138
75
|
this.responseWrapper = wrapper;
|
|
4139
76
|
}
|
|
4140
|
-
/** Retrieves the current auth token, using a provider if available. */
|
|
4141
77
|
async getCurrentToken() {
|
|
4142
78
|
if (this.tokenProvider) return await this.tokenProvider();
|
|
4143
79
|
return this.config.token;
|
|
4144
80
|
}
|
|
4145
|
-
/**
|
|
4146
|
-
* Core request entry point used by auto-generated endpoint methods.
|
|
4147
|
-
* Handles caching, deduplication, and mock mode routing.
|
|
4148
|
-
*/
|
|
4149
81
|
async request(endpoint, input, options) {
|
|
4150
82
|
const parsedInput = endpoint.request.parse(input);
|
|
4151
83
|
if (this.useMockData && endpoint.mockData) {
|
|
4152
84
|
return this.handleMockRequest(endpoint);
|
|
4153
85
|
}
|
|
4154
|
-
const
|
|
4155
|
-
|
|
4156
|
-
const promise = this.performRequestLogic(
|
|
86
|
+
const built = this.buildUrlAndBody(endpoint, parsedInput);
|
|
87
|
+
return this.performRequestLogic(
|
|
4157
88
|
endpoint,
|
|
4158
89
|
parsedInput,
|
|
4159
|
-
url,
|
|
4160
|
-
body,
|
|
4161
|
-
|
|
90
|
+
built.url,
|
|
91
|
+
built.body,
|
|
92
|
+
built.headers,
|
|
93
|
+
built.parts,
|
|
4162
94
|
options
|
|
4163
95
|
);
|
|
4164
|
-
return promise;
|
|
4165
96
|
}
|
|
4166
|
-
|
|
4167
|
-
* Full HTTP request workflow:
|
|
4168
|
-
* - Token injection
|
|
4169
|
-
* - Timeout support
|
|
4170
|
-
* - Middleware pipeline
|
|
4171
|
-
* - Fetch + response handling
|
|
4172
|
-
* - Zod parsing + transformation
|
|
4173
|
-
* - Caching
|
|
4174
|
-
*/
|
|
4175
|
-
async performRequestLogic(endpoint, parsedInput, url, body, key, options) {
|
|
97
|
+
async performRequestLogic(endpoint, parsedInput, url, body, requestHeaders, requestParts, options) {
|
|
4176
98
|
const headers = {};
|
|
4177
|
-
|
|
4178
|
-
if (endpoint.auth && !token) {
|
|
4179
|
-
const error = this.createError({
|
|
4180
|
-
message: `Missing token for ${endpoint.path}`,
|
|
4181
|
-
status: 401,
|
|
4182
|
-
code: "NO_TOKEN"
|
|
4183
|
-
});
|
|
4184
|
-
this.errorHandler?.(error);
|
|
4185
|
-
throw error;
|
|
4186
|
-
}
|
|
4187
|
-
if (endpoint.auth && token) headers["Authorization"] = `Bearer ${token}`;
|
|
4188
|
-
if (endpoint.bodyType !== "form-data")
|
|
99
|
+
if (endpoint.bodyType !== "form-data") {
|
|
4189
100
|
headers["Content-Type"] = "application/json";
|
|
101
|
+
}
|
|
102
|
+
const endpointHeaders = typeof endpoint.headers === "function" ? endpoint.headers(parsedInput) : endpoint.headers;
|
|
103
|
+
Object.assign(
|
|
104
|
+
headers,
|
|
105
|
+
this.normalizeHeaders(endpointHeaders),
|
|
106
|
+
requestHeaders
|
|
107
|
+
);
|
|
108
|
+
if (endpoint.auth) {
|
|
109
|
+
const token = await this.getCurrentToken();
|
|
110
|
+
if (!token) {
|
|
111
|
+
const error = this.createError({
|
|
112
|
+
message: `Missing token for ${endpoint.path}`,
|
|
113
|
+
status: 401,
|
|
114
|
+
code: "NO_TOKEN"
|
|
115
|
+
});
|
|
116
|
+
this.errorHandler?.(error);
|
|
117
|
+
throw error;
|
|
118
|
+
}
|
|
119
|
+
headers["Authorization"] = `Bearer ${token}`;
|
|
120
|
+
}
|
|
4190
121
|
const ctx = {
|
|
4191
122
|
url,
|
|
4192
|
-
init: { method: endpoint.method, headers, body }
|
|
123
|
+
init: { method: endpoint.method, headers, body },
|
|
124
|
+
endpoint,
|
|
125
|
+
request: {
|
|
126
|
+
...requestParts,
|
|
127
|
+
rawInput: parsedInput
|
|
128
|
+
}
|
|
4193
129
|
};
|
|
4194
130
|
let controller;
|
|
4195
131
|
let timeoutId;
|
|
@@ -4197,8 +133,9 @@ var ApiClient = class {
|
|
|
4197
133
|
controller = new AbortController();
|
|
4198
134
|
timeoutId = setTimeout(() => controller.abort(), options.timeout);
|
|
4199
135
|
}
|
|
4200
|
-
if (options?.signal || controller)
|
|
136
|
+
if (options?.signal || controller) {
|
|
4201
137
|
ctx.init.signal = options?.signal || controller?.signal;
|
|
138
|
+
}
|
|
4202
139
|
const runner = this.middlewares.reduceRight(
|
|
4203
140
|
(next, mw) => () => mw.fn(ctx, next, mw.options),
|
|
4204
141
|
() => fetch(ctx.url, ctx.init)
|
|
@@ -4212,9 +149,9 @@ var ApiClient = class {
|
|
|
4212
149
|
const parsedWrapped = wrappedSchema.parse(json);
|
|
4213
150
|
if (parsedWrapped.success === false) {
|
|
4214
151
|
const error = this.createError({
|
|
4215
|
-
message: parsedWrapped.message || "Request failed",
|
|
152
|
+
message: parsedWrapped.message || parsedWrapped.error || "Request failed",
|
|
4216
153
|
status: parsedWrapped.code || res.status,
|
|
4217
|
-
code: `API_ERROR_${parsedWrapped.code}`
|
|
154
|
+
code: parsedWrapped.code ? `API_ERROR_${parsedWrapped.code}` : "API_ERROR"
|
|
4218
155
|
});
|
|
4219
156
|
this.errorHandler?.(error);
|
|
4220
157
|
throw error;
|
|
@@ -4234,8 +171,7 @@ var ApiClient = class {
|
|
|
4234
171
|
throw error;
|
|
4235
172
|
}
|
|
4236
173
|
const parsed = endpoint.response.parse(responseData);
|
|
4237
|
-
|
|
4238
|
-
return result;
|
|
174
|
+
return this.responseTransform(parsed);
|
|
4239
175
|
};
|
|
4240
176
|
try {
|
|
4241
177
|
const result = await this.executeWithRetry(execute);
|
|
@@ -4248,10 +184,6 @@ var ApiClient = class {
|
|
|
4248
184
|
throw error;
|
|
4249
185
|
}
|
|
4250
186
|
}
|
|
4251
|
-
// =========================================================
|
|
4252
|
-
// 🔁 RETRY ENGINE
|
|
4253
|
-
// =========================================================
|
|
4254
|
-
/** Executes a function with retry logic and configurable backoff strategy. */
|
|
4255
187
|
async executeWithRetry(fn) {
|
|
4256
188
|
if (!this.retryConfig) return fn();
|
|
4257
189
|
const { maxRetries, backoff, retryCondition } = this.retryConfig;
|
|
@@ -4269,7 +201,6 @@ var ApiClient = class {
|
|
|
4269
201
|
}
|
|
4270
202
|
}
|
|
4271
203
|
}
|
|
4272
|
-
/** Calculates retry delay intervals for various backoff strategies. */
|
|
4273
204
|
getBackoffDelay(type, attempt) {
|
|
4274
205
|
const base = 300;
|
|
4275
206
|
switch (type) {
|
|
@@ -4281,48 +212,136 @@ var ApiClient = class {
|
|
|
4281
212
|
return base * Math.pow(2, attempt - 1);
|
|
4282
213
|
}
|
|
4283
214
|
}
|
|
4284
|
-
// =========================================================
|
|
4285
|
-
// 🔧 UTILITIES
|
|
4286
|
-
// =========================================================
|
|
4287
|
-
/** Builds the final URL and request body from the endpoint definition and input payload. */
|
|
4288
215
|
buildUrlAndBody(endpoint, input) {
|
|
216
|
+
const parts = this.extractRequestParts(input);
|
|
4289
217
|
let url = this.config.baseUrl + endpoint.path;
|
|
218
|
+
url = this.applyPathParams(url, parts.path);
|
|
219
|
+
url = this.appendQueryParams(url, parts.query);
|
|
4290
220
|
let body;
|
|
4291
|
-
|
|
221
|
+
const payload = parts.isStructured ? parts.body : input;
|
|
222
|
+
if (endpoint.method !== "GET" && payload !== void 0) {
|
|
4292
223
|
if (endpoint.bodyType === "form-data") {
|
|
4293
|
-
|
|
4294
|
-
|
|
4295
|
-
|
|
224
|
+
if (typeof FormData !== "undefined" && payload instanceof FormData) {
|
|
225
|
+
body = payload;
|
|
226
|
+
} else {
|
|
227
|
+
const form = new FormData();
|
|
228
|
+
if (this.isObjectRecord(payload)) {
|
|
229
|
+
for (const [key, value] of Object.entries(payload)) {
|
|
230
|
+
this.appendFormValue(form, key, value);
|
|
231
|
+
}
|
|
232
|
+
} else if (payload != null) {
|
|
233
|
+
form.append("value", String(payload));
|
|
234
|
+
}
|
|
235
|
+
body = form;
|
|
4296
236
|
}
|
|
4297
|
-
body = form;
|
|
4298
237
|
} else {
|
|
4299
|
-
body = JSON.stringify(
|
|
238
|
+
body = JSON.stringify(payload);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
return { url, body, headers: parts.headers, parts };
|
|
242
|
+
}
|
|
243
|
+
extractRequestParts(input) {
|
|
244
|
+
if (this.isStructuredRequestInput(input)) {
|
|
245
|
+
return {
|
|
246
|
+
path: this.isObjectRecord(input.path) ? input.path : void 0,
|
|
247
|
+
query: this.isObjectRecord(input.query) ? input.query : void 0,
|
|
248
|
+
body: input.body,
|
|
249
|
+
headers: this.normalizeHeaders(input.headers ?? input.header),
|
|
250
|
+
isStructured: true
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
return {
|
|
254
|
+
body: input,
|
|
255
|
+
headers: {},
|
|
256
|
+
isStructured: false
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
isStructuredRequestInput(input) {
|
|
260
|
+
if (!this.isObjectRecord(input)) return false;
|
|
261
|
+
const keys = Object.keys(input);
|
|
262
|
+
if (keys.length === 0) return false;
|
|
263
|
+
return keys.some((key) => REQUEST_PART_KEYS.has(key)) && keys.every((key) => REQUEST_PART_KEYS.has(key));
|
|
264
|
+
}
|
|
265
|
+
isObjectRecord(value) {
|
|
266
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
267
|
+
}
|
|
268
|
+
applyPathParams(url, pathParams) {
|
|
269
|
+
return url.replace(/:([A-Za-z0-9_]+)/g, (_, key) => {
|
|
270
|
+
const value = pathParams?.[key];
|
|
271
|
+
if (value === void 0 || value === null) {
|
|
272
|
+
throw this.createError({
|
|
273
|
+
message: `Missing path param "${key}"`,
|
|
274
|
+
code: "MISSING_PATH_PARAM"
|
|
275
|
+
});
|
|
4300
276
|
}
|
|
277
|
+
return encodeURIComponent(String(value));
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
appendQueryParams(url, query) {
|
|
281
|
+
if (!query) return url;
|
|
282
|
+
const params = new URLSearchParams();
|
|
283
|
+
for (const [key, value] of Object.entries(query)) {
|
|
284
|
+
this.appendQueryValue(params, key, value);
|
|
285
|
+
}
|
|
286
|
+
const queryString = params.toString();
|
|
287
|
+
if (!queryString) return url;
|
|
288
|
+
return `${url}${url.includes("?") ? "&" : "?"}${queryString}`;
|
|
289
|
+
}
|
|
290
|
+
appendQueryValue(params, key, value) {
|
|
291
|
+
if (value === void 0 || value === null) return;
|
|
292
|
+
if (Array.isArray(value)) {
|
|
293
|
+
for (const item of value) this.appendQueryValue(params, key, item);
|
|
294
|
+
return;
|
|
295
|
+
}
|
|
296
|
+
if (value instanceof Date) {
|
|
297
|
+
params.append(key, value.toISOString());
|
|
298
|
+
return;
|
|
299
|
+
}
|
|
300
|
+
if (typeof value === "object") {
|
|
301
|
+
params.append(key, JSON.stringify(value));
|
|
302
|
+
return;
|
|
303
|
+
}
|
|
304
|
+
params.append(key, String(value));
|
|
305
|
+
}
|
|
306
|
+
appendFormValue(form, key, value) {
|
|
307
|
+
if (value === void 0 || value === null) return;
|
|
308
|
+
if (Array.isArray(value)) {
|
|
309
|
+
for (const item of value) this.appendFormValue(form, key, item);
|
|
310
|
+
return;
|
|
4301
311
|
}
|
|
4302
|
-
|
|
312
|
+
if (value instanceof Date) {
|
|
313
|
+
form.append(key, value.toISOString());
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
316
|
+
const isBlob = typeof Blob !== "undefined" && value instanceof Blob;
|
|
317
|
+
if (typeof value === "object" && !isBlob) {
|
|
318
|
+
form.append(key, JSON.stringify(value));
|
|
319
|
+
return;
|
|
320
|
+
}
|
|
321
|
+
form.append(key, value);
|
|
322
|
+
}
|
|
323
|
+
normalizeHeaders(headers) {
|
|
324
|
+
if (!this.isObjectRecord(headers)) return {};
|
|
325
|
+
const normalized = {};
|
|
326
|
+
for (const [key, value] of Object.entries(headers)) {
|
|
327
|
+
if (value === void 0 || value === null) continue;
|
|
328
|
+
normalized[key] = String(value);
|
|
329
|
+
}
|
|
330
|
+
return normalized;
|
|
4303
331
|
}
|
|
4304
|
-
/** Creates and returns a RichError from details. */
|
|
4305
332
|
createError(error) {
|
|
4306
333
|
return new RichError(error);
|
|
4307
334
|
}
|
|
4308
|
-
/**
|
|
4309
|
-
* Converts any thrown error to a standardized RichError instance.
|
|
4310
|
-
* Also flattens Zod validation errors into readable messages.
|
|
4311
|
-
*/
|
|
4312
335
|
normalizeError(err) {
|
|
4313
336
|
if (err instanceof RichError) return err;
|
|
4314
|
-
if (err instanceof
|
|
337
|
+
if (err instanceof z.ZodError) {
|
|
4315
338
|
return this.createError({
|
|
4316
|
-
message: `Validation error: ${err.
|
|
339
|
+
message: `Validation error: ${err.issues.map((e) => e.message).join(", ")}`,
|
|
4317
340
|
code: "VALIDATION_ERROR"
|
|
4318
341
|
});
|
|
4319
342
|
}
|
|
4320
343
|
return this.createError({ message: err.message || "Unknown error" });
|
|
4321
344
|
}
|
|
4322
|
-
/**
|
|
4323
|
-
* Handles mock-mode requests by simulating a delayed network call
|
|
4324
|
-
* and returning validated mock data.
|
|
4325
|
-
*/
|
|
4326
345
|
async handleMockRequest(endpoint) {
|
|
4327
346
|
const delay = Math.floor(
|
|
4328
347
|
Math.random() * (this.mockDelay.max - this.mockDelay.min + 1)
|
|
@@ -4405,20 +424,282 @@ var cacheMiddleware = (options = {}) => {
|
|
|
4405
424
|
};
|
|
4406
425
|
};
|
|
4407
426
|
|
|
427
|
+
// src/middlewares/encryption.ts
|
|
428
|
+
import CryptoJS from "crypto-js";
|
|
429
|
+
import forge from "node-forge";
|
|
430
|
+
function safeJsonParse(value) {
|
|
431
|
+
try {
|
|
432
|
+
return JSON.parse(value);
|
|
433
|
+
} catch {
|
|
434
|
+
return value;
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
function isPlainObject(value) {
|
|
438
|
+
return typeof value === "object" && value !== null && !Array.isArray(value) && Object.prototype.toString.call(value) === "[object Object]";
|
|
439
|
+
}
|
|
440
|
+
function hasKey(value, key) {
|
|
441
|
+
return isPlainObject(value) && Object.prototype.hasOwnProperty.call(value, key);
|
|
442
|
+
}
|
|
443
|
+
function toMiddlewareError(message, error) {
|
|
444
|
+
const err = new Error(
|
|
445
|
+
`${message}: ${error instanceof Error ? error.message : "Unknown error"}`
|
|
446
|
+
);
|
|
447
|
+
err.cause = error;
|
|
448
|
+
return err;
|
|
449
|
+
}
|
|
450
|
+
function encryptWithAES(value, key) {
|
|
451
|
+
return CryptoJS.AES.encrypt(value, key).toString();
|
|
452
|
+
}
|
|
453
|
+
function decryptWithAES(value, key) {
|
|
454
|
+
const bytes = CryptoJS.AES.decrypt(value, key);
|
|
455
|
+
return bytes.toString(CryptoJS.enc.Utf8);
|
|
456
|
+
}
|
|
457
|
+
function encryptWithDES(value, key) {
|
|
458
|
+
return CryptoJS.DES.encrypt(value, key).toString();
|
|
459
|
+
}
|
|
460
|
+
function decryptWithDES(value, key) {
|
|
461
|
+
const bytes = CryptoJS.DES.decrypt(value, key);
|
|
462
|
+
return bytes.toString(CryptoJS.enc.Utf8);
|
|
463
|
+
}
|
|
464
|
+
function encodeWithBase64(value) {
|
|
465
|
+
return CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(value));
|
|
466
|
+
}
|
|
467
|
+
function decodeWithBase64(value) {
|
|
468
|
+
return CryptoJS.enc.Base64.parse(value).toString(CryptoJS.enc.Utf8);
|
|
469
|
+
}
|
|
470
|
+
function encryptWithRSA(value, publicKey) {
|
|
471
|
+
const publicKeyObject = forge.pki.publicKeyFromPem(publicKey);
|
|
472
|
+
const encrypted = publicKeyObject.encrypt(
|
|
473
|
+
forge.util.encodeUtf8(value),
|
|
474
|
+
"RSA-OAEP"
|
|
475
|
+
);
|
|
476
|
+
return forge.util.encode64(encrypted);
|
|
477
|
+
}
|
|
478
|
+
function decryptWithRSA(value, privateKey) {
|
|
479
|
+
const privateKeyObject = forge.pki.privateKeyFromPem(privateKey);
|
|
480
|
+
const decrypted = privateKeyObject.decrypt(
|
|
481
|
+
forge.util.decode64(value),
|
|
482
|
+
"RSA-OAEP"
|
|
483
|
+
);
|
|
484
|
+
return forge.util.decodeUtf8(decrypted);
|
|
485
|
+
}
|
|
486
|
+
async function encryptValue(value, method, keyMaterial, customHandlers) {
|
|
487
|
+
switch (method) {
|
|
488
|
+
case "AES": {
|
|
489
|
+
if (keyMaterial.type !== "symmetric") {
|
|
490
|
+
throw new Error("AES encryption requires symmetric key material.");
|
|
491
|
+
}
|
|
492
|
+
return encryptWithAES(value, keyMaterial.key);
|
|
493
|
+
}
|
|
494
|
+
case "DES": {
|
|
495
|
+
if (keyMaterial.type !== "symmetric") {
|
|
496
|
+
throw new Error("DES encryption requires symmetric key material.");
|
|
497
|
+
}
|
|
498
|
+
return encryptWithDES(value, keyMaterial.key);
|
|
499
|
+
}
|
|
500
|
+
case "Base64": {
|
|
501
|
+
return encodeWithBase64(value);
|
|
502
|
+
}
|
|
503
|
+
case "RSA": {
|
|
504
|
+
if (keyMaterial.type !== "rsa") {
|
|
505
|
+
throw new Error("RSA encryption requires RSA key material.");
|
|
506
|
+
}
|
|
507
|
+
return encryptWithRSA(value, keyMaterial.publicKey);
|
|
508
|
+
}
|
|
509
|
+
case "Custom": {
|
|
510
|
+
if (!customHandlers) {
|
|
511
|
+
throw new Error("Custom encryption requires custom handlers.");
|
|
512
|
+
}
|
|
513
|
+
return await customHandlers.encrypt(value, keyMaterial);
|
|
514
|
+
}
|
|
515
|
+
default: {
|
|
516
|
+
const exhaustiveCheck = method;
|
|
517
|
+
throw new Error(`Unsupported encryption method: ${exhaustiveCheck}`);
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
async function decryptValue(value, method, keyMaterial, customHandlers) {
|
|
522
|
+
switch (method) {
|
|
523
|
+
case "AES": {
|
|
524
|
+
if (keyMaterial.type !== "symmetric") {
|
|
525
|
+
throw new Error("AES decryption requires symmetric key material.");
|
|
526
|
+
}
|
|
527
|
+
return decryptWithAES(value, keyMaterial.key);
|
|
528
|
+
}
|
|
529
|
+
case "DES": {
|
|
530
|
+
if (keyMaterial.type !== "symmetric") {
|
|
531
|
+
throw new Error("DES decryption requires symmetric key material.");
|
|
532
|
+
}
|
|
533
|
+
return decryptWithDES(value, keyMaterial.key);
|
|
534
|
+
}
|
|
535
|
+
case "Base64": {
|
|
536
|
+
return decodeWithBase64(value);
|
|
537
|
+
}
|
|
538
|
+
case "RSA": {
|
|
539
|
+
if (keyMaterial.type !== "rsa") {
|
|
540
|
+
throw new Error("RSA decryption requires RSA key material.");
|
|
541
|
+
}
|
|
542
|
+
return decryptWithRSA(value, keyMaterial.privateKey);
|
|
543
|
+
}
|
|
544
|
+
case "Custom": {
|
|
545
|
+
if (!customHandlers) {
|
|
546
|
+
throw new Error("Custom decryption requires custom handlers.");
|
|
547
|
+
}
|
|
548
|
+
return await customHandlers.decrypt(value, keyMaterial);
|
|
549
|
+
}
|
|
550
|
+
default: {
|
|
551
|
+
const exhaustiveCheck = method;
|
|
552
|
+
throw new Error(`Unsupported decryption method: ${exhaustiveCheck}`);
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
async function processDeep(data, map, defaultMethod, transform) {
|
|
557
|
+
if (data == null || map == null) return data;
|
|
558
|
+
if (typeof map === "string") return await transform(data, map);
|
|
559
|
+
if (typeof map === "boolean") {
|
|
560
|
+
return map ? await transform(data, defaultMethod) : data;
|
|
561
|
+
}
|
|
562
|
+
if (Array.isArray(data)) {
|
|
563
|
+
if (!Array.isArray(map)) {
|
|
564
|
+
return Promise.all(
|
|
565
|
+
data.map((item) => processDeep(item, map, defaultMethod, transform))
|
|
566
|
+
);
|
|
567
|
+
}
|
|
568
|
+
return Promise.all(
|
|
569
|
+
data.map(
|
|
570
|
+
(item, idx) => processDeep(item, map[idx] ?? map[0], defaultMethod, transform)
|
|
571
|
+
)
|
|
572
|
+
);
|
|
573
|
+
}
|
|
574
|
+
if (isPlainObject(data) && isPlainObject(map)) {
|
|
575
|
+
const result = { ...data };
|
|
576
|
+
for (const key of Object.keys(map)) {
|
|
577
|
+
const childMap = map[key];
|
|
578
|
+
if (childMap == null) continue;
|
|
579
|
+
const currentVal = result[key];
|
|
580
|
+
if (currentVal !== void 0) {
|
|
581
|
+
result[key] = await processDeep(
|
|
582
|
+
currentVal,
|
|
583
|
+
childMap,
|
|
584
|
+
defaultMethod,
|
|
585
|
+
transform
|
|
586
|
+
);
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
return result;
|
|
590
|
+
}
|
|
591
|
+
return data;
|
|
592
|
+
}
|
|
593
|
+
function getRequestBodyMap(map) {
|
|
594
|
+
if (hasKey(map, "body")) {
|
|
595
|
+
return map.body;
|
|
596
|
+
}
|
|
597
|
+
return map;
|
|
598
|
+
}
|
|
599
|
+
var encryptionMiddleware = async (ctx, next, options) => {
|
|
600
|
+
if (!options) {
|
|
601
|
+
throw new Error("Encryption middleware options were not provided.");
|
|
602
|
+
}
|
|
603
|
+
const { keyProvider, customHandlers } = options;
|
|
604
|
+
const failClosed = options.failClosed ?? true;
|
|
605
|
+
const encryption = ctx.endpoint.encryption;
|
|
606
|
+
if (!encryption || !encryption.request && !encryption.response) {
|
|
607
|
+
return next();
|
|
608
|
+
}
|
|
609
|
+
const requestMethod = typeof encryption.method === "string" ? encryption.method : encryption.method?.request ?? "AES";
|
|
610
|
+
const responseMethod = typeof encryption.method === "string" ? encryption.method : encryption.method?.response ?? "AES";
|
|
611
|
+
const keyMaterial = await keyProvider();
|
|
612
|
+
if (encryption.request && typeof ctx.init.body === "string" && ctx.init.body.length > 0) {
|
|
613
|
+
try {
|
|
614
|
+
const parsedBody = JSON.parse(ctx.init.body);
|
|
615
|
+
const bodyMap = getRequestBodyMap(encryption.request);
|
|
616
|
+
const encryptedBody = await processDeep(
|
|
617
|
+
parsedBody,
|
|
618
|
+
bodyMap,
|
|
619
|
+
requestMethod,
|
|
620
|
+
async (value, method) => {
|
|
621
|
+
const serialized = typeof value === "string" ? value : JSON.stringify(value);
|
|
622
|
+
return encryptValue(serialized, method, keyMaterial, customHandlers);
|
|
623
|
+
}
|
|
624
|
+
);
|
|
625
|
+
ctx.init.body = JSON.stringify(encryptedBody);
|
|
626
|
+
if (ctx.request) {
|
|
627
|
+
ctx.request.body = encryptedBody;
|
|
628
|
+
}
|
|
629
|
+
} catch (error) {
|
|
630
|
+
if (failClosed) {
|
|
631
|
+
throw toMiddlewareError(
|
|
632
|
+
"Encryption middleware request encryption failed",
|
|
633
|
+
error
|
|
634
|
+
);
|
|
635
|
+
}
|
|
636
|
+
console.error("Encryption middleware request encryption failed.", error);
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
const response = await next();
|
|
640
|
+
if (!encryption.response) {
|
|
641
|
+
return response;
|
|
642
|
+
}
|
|
643
|
+
try {
|
|
644
|
+
const text = await response.clone().text();
|
|
645
|
+
if (!text) return response;
|
|
646
|
+
const parsedResponse = JSON.parse(text);
|
|
647
|
+
const decryptedPayload = await processDeep(
|
|
648
|
+
parsedResponse,
|
|
649
|
+
encryption.response,
|
|
650
|
+
responseMethod,
|
|
651
|
+
async (value, method) => {
|
|
652
|
+
if (typeof value !== "string") return value;
|
|
653
|
+
const decrypted = await decryptValue(
|
|
654
|
+
value,
|
|
655
|
+
method,
|
|
656
|
+
keyMaterial,
|
|
657
|
+
customHandlers
|
|
658
|
+
);
|
|
659
|
+
return safeJsonParse(decrypted);
|
|
660
|
+
}
|
|
661
|
+
);
|
|
662
|
+
return new Response(JSON.stringify(decryptedPayload), {
|
|
663
|
+
status: response.status,
|
|
664
|
+
statusText: response.statusText,
|
|
665
|
+
headers: response.headers
|
|
666
|
+
});
|
|
667
|
+
} catch (error) {
|
|
668
|
+
if (failClosed) {
|
|
669
|
+
throw toMiddlewareError(
|
|
670
|
+
"Encryption middleware response decryption failed",
|
|
671
|
+
error
|
|
672
|
+
);
|
|
673
|
+
}
|
|
674
|
+
console.error("Encryption middleware response decryption failed.", error);
|
|
675
|
+
return response;
|
|
676
|
+
}
|
|
677
|
+
};
|
|
678
|
+
|
|
4408
679
|
// src/utils/make-request-schema.ts
|
|
4409
|
-
|
|
4410
|
-
|
|
4411
|
-
|
|
4412
|
-
|
|
4413
|
-
|
|
4414
|
-
|
|
680
|
+
import { z as z2 } from "zod";
|
|
681
|
+
var makeRequestSchema = () => (defs = {}) => {
|
|
682
|
+
const pathSchema = defs.path ?? z2.object({});
|
|
683
|
+
const querySchema = defs.query ?? z2.object({});
|
|
684
|
+
const rawBodySchema = defs.body ?? z2.undefined();
|
|
685
|
+
const bodySchema = defs.body ? rawBodySchema : rawBodySchema.optional();
|
|
686
|
+
const headersSchema = defs.headers ?? z2.record(z2.string(), z2.string()).optional();
|
|
687
|
+
return z2.object({
|
|
688
|
+
path: pathSchema.optional(),
|
|
689
|
+
query: querySchema.optional(),
|
|
690
|
+
body: bodySchema,
|
|
691
|
+
headers: headersSchema
|
|
692
|
+
});
|
|
693
|
+
};
|
|
4415
694
|
export {
|
|
4416
695
|
ApiClient,
|
|
4417
696
|
RichError,
|
|
4418
697
|
authMiddleware,
|
|
4419
698
|
cacheMiddleware,
|
|
699
|
+
encryptionMiddleware,
|
|
4420
700
|
loggingMiddleware,
|
|
4421
701
|
makeRequestSchema,
|
|
702
|
+
processDeep,
|
|
4422
703
|
retryMiddleware
|
|
4423
704
|
};
|
|
4424
705
|
//# sourceMappingURL=index.mjs.map
|