zod 3.14.4 → 3.15.1

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.
@@ -0,0 +1,2983 @@
1
+ (function (global, factory) {
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
3
+ typeof define === 'function' && define.amd ? define(['exports'], factory) :
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.Zod = {}));
5
+ })(this, (function (exports) { 'use strict';
6
+
7
+ var util;
8
+ (function (util) {
9
+ function assertNever(_x) {
10
+ throw new Error();
11
+ }
12
+ util.assertNever = assertNever;
13
+ util.arrayToEnum = (items) => {
14
+ const obj = {};
15
+ for (const item of items) {
16
+ obj[item] = item;
17
+ }
18
+ return obj;
19
+ };
20
+ util.getValidEnumValues = (obj) => {
21
+ const validKeys = util.objectKeys(obj).filter((k) => typeof obj[obj[k]] !== "number");
22
+ const filtered = {};
23
+ for (const k of validKeys) {
24
+ filtered[k] = obj[k];
25
+ }
26
+ return util.objectValues(filtered);
27
+ };
28
+ util.objectValues = (obj) => {
29
+ return util.objectKeys(obj).map(function (e) {
30
+ return obj[e];
31
+ });
32
+ };
33
+ util.objectKeys = typeof Object.keys === "function" // eslint-disable-line ban/ban
34
+ ? (obj) => Object.keys(obj) // eslint-disable-line ban/ban
35
+ : (object) => {
36
+ const keys = [];
37
+ for (const key in object) {
38
+ if (Object.prototype.hasOwnProperty.call(object, key)) {
39
+ keys.push(key);
40
+ }
41
+ }
42
+ return keys;
43
+ };
44
+ util.find = (arr, checker) => {
45
+ for (const item of arr) {
46
+ if (checker(item))
47
+ return item;
48
+ }
49
+ return undefined;
50
+ };
51
+ util.isInteger = typeof Number.isInteger === "function"
52
+ ? (val) => Number.isInteger(val) // eslint-disable-line ban/ban
53
+ : (val) => typeof val === "number" && isFinite(val) && Math.floor(val) === val;
54
+ })(util || (util = {}));
55
+
56
+ const ZodIssueCode = util.arrayToEnum([
57
+ "invalid_type",
58
+ "invalid_literal",
59
+ "custom",
60
+ "invalid_union",
61
+ "invalid_union_discriminator",
62
+ "invalid_enum_value",
63
+ "unrecognized_keys",
64
+ "invalid_arguments",
65
+ "invalid_return_type",
66
+ "invalid_date",
67
+ "invalid_string",
68
+ "too_small",
69
+ "too_big",
70
+ "invalid_intersection_types",
71
+ "not_multiple_of",
72
+ ]);
73
+ const quotelessJson = (obj) => {
74
+ const json = JSON.stringify(obj, null, 2);
75
+ return json.replace(/"([^"]+)":/g, "$1:");
76
+ };
77
+ class ZodError extends Error {
78
+ constructor(issues) {
79
+ super();
80
+ this.issues = [];
81
+ this.addIssue = (sub) => {
82
+ this.issues = [...this.issues, sub];
83
+ };
84
+ this.addIssues = (subs = []) => {
85
+ this.issues = [...this.issues, ...subs];
86
+ };
87
+ const actualProto = new.target.prototype;
88
+ if (Object.setPrototypeOf) {
89
+ // eslint-disable-next-line ban/ban
90
+ Object.setPrototypeOf(this, actualProto);
91
+ }
92
+ else {
93
+ this.__proto__ = actualProto;
94
+ }
95
+ this.name = "ZodError";
96
+ this.issues = issues;
97
+ }
98
+ get errors() {
99
+ return this.issues;
100
+ }
101
+ format(_mapper) {
102
+ const mapper = _mapper ||
103
+ function (issue) {
104
+ return issue.message;
105
+ };
106
+ const fieldErrors = { _errors: [] };
107
+ const processError = (error) => {
108
+ for (const issue of error.issues) {
109
+ if (issue.code === "invalid_union") {
110
+ issue.unionErrors.map(processError);
111
+ }
112
+ else if (issue.code === "invalid_return_type") {
113
+ processError(issue.returnTypeError);
114
+ }
115
+ else if (issue.code === "invalid_arguments") {
116
+ processError(issue.argumentsError);
117
+ }
118
+ else if (issue.path.length === 0) {
119
+ fieldErrors._errors.push(mapper(issue));
120
+ }
121
+ else {
122
+ let curr = fieldErrors;
123
+ let i = 0;
124
+ while (i < issue.path.length) {
125
+ const el = issue.path[i];
126
+ const terminal = i === issue.path.length - 1;
127
+ if (!terminal) {
128
+ curr[el] = curr[el] || { _errors: [] };
129
+ // if (typeof el === "string") {
130
+ // curr[el] = curr[el] || { _errors: [] };
131
+ // } else if (typeof el === "number") {
132
+ // const errorArray: any = [];
133
+ // errorArray._errors = [];
134
+ // curr[el] = curr[el] || errorArray;
135
+ // }
136
+ }
137
+ else {
138
+ curr[el] = curr[el] || { _errors: [] };
139
+ curr[el]._errors.push(mapper(issue));
140
+ }
141
+ curr = curr[el];
142
+ i++;
143
+ }
144
+ }
145
+ }
146
+ };
147
+ processError(this);
148
+ return fieldErrors;
149
+ }
150
+ toString() {
151
+ return this.message;
152
+ }
153
+ get message() {
154
+ return JSON.stringify(this.issues, null, 2);
155
+ }
156
+ get isEmpty() {
157
+ return this.issues.length === 0;
158
+ }
159
+ flatten(mapper = (issue) => issue.message) {
160
+ const fieldErrors = {};
161
+ const formErrors = [];
162
+ for (const sub of this.issues) {
163
+ if (sub.path.length > 0) {
164
+ fieldErrors[sub.path[0]] = fieldErrors[sub.path[0]] || [];
165
+ fieldErrors[sub.path[0]].push(mapper(sub));
166
+ }
167
+ else {
168
+ formErrors.push(mapper(sub));
169
+ }
170
+ }
171
+ return { formErrors, fieldErrors };
172
+ }
173
+ get formErrors() {
174
+ return this.flatten();
175
+ }
176
+ }
177
+ ZodError.create = (issues) => {
178
+ const error = new ZodError(issues);
179
+ return error;
180
+ };
181
+ const defaultErrorMap = (issue, _ctx) => {
182
+ let message;
183
+ switch (issue.code) {
184
+ case ZodIssueCode.invalid_type:
185
+ if (issue.received === "undefined") {
186
+ message = "Required";
187
+ }
188
+ else {
189
+ message = `Expected ${issue.expected}, received ${issue.received}`;
190
+ }
191
+ break;
192
+ case ZodIssueCode.invalid_literal:
193
+ message = `Invalid literal value, expected ${JSON.stringify(issue.expected)}`;
194
+ break;
195
+ case ZodIssueCode.unrecognized_keys:
196
+ message = `Unrecognized key(s) in object: ${issue.keys
197
+ .map((k) => `'${k}'`)
198
+ .join(", ")}`;
199
+ break;
200
+ case ZodIssueCode.invalid_union:
201
+ message = `Invalid input`;
202
+ break;
203
+ case ZodIssueCode.invalid_union_discriminator:
204
+ message = `Invalid discriminator value. Expected ${issue.options
205
+ .map((val) => (typeof val === "string" ? `'${val}'` : val))
206
+ .join(" | ")}`;
207
+ break;
208
+ case ZodIssueCode.invalid_enum_value:
209
+ message = `Invalid enum value. Expected ${issue.options
210
+ .map((val) => (typeof val === "string" ? `'${val}'` : val))
211
+ .join(" | ")}`;
212
+ break;
213
+ case ZodIssueCode.invalid_arguments:
214
+ message = `Invalid function arguments`;
215
+ break;
216
+ case ZodIssueCode.invalid_return_type:
217
+ message = `Invalid function return type`;
218
+ break;
219
+ case ZodIssueCode.invalid_date:
220
+ message = `Invalid date`;
221
+ break;
222
+ case ZodIssueCode.invalid_string:
223
+ if (issue.validation !== "regex")
224
+ message = `Invalid ${issue.validation}`;
225
+ else
226
+ message = "Invalid";
227
+ break;
228
+ case ZodIssueCode.too_small:
229
+ if (issue.type === "array")
230
+ message = `Array must contain ${issue.inclusive ? `at least` : `more than`} ${issue.minimum} element(s)`;
231
+ else if (issue.type === "string")
232
+ message = `String must contain ${issue.inclusive ? `at least` : `over`} ${issue.minimum} character(s)`;
233
+ else if (issue.type === "number")
234
+ message = `Number must be greater than ${issue.inclusive ? `or equal to ` : ``}${issue.minimum}`;
235
+ else
236
+ message = "Invalid input";
237
+ break;
238
+ case ZodIssueCode.too_big:
239
+ if (issue.type === "array")
240
+ message = `Array must contain ${issue.inclusive ? `at most` : `less than`} ${issue.maximum} element(s)`;
241
+ else if (issue.type === "string")
242
+ message = `String must contain ${issue.inclusive ? `at most` : `under`} ${issue.maximum} character(s)`;
243
+ else if (issue.type === "number")
244
+ message = `Number must be less than ${issue.inclusive ? `or equal to ` : ``}${issue.maximum}`;
245
+ else
246
+ message = "Invalid input";
247
+ break;
248
+ case ZodIssueCode.custom:
249
+ message = `Invalid input`;
250
+ break;
251
+ case ZodIssueCode.invalid_intersection_types:
252
+ message = `Intersection results could not be merged`;
253
+ break;
254
+ case ZodIssueCode.not_multiple_of:
255
+ message = `Number must be a multiple of ${issue.multipleOf}`;
256
+ break;
257
+ default:
258
+ message = _ctx.defaultError;
259
+ util.assertNever(issue);
260
+ }
261
+ return { message };
262
+ };
263
+ exports.overrideErrorMap = defaultErrorMap;
264
+ const setErrorMap = (map) => {
265
+ exports.overrideErrorMap = map;
266
+ };
267
+
268
+ const ZodParsedType = util.arrayToEnum([
269
+ "string",
270
+ "nan",
271
+ "number",
272
+ "integer",
273
+ "float",
274
+ "boolean",
275
+ "date",
276
+ "bigint",
277
+ "symbol",
278
+ "function",
279
+ "undefined",
280
+ "null",
281
+ "array",
282
+ "object",
283
+ "unknown",
284
+ "promise",
285
+ "void",
286
+ "never",
287
+ "map",
288
+ "set",
289
+ ]);
290
+ const getParsedType = (data) => {
291
+ const t = typeof data;
292
+ switch (t) {
293
+ case "undefined":
294
+ return ZodParsedType.undefined;
295
+ case "string":
296
+ return ZodParsedType.string;
297
+ case "number":
298
+ return isNaN(data) ? ZodParsedType.nan : ZodParsedType.number;
299
+ case "boolean":
300
+ return ZodParsedType.boolean;
301
+ case "function":
302
+ return ZodParsedType.function;
303
+ case "bigint":
304
+ return ZodParsedType.bigint;
305
+ case "object":
306
+ if (Array.isArray(data)) {
307
+ return ZodParsedType.array;
308
+ }
309
+ if (data === null) {
310
+ return ZodParsedType.null;
311
+ }
312
+ if (data.then &&
313
+ typeof data.then === "function" &&
314
+ data.catch &&
315
+ typeof data.catch === "function") {
316
+ return ZodParsedType.promise;
317
+ }
318
+ if (typeof Map !== "undefined" && data instanceof Map) {
319
+ return ZodParsedType.map;
320
+ }
321
+ if (typeof Set !== "undefined" && data instanceof Set) {
322
+ return ZodParsedType.set;
323
+ }
324
+ if (typeof Date !== "undefined" && data instanceof Date) {
325
+ return ZodParsedType.date;
326
+ }
327
+ return ZodParsedType.object;
328
+ default:
329
+ return ZodParsedType.unknown;
330
+ }
331
+ };
332
+ const makeIssue = (params) => {
333
+ const { data, path, errorMaps, issueData } = params;
334
+ const fullPath = [...path, ...(issueData.path || [])];
335
+ const fullIssue = {
336
+ ...issueData,
337
+ path: fullPath,
338
+ };
339
+ let errorMessage = "";
340
+ const maps = errorMaps
341
+ .filter((m) => !!m)
342
+ .slice()
343
+ .reverse();
344
+ for (const map of maps) {
345
+ errorMessage = map(fullIssue, { data, defaultError: errorMessage }).message;
346
+ }
347
+ return {
348
+ ...issueData,
349
+ path: fullPath,
350
+ message: issueData.message || errorMessage,
351
+ };
352
+ };
353
+ const EMPTY_PATH = [];
354
+ function addIssueToContext(ctx, issueData) {
355
+ const issue = makeIssue({
356
+ issueData: issueData,
357
+ data: ctx.data,
358
+ path: ctx.path,
359
+ errorMaps: [
360
+ ctx.common.contextualErrorMap,
361
+ ctx.schemaErrorMap,
362
+ exports.overrideErrorMap,
363
+ defaultErrorMap, // then global default map
364
+ ].filter((x) => !!x),
365
+ });
366
+ ctx.common.issues.push(issue);
367
+ }
368
+ class ParseStatus {
369
+ constructor() {
370
+ this.value = "valid";
371
+ }
372
+ dirty() {
373
+ if (this.value === "valid")
374
+ this.value = "dirty";
375
+ }
376
+ abort() {
377
+ if (this.value !== "aborted")
378
+ this.value = "aborted";
379
+ }
380
+ static mergeArray(status, results) {
381
+ const arrayValue = [];
382
+ for (const s of results) {
383
+ if (s.status === "aborted")
384
+ return INVALID;
385
+ if (s.status === "dirty")
386
+ status.dirty();
387
+ arrayValue.push(s.value);
388
+ }
389
+ return { status: status.value, value: arrayValue };
390
+ }
391
+ static async mergeObjectAsync(status, pairs) {
392
+ const syncPairs = [];
393
+ for (const pair of pairs) {
394
+ syncPairs.push({
395
+ key: await pair.key,
396
+ value: await pair.value,
397
+ });
398
+ }
399
+ return ParseStatus.mergeObjectSync(status, syncPairs);
400
+ }
401
+ static mergeObjectSync(status, pairs) {
402
+ const finalObject = {};
403
+ for (const pair of pairs) {
404
+ const { key, value } = pair;
405
+ if (key.status === "aborted")
406
+ return INVALID;
407
+ if (value.status === "aborted")
408
+ return INVALID;
409
+ if (key.status === "dirty")
410
+ status.dirty();
411
+ if (value.status === "dirty")
412
+ status.dirty();
413
+ if (typeof value.value !== "undefined" || pair.alwaysSet) {
414
+ finalObject[key.value] = value.value;
415
+ }
416
+ }
417
+ return { status: status.value, value: finalObject };
418
+ }
419
+ }
420
+ const INVALID = Object.freeze({
421
+ status: "aborted",
422
+ });
423
+ const DIRTY = (value) => ({ status: "dirty", value });
424
+ const OK = (value) => ({ status: "valid", value });
425
+ const isAborted = (x) => x.status === "aborted";
426
+ const isDirty = (x) => x.status === "dirty";
427
+ const isValid = (x) => x.status === "valid";
428
+ const isAsync = (x) => typeof Promise !== undefined && x instanceof Promise;
429
+
430
+ var errorUtil;
431
+ (function (errorUtil) {
432
+ errorUtil.errToObj = (message) => typeof message === "string" ? { message } : message || {};
433
+ errorUtil.toString = (message) => typeof message === "string" ? message : message === null || message === void 0 ? void 0 : message.message;
434
+ })(errorUtil || (errorUtil = {}));
435
+
436
+ class ParseInputLazyPath {
437
+ constructor(parent, value, path, key) {
438
+ this.parent = parent;
439
+ this.data = value;
440
+ this._path = path;
441
+ this._key = key;
442
+ }
443
+ get path() {
444
+ return this._path.concat(this._key);
445
+ }
446
+ }
447
+ const handleResult = (ctx, result) => {
448
+ if (isValid(result)) {
449
+ return { success: true, data: result.value };
450
+ }
451
+ else {
452
+ if (!ctx.common.issues.length) {
453
+ throw new Error("Validation failed but no issues detected.");
454
+ }
455
+ const error = new ZodError(ctx.common.issues);
456
+ return { success: false, error };
457
+ }
458
+ };
459
+ function processCreateParams(params) {
460
+ if (!params)
461
+ return {};
462
+ const { errorMap, invalid_type_error, required_error, description } = params;
463
+ if (errorMap && (invalid_type_error || required_error)) {
464
+ throw new Error(`Can't use "invalid" or "required" in conjunction with custom error map.`);
465
+ }
466
+ if (errorMap)
467
+ return { errorMap: errorMap, description };
468
+ const customMap = (iss, ctx) => {
469
+ if (iss.code !== "invalid_type")
470
+ return { message: ctx.defaultError };
471
+ if (typeof ctx.data === "undefined" && required_error)
472
+ return { message: required_error };
473
+ if (params.invalid_type_error)
474
+ return { message: params.invalid_type_error };
475
+ return { message: ctx.defaultError };
476
+ };
477
+ return { errorMap: customMap, description };
478
+ }
479
+ class ZodType {
480
+ constructor(def) {
481
+ /** Alias of safeParseAsync */
482
+ this.spa = this.safeParseAsync;
483
+ this.superRefine = this._refinement;
484
+ this._def = def;
485
+ this.parse = this.parse.bind(this);
486
+ this.safeParse = this.safeParse.bind(this);
487
+ this.parseAsync = this.parseAsync.bind(this);
488
+ this.safeParseAsync = this.safeParseAsync.bind(this);
489
+ this.spa = this.spa.bind(this);
490
+ this.refine = this.refine.bind(this);
491
+ this.refinement = this.refinement.bind(this);
492
+ this.superRefine = this.superRefine.bind(this);
493
+ this.optional = this.optional.bind(this);
494
+ this.nullable = this.nullable.bind(this);
495
+ this.nullish = this.nullish.bind(this);
496
+ this.array = this.array.bind(this);
497
+ this.promise = this.promise.bind(this);
498
+ this.or = this.or.bind(this);
499
+ this.and = this.and.bind(this);
500
+ this.transform = this.transform.bind(this);
501
+ this.default = this.default.bind(this);
502
+ this.describe = this.describe.bind(this);
503
+ this.isNullable = this.isNullable.bind(this);
504
+ this.isOptional = this.isOptional.bind(this);
505
+ }
506
+ get description() {
507
+ return this._def.description;
508
+ }
509
+ _getType(input) {
510
+ return getParsedType(input.data);
511
+ }
512
+ _getOrReturnCtx(input, ctx) {
513
+ return (ctx || {
514
+ common: input.parent.common,
515
+ data: input.data,
516
+ parsedType: getParsedType(input.data),
517
+ schemaErrorMap: this._def.errorMap,
518
+ path: input.path,
519
+ parent: input.parent,
520
+ });
521
+ }
522
+ _processInputParams(input) {
523
+ return {
524
+ status: new ParseStatus(),
525
+ ctx: {
526
+ common: input.parent.common,
527
+ data: input.data,
528
+ parsedType: getParsedType(input.data),
529
+ schemaErrorMap: this._def.errorMap,
530
+ path: input.path,
531
+ parent: input.parent,
532
+ },
533
+ };
534
+ }
535
+ _parseSync(input) {
536
+ const result = this._parse(input);
537
+ if (isAsync(result)) {
538
+ throw new Error("Synchronous parse encountered promise.");
539
+ }
540
+ return result;
541
+ }
542
+ _parseAsync(input) {
543
+ const result = this._parse(input);
544
+ return Promise.resolve(result);
545
+ }
546
+ parse(data, params) {
547
+ const result = this.safeParse(data, params);
548
+ if (result.success)
549
+ return result.data;
550
+ throw result.error;
551
+ }
552
+ safeParse(data, params) {
553
+ var _a;
554
+ const ctx = {
555
+ common: {
556
+ issues: [],
557
+ async: (_a = params === null || params === void 0 ? void 0 : params.async) !== null && _a !== void 0 ? _a : false,
558
+ contextualErrorMap: params === null || params === void 0 ? void 0 : params.errorMap,
559
+ },
560
+ path: (params === null || params === void 0 ? void 0 : params.path) || [],
561
+ schemaErrorMap: this._def.errorMap,
562
+ parent: null,
563
+ data,
564
+ parsedType: getParsedType(data),
565
+ };
566
+ const result = this._parseSync({ data, path: ctx.path, parent: ctx });
567
+ return handleResult(ctx, result);
568
+ }
569
+ async parseAsync(data, params) {
570
+ const result = await this.safeParseAsync(data, params);
571
+ if (result.success)
572
+ return result.data;
573
+ throw result.error;
574
+ }
575
+ async safeParseAsync(data, params) {
576
+ const ctx = {
577
+ common: {
578
+ issues: [],
579
+ contextualErrorMap: params === null || params === void 0 ? void 0 : params.errorMap,
580
+ async: true,
581
+ },
582
+ path: (params === null || params === void 0 ? void 0 : params.path) || [],
583
+ schemaErrorMap: this._def.errorMap,
584
+ parent: null,
585
+ data,
586
+ parsedType: getParsedType(data),
587
+ };
588
+ const maybeAsyncResult = this._parse({ data, path: [], parent: ctx });
589
+ const result = await (isAsync(maybeAsyncResult)
590
+ ? maybeAsyncResult
591
+ : Promise.resolve(maybeAsyncResult));
592
+ return handleResult(ctx, result);
593
+ }
594
+ refine(check, message) {
595
+ const getIssueProperties = (val) => {
596
+ if (typeof message === "string" || typeof message === "undefined") {
597
+ return { message };
598
+ }
599
+ else if (typeof message === "function") {
600
+ return message(val);
601
+ }
602
+ else {
603
+ return message;
604
+ }
605
+ };
606
+ return this._refinement((val, ctx) => {
607
+ const result = check(val);
608
+ const setError = () => ctx.addIssue({
609
+ code: ZodIssueCode.custom,
610
+ ...getIssueProperties(val),
611
+ });
612
+ if (typeof Promise !== "undefined" && result instanceof Promise) {
613
+ return result.then((data) => {
614
+ if (!data) {
615
+ setError();
616
+ return false;
617
+ }
618
+ else {
619
+ return true;
620
+ }
621
+ });
622
+ }
623
+ if (!result) {
624
+ setError();
625
+ return false;
626
+ }
627
+ else {
628
+ return true;
629
+ }
630
+ });
631
+ }
632
+ refinement(check, refinementData) {
633
+ return this._refinement((val, ctx) => {
634
+ if (!check(val)) {
635
+ ctx.addIssue(typeof refinementData === "function"
636
+ ? refinementData(val, ctx)
637
+ : refinementData);
638
+ return false;
639
+ }
640
+ else {
641
+ return true;
642
+ }
643
+ });
644
+ }
645
+ _refinement(refinement) {
646
+ return new ZodEffects({
647
+ schema: this,
648
+ typeName: exports.ZodFirstPartyTypeKind.ZodEffects,
649
+ effect: { type: "refinement", refinement },
650
+ });
651
+ }
652
+ optional() {
653
+ return ZodOptional.create(this);
654
+ }
655
+ nullable() {
656
+ return ZodNullable.create(this);
657
+ }
658
+ nullish() {
659
+ return this.optional().nullable();
660
+ }
661
+ array() {
662
+ return ZodArray.create(this);
663
+ }
664
+ promise() {
665
+ return ZodPromise.create(this);
666
+ }
667
+ or(option) {
668
+ return ZodUnion.create([this, option]);
669
+ }
670
+ and(incoming) {
671
+ return ZodIntersection.create(this, incoming);
672
+ }
673
+ transform(transform) {
674
+ return new ZodEffects({
675
+ schema: this,
676
+ typeName: exports.ZodFirstPartyTypeKind.ZodEffects,
677
+ effect: { type: "transform", transform },
678
+ });
679
+ }
680
+ default(def) {
681
+ const defaultValueFunc = typeof def === "function" ? def : () => def;
682
+ return new ZodDefault({
683
+ innerType: this,
684
+ defaultValue: defaultValueFunc,
685
+ typeName: exports.ZodFirstPartyTypeKind.ZodDefault,
686
+ });
687
+ }
688
+ describe(description) {
689
+ const This = this.constructor;
690
+ return new This({
691
+ ...this._def,
692
+ description,
693
+ });
694
+ }
695
+ isOptional() {
696
+ return this.safeParse(undefined).success;
697
+ }
698
+ isNullable() {
699
+ return this.safeParse(null).success;
700
+ }
701
+ }
702
+ const cuidRegex = /^c[^\s-]{8,}$/i;
703
+ const uuidRegex = /^([a-f0-9]{8}-[a-f0-9]{4}-[1-5][a-f0-9]{3}-[a-f0-9]{4}-[a-f0-9]{12}|00000000-0000-0000-0000-000000000000)$/i;
704
+ // from https://stackoverflow.com/a/46181/1550155
705
+ // old version: too slow, didn't support unicode
706
+ // const emailRegex = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$/i;
707
+ // eslint-disable-next-line
708
+ const emailRegex = /^(([^<>()[\]\.,;:\s@\"]+(\.[^<>()[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i;
709
+ class ZodString extends ZodType {
710
+ constructor() {
711
+ super(...arguments);
712
+ this._regex = (regex, validation, message) => this.refinement((data) => regex.test(data), {
713
+ validation,
714
+ code: ZodIssueCode.invalid_string,
715
+ ...errorUtil.errToObj(message),
716
+ });
717
+ /**
718
+ * Deprecated.
719
+ * Use z.string().min(1) instead.
720
+ */
721
+ this.nonempty = (message) => this.min(1, errorUtil.errToObj(message));
722
+ }
723
+ _parse(input) {
724
+ const parsedType = this._getType(input);
725
+ if (parsedType !== ZodParsedType.string) {
726
+ const ctx = this._getOrReturnCtx(input);
727
+ addIssueToContext(ctx, {
728
+ code: ZodIssueCode.invalid_type,
729
+ expected: ZodParsedType.string,
730
+ received: ctx.parsedType,
731
+ }
732
+ //
733
+ );
734
+ return INVALID;
735
+ }
736
+ const status = new ParseStatus();
737
+ let ctx = undefined;
738
+ for (const check of this._def.checks) {
739
+ if (check.kind === "min") {
740
+ if (input.data.length < check.value) {
741
+ ctx = this._getOrReturnCtx(input, ctx);
742
+ addIssueToContext(ctx, {
743
+ code: ZodIssueCode.too_small,
744
+ minimum: check.value,
745
+ type: "string",
746
+ inclusive: true,
747
+ message: check.message,
748
+ });
749
+ status.dirty();
750
+ }
751
+ }
752
+ else if (check.kind === "max") {
753
+ if (input.data.length > check.value) {
754
+ ctx = this._getOrReturnCtx(input, ctx);
755
+ addIssueToContext(ctx, {
756
+ code: ZodIssueCode.too_big,
757
+ maximum: check.value,
758
+ type: "string",
759
+ inclusive: true,
760
+ message: check.message,
761
+ });
762
+ status.dirty();
763
+ }
764
+ }
765
+ else if (check.kind === "email") {
766
+ if (!emailRegex.test(input.data)) {
767
+ ctx = this._getOrReturnCtx(input, ctx);
768
+ addIssueToContext(ctx, {
769
+ validation: "email",
770
+ code: ZodIssueCode.invalid_string,
771
+ message: check.message,
772
+ });
773
+ status.dirty();
774
+ }
775
+ }
776
+ else if (check.kind === "uuid") {
777
+ if (!uuidRegex.test(input.data)) {
778
+ ctx = this._getOrReturnCtx(input, ctx);
779
+ addIssueToContext(ctx, {
780
+ validation: "uuid",
781
+ code: ZodIssueCode.invalid_string,
782
+ message: check.message,
783
+ });
784
+ status.dirty();
785
+ }
786
+ }
787
+ else if (check.kind === "cuid") {
788
+ if (!cuidRegex.test(input.data)) {
789
+ ctx = this._getOrReturnCtx(input, ctx);
790
+ addIssueToContext(ctx, {
791
+ validation: "cuid",
792
+ code: ZodIssueCode.invalid_string,
793
+ message: check.message,
794
+ });
795
+ status.dirty();
796
+ }
797
+ }
798
+ else if (check.kind === "url") {
799
+ try {
800
+ new URL(input.data);
801
+ }
802
+ catch (_a) {
803
+ ctx = this._getOrReturnCtx(input, ctx);
804
+ addIssueToContext(ctx, {
805
+ validation: "url",
806
+ code: ZodIssueCode.invalid_string,
807
+ message: check.message,
808
+ });
809
+ status.dirty();
810
+ }
811
+ }
812
+ else if (check.kind === "regex") {
813
+ check.regex.lastIndex = 0;
814
+ const testResult = check.regex.test(input.data);
815
+ if (!testResult) {
816
+ ctx = this._getOrReturnCtx(input, ctx);
817
+ addIssueToContext(ctx, {
818
+ validation: "regex",
819
+ code: ZodIssueCode.invalid_string,
820
+ message: check.message,
821
+ });
822
+ status.dirty();
823
+ }
824
+ }
825
+ }
826
+ return { status: status.value, value: input.data };
827
+ }
828
+ _addCheck(check) {
829
+ return new ZodString({
830
+ ...this._def,
831
+ checks: [...this._def.checks, check],
832
+ });
833
+ }
834
+ email(message) {
835
+ return this._addCheck({ kind: "email", ...errorUtil.errToObj(message) });
836
+ }
837
+ url(message) {
838
+ return this._addCheck({ kind: "url", ...errorUtil.errToObj(message) });
839
+ }
840
+ uuid(message) {
841
+ return this._addCheck({ kind: "uuid", ...errorUtil.errToObj(message) });
842
+ }
843
+ cuid(message) {
844
+ return this._addCheck({ kind: "cuid", ...errorUtil.errToObj(message) });
845
+ }
846
+ regex(regex, message) {
847
+ return this._addCheck({
848
+ kind: "regex",
849
+ regex: regex,
850
+ ...errorUtil.errToObj(message),
851
+ });
852
+ }
853
+ min(minLength, message) {
854
+ return this._addCheck({
855
+ kind: "min",
856
+ value: minLength,
857
+ ...errorUtil.errToObj(message),
858
+ });
859
+ }
860
+ max(maxLength, message) {
861
+ return this._addCheck({
862
+ kind: "max",
863
+ value: maxLength,
864
+ ...errorUtil.errToObj(message),
865
+ });
866
+ }
867
+ length(len, message) {
868
+ return this.min(len, message).max(len, message);
869
+ }
870
+ get isEmail() {
871
+ return !!this._def.checks.find((ch) => ch.kind === "email");
872
+ }
873
+ get isURL() {
874
+ return !!this._def.checks.find((ch) => ch.kind === "url");
875
+ }
876
+ get isUUID() {
877
+ return !!this._def.checks.find((ch) => ch.kind === "uuid");
878
+ }
879
+ get isCUID() {
880
+ return !!this._def.checks.find((ch) => ch.kind === "cuid");
881
+ }
882
+ get minLength() {
883
+ let min = -Infinity;
884
+ this._def.checks.map((ch) => {
885
+ if (ch.kind === "min") {
886
+ if (min === null || ch.value > min) {
887
+ min = ch.value;
888
+ }
889
+ }
890
+ });
891
+ return min;
892
+ }
893
+ get maxLength() {
894
+ let max = null;
895
+ this._def.checks.map((ch) => {
896
+ if (ch.kind === "max") {
897
+ if (max === null || ch.value < max) {
898
+ max = ch.value;
899
+ }
900
+ }
901
+ });
902
+ return max;
903
+ }
904
+ }
905
+ ZodString.create = (params) => {
906
+ return new ZodString({
907
+ checks: [],
908
+ typeName: exports.ZodFirstPartyTypeKind.ZodString,
909
+ ...processCreateParams(params),
910
+ });
911
+ };
912
+ // https://stackoverflow.com/questions/3966484/why-does-modulus-operator-return-fractional-number-in-javascript/31711034#31711034
913
+ function floatSafeRemainder(val, step) {
914
+ const valDecCount = (val.toString().split(".")[1] || "").length;
915
+ const stepDecCount = (step.toString().split(".")[1] || "").length;
916
+ const decCount = valDecCount > stepDecCount ? valDecCount : stepDecCount;
917
+ const valInt = parseInt(val.toFixed(decCount).replace(".", ""));
918
+ const stepInt = parseInt(step.toFixed(decCount).replace(".", ""));
919
+ return (valInt % stepInt) / Math.pow(10, decCount);
920
+ }
921
+ class ZodNumber extends ZodType {
922
+ constructor() {
923
+ super(...arguments);
924
+ this.min = this.gte;
925
+ this.max = this.lte;
926
+ this.step = this.multipleOf;
927
+ }
928
+ _parse(input) {
929
+ const parsedType = this._getType(input);
930
+ if (parsedType !== ZodParsedType.number) {
931
+ const ctx = this._getOrReturnCtx(input);
932
+ addIssueToContext(ctx, {
933
+ code: ZodIssueCode.invalid_type,
934
+ expected: ZodParsedType.number,
935
+ received: ctx.parsedType,
936
+ });
937
+ return INVALID;
938
+ }
939
+ let ctx = undefined;
940
+ const status = new ParseStatus();
941
+ for (const check of this._def.checks) {
942
+ if (check.kind === "int") {
943
+ if (!util.isInteger(input.data)) {
944
+ ctx = this._getOrReturnCtx(input, ctx);
945
+ addIssueToContext(ctx, {
946
+ code: ZodIssueCode.invalid_type,
947
+ expected: "integer",
948
+ received: "float",
949
+ message: check.message,
950
+ });
951
+ status.dirty();
952
+ }
953
+ }
954
+ else if (check.kind === "min") {
955
+ const tooSmall = check.inclusive
956
+ ? input.data < check.value
957
+ : input.data <= check.value;
958
+ if (tooSmall) {
959
+ ctx = this._getOrReturnCtx(input, ctx);
960
+ addIssueToContext(ctx, {
961
+ code: ZodIssueCode.too_small,
962
+ minimum: check.value,
963
+ type: "number",
964
+ inclusive: check.inclusive,
965
+ message: check.message,
966
+ });
967
+ status.dirty();
968
+ }
969
+ }
970
+ else if (check.kind === "max") {
971
+ const tooBig = check.inclusive
972
+ ? input.data > check.value
973
+ : input.data >= check.value;
974
+ if (tooBig) {
975
+ ctx = this._getOrReturnCtx(input, ctx);
976
+ addIssueToContext(ctx, {
977
+ code: ZodIssueCode.too_big,
978
+ maximum: check.value,
979
+ type: "number",
980
+ inclusive: check.inclusive,
981
+ message: check.message,
982
+ });
983
+ status.dirty();
984
+ }
985
+ }
986
+ else if (check.kind === "multipleOf") {
987
+ if (floatSafeRemainder(input.data, check.value) !== 0) {
988
+ ctx = this._getOrReturnCtx(input, ctx);
989
+ addIssueToContext(ctx, {
990
+ code: ZodIssueCode.not_multiple_of,
991
+ multipleOf: check.value,
992
+ message: check.message,
993
+ });
994
+ status.dirty();
995
+ }
996
+ }
997
+ else {
998
+ util.assertNever(check);
999
+ }
1000
+ }
1001
+ return { status: status.value, value: input.data };
1002
+ }
1003
+ gte(value, message) {
1004
+ return this.setLimit("min", value, true, errorUtil.toString(message));
1005
+ }
1006
+ gt(value, message) {
1007
+ return this.setLimit("min", value, false, errorUtil.toString(message));
1008
+ }
1009
+ lte(value, message) {
1010
+ return this.setLimit("max", value, true, errorUtil.toString(message));
1011
+ }
1012
+ lt(value, message) {
1013
+ return this.setLimit("max", value, false, errorUtil.toString(message));
1014
+ }
1015
+ setLimit(kind, value, inclusive, message) {
1016
+ return new ZodNumber({
1017
+ ...this._def,
1018
+ checks: [
1019
+ ...this._def.checks,
1020
+ {
1021
+ kind,
1022
+ value,
1023
+ inclusive,
1024
+ message: errorUtil.toString(message),
1025
+ },
1026
+ ],
1027
+ });
1028
+ }
1029
+ _addCheck(check) {
1030
+ return new ZodNumber({
1031
+ ...this._def,
1032
+ checks: [...this._def.checks, check],
1033
+ });
1034
+ }
1035
+ int(message) {
1036
+ return this._addCheck({
1037
+ kind: "int",
1038
+ message: errorUtil.toString(message),
1039
+ });
1040
+ }
1041
+ positive(message) {
1042
+ return this._addCheck({
1043
+ kind: "min",
1044
+ value: 0,
1045
+ inclusive: false,
1046
+ message: errorUtil.toString(message),
1047
+ });
1048
+ }
1049
+ negative(message) {
1050
+ return this._addCheck({
1051
+ kind: "max",
1052
+ value: 0,
1053
+ inclusive: false,
1054
+ message: errorUtil.toString(message),
1055
+ });
1056
+ }
1057
+ nonpositive(message) {
1058
+ return this._addCheck({
1059
+ kind: "max",
1060
+ value: 0,
1061
+ inclusive: true,
1062
+ message: errorUtil.toString(message),
1063
+ });
1064
+ }
1065
+ nonnegative(message) {
1066
+ return this._addCheck({
1067
+ kind: "min",
1068
+ value: 0,
1069
+ inclusive: true,
1070
+ message: errorUtil.toString(message),
1071
+ });
1072
+ }
1073
+ multipleOf(value, message) {
1074
+ return this._addCheck({
1075
+ kind: "multipleOf",
1076
+ value: value,
1077
+ message: errorUtil.toString(message),
1078
+ });
1079
+ }
1080
+ get minValue() {
1081
+ let min = null;
1082
+ for (const ch of this._def.checks) {
1083
+ if (ch.kind === "min") {
1084
+ if (min === null || ch.value > min)
1085
+ min = ch.value;
1086
+ }
1087
+ }
1088
+ return min;
1089
+ }
1090
+ get maxValue() {
1091
+ let max = null;
1092
+ for (const ch of this._def.checks) {
1093
+ if (ch.kind === "max") {
1094
+ if (max === null || ch.value < max)
1095
+ max = ch.value;
1096
+ }
1097
+ }
1098
+ return max;
1099
+ }
1100
+ get isInt() {
1101
+ return !!this._def.checks.find((ch) => ch.kind === "int");
1102
+ }
1103
+ }
1104
+ ZodNumber.create = (params) => {
1105
+ return new ZodNumber({
1106
+ checks: [],
1107
+ typeName: exports.ZodFirstPartyTypeKind.ZodNumber,
1108
+ ...processCreateParams(params),
1109
+ });
1110
+ };
1111
+ class ZodBigInt extends ZodType {
1112
+ _parse(input) {
1113
+ const parsedType = this._getType(input);
1114
+ if (parsedType !== ZodParsedType.bigint) {
1115
+ const ctx = this._getOrReturnCtx(input);
1116
+ addIssueToContext(ctx, {
1117
+ code: ZodIssueCode.invalid_type,
1118
+ expected: ZodParsedType.bigint,
1119
+ received: ctx.parsedType,
1120
+ });
1121
+ return INVALID;
1122
+ }
1123
+ return OK(input.data);
1124
+ }
1125
+ }
1126
+ ZodBigInt.create = (params) => {
1127
+ return new ZodBigInt({
1128
+ typeName: exports.ZodFirstPartyTypeKind.ZodBigInt,
1129
+ ...processCreateParams(params),
1130
+ });
1131
+ };
1132
+ class ZodBoolean extends ZodType {
1133
+ _parse(input) {
1134
+ const parsedType = this._getType(input);
1135
+ if (parsedType !== ZodParsedType.boolean) {
1136
+ const ctx = this._getOrReturnCtx(input);
1137
+ addIssueToContext(ctx, {
1138
+ code: ZodIssueCode.invalid_type,
1139
+ expected: ZodParsedType.boolean,
1140
+ received: ctx.parsedType,
1141
+ });
1142
+ return INVALID;
1143
+ }
1144
+ return OK(input.data);
1145
+ }
1146
+ }
1147
+ ZodBoolean.create = (params) => {
1148
+ return new ZodBoolean({
1149
+ typeName: exports.ZodFirstPartyTypeKind.ZodBoolean,
1150
+ ...processCreateParams(params),
1151
+ });
1152
+ };
1153
+ class ZodDate extends ZodType {
1154
+ _parse(input) {
1155
+ const parsedType = this._getType(input);
1156
+ if (parsedType !== ZodParsedType.date) {
1157
+ const ctx = this._getOrReturnCtx(input);
1158
+ addIssueToContext(ctx, {
1159
+ code: ZodIssueCode.invalid_type,
1160
+ expected: ZodParsedType.date,
1161
+ received: ctx.parsedType,
1162
+ });
1163
+ return INVALID;
1164
+ }
1165
+ if (isNaN(input.data.getTime())) {
1166
+ const ctx = this._getOrReturnCtx(input);
1167
+ addIssueToContext(ctx, {
1168
+ code: ZodIssueCode.invalid_date,
1169
+ });
1170
+ return INVALID;
1171
+ }
1172
+ return {
1173
+ status: "valid",
1174
+ value: new Date(input.data.getTime()),
1175
+ };
1176
+ }
1177
+ }
1178
+ ZodDate.create = (params) => {
1179
+ return new ZodDate({
1180
+ typeName: exports.ZodFirstPartyTypeKind.ZodDate,
1181
+ ...processCreateParams(params),
1182
+ });
1183
+ };
1184
+ class ZodUndefined extends ZodType {
1185
+ _parse(input) {
1186
+ const parsedType = this._getType(input);
1187
+ if (parsedType !== ZodParsedType.undefined) {
1188
+ const ctx = this._getOrReturnCtx(input);
1189
+ addIssueToContext(ctx, {
1190
+ code: ZodIssueCode.invalid_type,
1191
+ expected: ZodParsedType.undefined,
1192
+ received: ctx.parsedType,
1193
+ });
1194
+ return INVALID;
1195
+ }
1196
+ return OK(input.data);
1197
+ }
1198
+ }
1199
+ ZodUndefined.create = (params) => {
1200
+ return new ZodUndefined({
1201
+ typeName: exports.ZodFirstPartyTypeKind.ZodUndefined,
1202
+ ...processCreateParams(params),
1203
+ });
1204
+ };
1205
+ class ZodNull extends ZodType {
1206
+ _parse(input) {
1207
+ const parsedType = this._getType(input);
1208
+ if (parsedType !== ZodParsedType.null) {
1209
+ const ctx = this._getOrReturnCtx(input);
1210
+ addIssueToContext(ctx, {
1211
+ code: ZodIssueCode.invalid_type,
1212
+ expected: ZodParsedType.null,
1213
+ received: ctx.parsedType,
1214
+ });
1215
+ return INVALID;
1216
+ }
1217
+ return OK(input.data);
1218
+ }
1219
+ }
1220
+ ZodNull.create = (params) => {
1221
+ return new ZodNull({
1222
+ typeName: exports.ZodFirstPartyTypeKind.ZodNull,
1223
+ ...processCreateParams(params),
1224
+ });
1225
+ };
1226
+ class ZodAny extends ZodType {
1227
+ constructor() {
1228
+ super(...arguments);
1229
+ // to prevent instances of other classes from extending ZodAny. this causes issues with catchall in ZodObject.
1230
+ this._any = true;
1231
+ }
1232
+ _parse(input) {
1233
+ return OK(input.data);
1234
+ }
1235
+ }
1236
+ ZodAny.create = (params) => {
1237
+ return new ZodAny({
1238
+ typeName: exports.ZodFirstPartyTypeKind.ZodAny,
1239
+ ...processCreateParams(params),
1240
+ });
1241
+ };
1242
+ class ZodUnknown extends ZodType {
1243
+ constructor() {
1244
+ super(...arguments);
1245
+ // required
1246
+ this._unknown = true;
1247
+ }
1248
+ _parse(input) {
1249
+ return OK(input.data);
1250
+ }
1251
+ }
1252
+ ZodUnknown.create = (params) => {
1253
+ return new ZodUnknown({
1254
+ typeName: exports.ZodFirstPartyTypeKind.ZodUnknown,
1255
+ ...processCreateParams(params),
1256
+ });
1257
+ };
1258
+ class ZodNever extends ZodType {
1259
+ _parse(input) {
1260
+ const ctx = this._getOrReturnCtx(input);
1261
+ addIssueToContext(ctx, {
1262
+ code: ZodIssueCode.invalid_type,
1263
+ expected: ZodParsedType.never,
1264
+ received: ctx.parsedType,
1265
+ });
1266
+ return INVALID;
1267
+ }
1268
+ }
1269
+ ZodNever.create = (params) => {
1270
+ return new ZodNever({
1271
+ typeName: exports.ZodFirstPartyTypeKind.ZodNever,
1272
+ ...processCreateParams(params),
1273
+ });
1274
+ };
1275
+ class ZodVoid extends ZodType {
1276
+ _parse(input) {
1277
+ const parsedType = this._getType(input);
1278
+ if (parsedType !== ZodParsedType.undefined) {
1279
+ const ctx = this._getOrReturnCtx(input);
1280
+ addIssueToContext(ctx, {
1281
+ code: ZodIssueCode.invalid_type,
1282
+ expected: ZodParsedType.void,
1283
+ received: ctx.parsedType,
1284
+ });
1285
+ return INVALID;
1286
+ }
1287
+ return OK(input.data);
1288
+ }
1289
+ }
1290
+ ZodVoid.create = (params) => {
1291
+ return new ZodVoid({
1292
+ typeName: exports.ZodFirstPartyTypeKind.ZodVoid,
1293
+ ...processCreateParams(params),
1294
+ });
1295
+ };
1296
+ class ZodArray extends ZodType {
1297
+ _parse(input) {
1298
+ const { ctx, status } = this._processInputParams(input);
1299
+ const def = this._def;
1300
+ if (ctx.parsedType !== ZodParsedType.array) {
1301
+ addIssueToContext(ctx, {
1302
+ code: ZodIssueCode.invalid_type,
1303
+ expected: ZodParsedType.array,
1304
+ received: ctx.parsedType,
1305
+ });
1306
+ return INVALID;
1307
+ }
1308
+ if (def.minLength !== null) {
1309
+ if (ctx.data.length < def.minLength.value) {
1310
+ addIssueToContext(ctx, {
1311
+ code: ZodIssueCode.too_small,
1312
+ minimum: def.minLength.value,
1313
+ type: "array",
1314
+ inclusive: true,
1315
+ message: def.minLength.message,
1316
+ });
1317
+ status.dirty();
1318
+ }
1319
+ }
1320
+ if (def.maxLength !== null) {
1321
+ if (ctx.data.length > def.maxLength.value) {
1322
+ addIssueToContext(ctx, {
1323
+ code: ZodIssueCode.too_big,
1324
+ maximum: def.maxLength.value,
1325
+ type: "array",
1326
+ inclusive: true,
1327
+ message: def.maxLength.message,
1328
+ });
1329
+ status.dirty();
1330
+ }
1331
+ }
1332
+ if (ctx.common.async) {
1333
+ return Promise.all(ctx.data.map((item, i) => {
1334
+ return def.type._parseAsync(new ParseInputLazyPath(ctx, item, ctx.path, i));
1335
+ })).then((result) => {
1336
+ return ParseStatus.mergeArray(status, result);
1337
+ });
1338
+ }
1339
+ const result = ctx.data.map((item, i) => {
1340
+ return def.type._parseSync(new ParseInputLazyPath(ctx, item, ctx.path, i));
1341
+ });
1342
+ return ParseStatus.mergeArray(status, result);
1343
+ }
1344
+ get element() {
1345
+ return this._def.type;
1346
+ }
1347
+ min(minLength, message) {
1348
+ return new ZodArray({
1349
+ ...this._def,
1350
+ minLength: { value: minLength, message: errorUtil.toString(message) },
1351
+ });
1352
+ }
1353
+ max(maxLength, message) {
1354
+ return new ZodArray({
1355
+ ...this._def,
1356
+ maxLength: { value: maxLength, message: errorUtil.toString(message) },
1357
+ });
1358
+ }
1359
+ length(len, message) {
1360
+ return this.min(len, message).max(len, message);
1361
+ }
1362
+ nonempty(message) {
1363
+ return this.min(1, message);
1364
+ }
1365
+ }
1366
+ ZodArray.create = (schema, params) => {
1367
+ return new ZodArray({
1368
+ type: schema,
1369
+ minLength: null,
1370
+ maxLength: null,
1371
+ typeName: exports.ZodFirstPartyTypeKind.ZodArray,
1372
+ ...processCreateParams(params),
1373
+ });
1374
+ };
1375
+ /////////////////////////////////////////
1376
+ /////////////////////////////////////////
1377
+ ////////// //////////
1378
+ ////////// ZodObject //////////
1379
+ ////////// //////////
1380
+ /////////////////////////////////////////
1381
+ /////////////////////////////////////////
1382
+ exports.objectUtil = void 0;
1383
+ (function (objectUtil) {
1384
+ objectUtil.mergeShapes = (first, second) => {
1385
+ return {
1386
+ ...first,
1387
+ ...second, // second overwrites first
1388
+ };
1389
+ };
1390
+ })(exports.objectUtil || (exports.objectUtil = {}));
1391
+ const AugmentFactory = (def) => (augmentation) => {
1392
+ return new ZodObject({
1393
+ ...def,
1394
+ shape: () => ({
1395
+ ...def.shape(),
1396
+ ...augmentation,
1397
+ }),
1398
+ });
1399
+ };
1400
+ function deepPartialify(schema) {
1401
+ if (schema instanceof ZodObject) {
1402
+ const newShape = {};
1403
+ for (const key in schema.shape) {
1404
+ const fieldSchema = schema.shape[key];
1405
+ newShape[key] = ZodOptional.create(deepPartialify(fieldSchema));
1406
+ }
1407
+ return new ZodObject({
1408
+ ...schema._def,
1409
+ shape: () => newShape,
1410
+ });
1411
+ }
1412
+ else if (schema instanceof ZodArray) {
1413
+ return ZodArray.create(deepPartialify(schema.element));
1414
+ }
1415
+ else if (schema instanceof ZodOptional) {
1416
+ return ZodOptional.create(deepPartialify(schema.unwrap()));
1417
+ }
1418
+ else if (schema instanceof ZodNullable) {
1419
+ return ZodNullable.create(deepPartialify(schema.unwrap()));
1420
+ }
1421
+ else if (schema instanceof ZodTuple) {
1422
+ return ZodTuple.create(schema.items.map((item) => deepPartialify(item)));
1423
+ }
1424
+ else {
1425
+ return schema;
1426
+ }
1427
+ }
1428
+ class ZodObject extends ZodType {
1429
+ constructor() {
1430
+ super(...arguments);
1431
+ this._cached = null;
1432
+ /**
1433
+ * @deprecated In most cases, this is no longer needed - unknown properties are now silently stripped.
1434
+ * If you want to pass through unknown properties, use `.passthrough()` instead.
1435
+ */
1436
+ this.nonstrict = this.passthrough;
1437
+ this.augment = AugmentFactory(this._def);
1438
+ this.extend = AugmentFactory(this._def);
1439
+ }
1440
+ _getCached() {
1441
+ if (this._cached !== null)
1442
+ return this._cached;
1443
+ const shape = this._def.shape();
1444
+ const keys = util.objectKeys(shape);
1445
+ return (this._cached = { shape, keys });
1446
+ }
1447
+ _parse(input) {
1448
+ const parsedType = this._getType(input);
1449
+ if (parsedType !== ZodParsedType.object) {
1450
+ const ctx = this._getOrReturnCtx(input);
1451
+ addIssueToContext(ctx, {
1452
+ code: ZodIssueCode.invalid_type,
1453
+ expected: ZodParsedType.object,
1454
+ received: ctx.parsedType,
1455
+ });
1456
+ return INVALID;
1457
+ }
1458
+ const { status, ctx } = this._processInputParams(input);
1459
+ const { shape, keys: shapeKeys } = this._getCached();
1460
+ const extraKeys = [];
1461
+ for (const key in ctx.data) {
1462
+ if (!shapeKeys.includes(key)) {
1463
+ extraKeys.push(key);
1464
+ }
1465
+ }
1466
+ const pairs = [];
1467
+ for (const key of shapeKeys) {
1468
+ const keyValidator = shape[key];
1469
+ const value = ctx.data[key];
1470
+ pairs.push({
1471
+ key: { status: "valid", value: key },
1472
+ value: keyValidator._parse(new ParseInputLazyPath(ctx, value, ctx.path, key)),
1473
+ alwaysSet: key in ctx.data,
1474
+ });
1475
+ }
1476
+ if (this._def.catchall instanceof ZodNever) {
1477
+ const unknownKeys = this._def.unknownKeys;
1478
+ if (unknownKeys === "passthrough") {
1479
+ for (const key of extraKeys) {
1480
+ pairs.push({
1481
+ key: { status: "valid", value: key },
1482
+ value: { status: "valid", value: ctx.data[key] },
1483
+ });
1484
+ }
1485
+ }
1486
+ else if (unknownKeys === "strict") {
1487
+ if (extraKeys.length > 0) {
1488
+ addIssueToContext(ctx, {
1489
+ code: ZodIssueCode.unrecognized_keys,
1490
+ keys: extraKeys,
1491
+ });
1492
+ status.dirty();
1493
+ }
1494
+ }
1495
+ else if (unknownKeys === "strip") ;
1496
+ else {
1497
+ throw new Error(`Internal ZodObject error: invalid unknownKeys value.`);
1498
+ }
1499
+ }
1500
+ else {
1501
+ // run catchall validation
1502
+ const catchall = this._def.catchall;
1503
+ for (const key of extraKeys) {
1504
+ const value = ctx.data[key];
1505
+ pairs.push({
1506
+ key: { status: "valid", value: key },
1507
+ value: catchall._parse(new ParseInputLazyPath(ctx, value, ctx.path, key) //, ctx.child(key), value, getParsedType(value)
1508
+ ),
1509
+ alwaysSet: key in ctx.data,
1510
+ });
1511
+ }
1512
+ }
1513
+ if (ctx.common.async) {
1514
+ return Promise.resolve()
1515
+ .then(async () => {
1516
+ const syncPairs = [];
1517
+ for (const pair of pairs) {
1518
+ const key = await pair.key;
1519
+ syncPairs.push({
1520
+ key,
1521
+ value: await pair.value,
1522
+ alwaysSet: pair.alwaysSet,
1523
+ });
1524
+ }
1525
+ return syncPairs;
1526
+ })
1527
+ .then((syncPairs) => {
1528
+ return ParseStatus.mergeObjectSync(status, syncPairs);
1529
+ });
1530
+ }
1531
+ else {
1532
+ return ParseStatus.mergeObjectSync(status, pairs);
1533
+ }
1534
+ }
1535
+ get shape() {
1536
+ return this._def.shape();
1537
+ }
1538
+ strict(message) {
1539
+ errorUtil.errToObj;
1540
+ return new ZodObject({
1541
+ ...this._def,
1542
+ unknownKeys: "strict",
1543
+ ...(message !== undefined
1544
+ ? {
1545
+ errorMap: (issue, ctx) => {
1546
+ var _a, _b, _c, _d;
1547
+ const defaultError = (_c = (_b = (_a = this._def).errorMap) === null || _b === void 0 ? void 0 : _b.call(_a, issue, ctx).message) !== null && _c !== void 0 ? _c : ctx.defaultError;
1548
+ if (issue.code === "unrecognized_keys")
1549
+ return {
1550
+ message: (_d = errorUtil.errToObj(message).message) !== null && _d !== void 0 ? _d : defaultError,
1551
+ };
1552
+ return {
1553
+ message: defaultError,
1554
+ };
1555
+ },
1556
+ }
1557
+ : {}),
1558
+ });
1559
+ }
1560
+ strip() {
1561
+ return new ZodObject({
1562
+ ...this._def,
1563
+ unknownKeys: "strip",
1564
+ });
1565
+ }
1566
+ passthrough() {
1567
+ return new ZodObject({
1568
+ ...this._def,
1569
+ unknownKeys: "passthrough",
1570
+ });
1571
+ }
1572
+ setKey(key, schema) {
1573
+ return this.augment({ [key]: schema });
1574
+ }
1575
+ /**
1576
+ * Prior to zod@1.0.12 there was a bug in the
1577
+ * inferred type of merged objects. Please
1578
+ * upgrade if you are experiencing issues.
1579
+ */
1580
+ merge(merging) {
1581
+ // const mergedShape = objectUtil.mergeShapes(
1582
+ // this._def.shape(),
1583
+ // merging._def.shape()
1584
+ // );
1585
+ const merged = new ZodObject({
1586
+ unknownKeys: merging._def.unknownKeys,
1587
+ catchall: merging._def.catchall,
1588
+ shape: () => exports.objectUtil.mergeShapes(this._def.shape(), merging._def.shape()),
1589
+ typeName: exports.ZodFirstPartyTypeKind.ZodObject,
1590
+ });
1591
+ return merged;
1592
+ }
1593
+ catchall(index) {
1594
+ return new ZodObject({
1595
+ ...this._def,
1596
+ catchall: index,
1597
+ });
1598
+ }
1599
+ pick(mask) {
1600
+ const shape = {};
1601
+ util.objectKeys(mask).map((key) => {
1602
+ shape[key] = this.shape[key];
1603
+ });
1604
+ return new ZodObject({
1605
+ ...this._def,
1606
+ shape: () => shape,
1607
+ });
1608
+ }
1609
+ omit(mask) {
1610
+ const shape = {};
1611
+ util.objectKeys(this.shape).map((key) => {
1612
+ if (util.objectKeys(mask).indexOf(key) === -1) {
1613
+ shape[key] = this.shape[key];
1614
+ }
1615
+ });
1616
+ return new ZodObject({
1617
+ ...this._def,
1618
+ shape: () => shape,
1619
+ });
1620
+ }
1621
+ deepPartial() {
1622
+ return deepPartialify(this);
1623
+ }
1624
+ partial(mask) {
1625
+ const newShape = {};
1626
+ if (mask) {
1627
+ util.objectKeys(this.shape).map((key) => {
1628
+ if (util.objectKeys(mask).indexOf(key) === -1) {
1629
+ newShape[key] = this.shape[key];
1630
+ }
1631
+ else {
1632
+ newShape[key] = this.shape[key].optional();
1633
+ }
1634
+ });
1635
+ return new ZodObject({
1636
+ ...this._def,
1637
+ shape: () => newShape,
1638
+ });
1639
+ }
1640
+ else {
1641
+ for (const key in this.shape) {
1642
+ const fieldSchema = this.shape[key];
1643
+ newShape[key] = fieldSchema.optional();
1644
+ }
1645
+ }
1646
+ return new ZodObject({
1647
+ ...this._def,
1648
+ shape: () => newShape,
1649
+ });
1650
+ }
1651
+ required() {
1652
+ const newShape = {};
1653
+ for (const key in this.shape) {
1654
+ const fieldSchema = this.shape[key];
1655
+ let newField = fieldSchema;
1656
+ while (newField instanceof ZodOptional) {
1657
+ newField = newField._def.innerType;
1658
+ }
1659
+ newShape[key] = newField;
1660
+ }
1661
+ return new ZodObject({
1662
+ ...this._def,
1663
+ shape: () => newShape,
1664
+ });
1665
+ }
1666
+ }
1667
+ ZodObject.create = (shape, params) => {
1668
+ return new ZodObject({
1669
+ shape: () => shape,
1670
+ unknownKeys: "strip",
1671
+ catchall: ZodNever.create(),
1672
+ typeName: exports.ZodFirstPartyTypeKind.ZodObject,
1673
+ ...processCreateParams(params),
1674
+ });
1675
+ };
1676
+ ZodObject.strictCreate = (shape, params) => {
1677
+ return new ZodObject({
1678
+ shape: () => shape,
1679
+ unknownKeys: "strict",
1680
+ catchall: ZodNever.create(),
1681
+ typeName: exports.ZodFirstPartyTypeKind.ZodObject,
1682
+ ...processCreateParams(params),
1683
+ });
1684
+ };
1685
+ ZodObject.lazycreate = (shape, params) => {
1686
+ return new ZodObject({
1687
+ shape,
1688
+ unknownKeys: "strip",
1689
+ catchall: ZodNever.create(),
1690
+ typeName: exports.ZodFirstPartyTypeKind.ZodObject,
1691
+ ...processCreateParams(params),
1692
+ });
1693
+ };
1694
+ class ZodUnion extends ZodType {
1695
+ _parse(input) {
1696
+ const { ctx } = this._processInputParams(input);
1697
+ const options = this._def.options;
1698
+ function handleResults(results) {
1699
+ // return first issue-free validation if it exists
1700
+ for (const result of results) {
1701
+ if (result.result.status === "valid") {
1702
+ return result.result;
1703
+ }
1704
+ }
1705
+ for (const result of results) {
1706
+ if (result.result.status === "dirty") {
1707
+ // add issues from dirty option
1708
+ ctx.common.issues.push(...result.ctx.common.issues);
1709
+ return result.result;
1710
+ }
1711
+ }
1712
+ // return invalid
1713
+ const unionErrors = results.map((result) => new ZodError(result.ctx.common.issues));
1714
+ addIssueToContext(ctx, {
1715
+ code: ZodIssueCode.invalid_union,
1716
+ unionErrors,
1717
+ });
1718
+ return INVALID;
1719
+ }
1720
+ if (ctx.common.async) {
1721
+ return Promise.all(options.map(async (option) => {
1722
+ const childCtx = {
1723
+ ...ctx,
1724
+ common: {
1725
+ ...ctx.common,
1726
+ issues: [],
1727
+ },
1728
+ parent: null,
1729
+ };
1730
+ return {
1731
+ result: await option._parseAsync({
1732
+ data: ctx.data,
1733
+ path: ctx.path,
1734
+ parent: childCtx,
1735
+ }),
1736
+ ctx: childCtx,
1737
+ };
1738
+ })).then(handleResults);
1739
+ }
1740
+ else {
1741
+ let dirty = undefined;
1742
+ const issues = [];
1743
+ for (const option of options) {
1744
+ const childCtx = {
1745
+ ...ctx,
1746
+ common: {
1747
+ ...ctx.common,
1748
+ issues: [],
1749
+ },
1750
+ parent: null,
1751
+ };
1752
+ const result = option._parseSync({
1753
+ data: ctx.data,
1754
+ path: ctx.path,
1755
+ parent: childCtx,
1756
+ });
1757
+ if (result.status === "valid") {
1758
+ return result;
1759
+ }
1760
+ else if (result.status === "dirty" && !dirty) {
1761
+ dirty = { result, ctx: childCtx };
1762
+ }
1763
+ if (childCtx.common.issues.length) {
1764
+ issues.push(childCtx.common.issues);
1765
+ }
1766
+ }
1767
+ if (dirty) {
1768
+ ctx.common.issues.push(...dirty.ctx.common.issues);
1769
+ return dirty.result;
1770
+ }
1771
+ const unionErrors = issues.map((issues) => new ZodError(issues));
1772
+ addIssueToContext(ctx, {
1773
+ code: ZodIssueCode.invalid_union,
1774
+ unionErrors,
1775
+ });
1776
+ return INVALID;
1777
+ }
1778
+ }
1779
+ get options() {
1780
+ return this._def.options;
1781
+ }
1782
+ }
1783
+ ZodUnion.create = (types, params) => {
1784
+ return new ZodUnion({
1785
+ options: types,
1786
+ typeName: exports.ZodFirstPartyTypeKind.ZodUnion,
1787
+ ...processCreateParams(params),
1788
+ });
1789
+ };
1790
+ class ZodDiscriminatedUnion extends ZodType {
1791
+ _parse(input) {
1792
+ const { ctx } = this._processInputParams(input);
1793
+ if (ctx.parsedType !== ZodParsedType.object) {
1794
+ addIssueToContext(ctx, {
1795
+ code: ZodIssueCode.invalid_type,
1796
+ expected: ZodParsedType.object,
1797
+ received: ctx.parsedType,
1798
+ });
1799
+ return INVALID;
1800
+ }
1801
+ const discriminator = this.discriminator;
1802
+ const discriminatorValue = ctx.data[discriminator];
1803
+ const option = this.options.get(discriminatorValue);
1804
+ if (!option) {
1805
+ addIssueToContext(ctx, {
1806
+ code: ZodIssueCode.invalid_union_discriminator,
1807
+ options: this.validDiscriminatorValues,
1808
+ path: [discriminator],
1809
+ });
1810
+ return INVALID;
1811
+ }
1812
+ if (ctx.common.async) {
1813
+ return option._parseAsync({
1814
+ data: ctx.data,
1815
+ path: ctx.path,
1816
+ parent: ctx,
1817
+ });
1818
+ }
1819
+ else {
1820
+ return option._parseSync({
1821
+ data: ctx.data,
1822
+ path: ctx.path,
1823
+ parent: ctx,
1824
+ });
1825
+ }
1826
+ }
1827
+ get discriminator() {
1828
+ return this._def.discriminator;
1829
+ }
1830
+ get validDiscriminatorValues() {
1831
+ return Array.from(this.options.keys());
1832
+ }
1833
+ get options() {
1834
+ return this._def.options;
1835
+ }
1836
+ /**
1837
+ * The constructor of the discriminated union schema. Its behaviour is very similar to that of the normal z.union() constructor.
1838
+ * However, it only allows a union of objects, all of which need to share a discriminator property. This property must
1839
+ * have a different value for each object in the union.
1840
+ * @param discriminator the name of the discriminator property
1841
+ * @param types an array of object schemas
1842
+ * @param params
1843
+ */
1844
+ static create(discriminator, types, params) {
1845
+ // Get all the valid discriminator values
1846
+ const options = new Map();
1847
+ try {
1848
+ types.forEach((type) => {
1849
+ const discriminatorValue = type.shape[discriminator].value;
1850
+ options.set(discriminatorValue, type);
1851
+ });
1852
+ }
1853
+ catch (e) {
1854
+ throw new Error("The discriminator value could not be extracted from all the provided schemas");
1855
+ }
1856
+ // Assert that all the discriminator values are unique
1857
+ if (options.size !== types.length) {
1858
+ throw new Error("Some of the discriminator values are not unique");
1859
+ }
1860
+ return new ZodDiscriminatedUnion({
1861
+ typeName: exports.ZodFirstPartyTypeKind.ZodDiscriminatedUnion,
1862
+ discriminator,
1863
+ options,
1864
+ ...processCreateParams(params),
1865
+ });
1866
+ }
1867
+ }
1868
+ function mergeValues(a, b) {
1869
+ const aType = getParsedType(a);
1870
+ const bType = getParsedType(b);
1871
+ if (a === b) {
1872
+ return { valid: true, data: a };
1873
+ }
1874
+ else if (aType === ZodParsedType.object && bType === ZodParsedType.object) {
1875
+ const bKeys = util.objectKeys(b);
1876
+ const sharedKeys = util
1877
+ .objectKeys(a)
1878
+ .filter((key) => bKeys.indexOf(key) !== -1);
1879
+ const newObj = { ...a, ...b };
1880
+ for (const key of sharedKeys) {
1881
+ const sharedValue = mergeValues(a[key], b[key]);
1882
+ if (!sharedValue.valid) {
1883
+ return { valid: false };
1884
+ }
1885
+ newObj[key] = sharedValue.data;
1886
+ }
1887
+ return { valid: true, data: newObj };
1888
+ }
1889
+ else if (aType === ZodParsedType.array && bType === ZodParsedType.array) {
1890
+ if (a.length !== b.length) {
1891
+ return { valid: false };
1892
+ }
1893
+ const newArray = [];
1894
+ for (let index = 0; index < a.length; index++) {
1895
+ const itemA = a[index];
1896
+ const itemB = b[index];
1897
+ const sharedValue = mergeValues(itemA, itemB);
1898
+ if (!sharedValue.valid) {
1899
+ return { valid: false };
1900
+ }
1901
+ newArray.push(sharedValue.data);
1902
+ }
1903
+ return { valid: true, data: newArray };
1904
+ }
1905
+ else if (aType === ZodParsedType.date &&
1906
+ bType === ZodParsedType.date &&
1907
+ +a === +b) {
1908
+ return { valid: true, data: a };
1909
+ }
1910
+ else {
1911
+ return { valid: false };
1912
+ }
1913
+ }
1914
+ class ZodIntersection extends ZodType {
1915
+ _parse(input) {
1916
+ const { status, ctx } = this._processInputParams(input);
1917
+ const handleParsed = (parsedLeft, parsedRight) => {
1918
+ if (isAborted(parsedLeft) || isAborted(parsedRight)) {
1919
+ return INVALID;
1920
+ }
1921
+ const merged = mergeValues(parsedLeft.value, parsedRight.value);
1922
+ if (!merged.valid) {
1923
+ addIssueToContext(ctx, {
1924
+ code: ZodIssueCode.invalid_intersection_types,
1925
+ });
1926
+ return INVALID;
1927
+ }
1928
+ if (isDirty(parsedLeft) || isDirty(parsedRight)) {
1929
+ status.dirty();
1930
+ }
1931
+ return { status: status.value, value: merged.data };
1932
+ };
1933
+ if (ctx.common.async) {
1934
+ return Promise.all([
1935
+ this._def.left._parseAsync({
1936
+ data: ctx.data,
1937
+ path: ctx.path,
1938
+ parent: ctx,
1939
+ }),
1940
+ this._def.right._parseAsync({
1941
+ data: ctx.data,
1942
+ path: ctx.path,
1943
+ parent: ctx,
1944
+ }),
1945
+ ]).then(([left, right]) => handleParsed(left, right));
1946
+ }
1947
+ else {
1948
+ return handleParsed(this._def.left._parseSync({
1949
+ data: ctx.data,
1950
+ path: ctx.path,
1951
+ parent: ctx,
1952
+ }), this._def.right._parseSync({
1953
+ data: ctx.data,
1954
+ path: ctx.path,
1955
+ parent: ctx,
1956
+ }));
1957
+ }
1958
+ }
1959
+ }
1960
+ ZodIntersection.create = (left, right, params) => {
1961
+ return new ZodIntersection({
1962
+ left: left,
1963
+ right: right,
1964
+ typeName: exports.ZodFirstPartyTypeKind.ZodIntersection,
1965
+ ...processCreateParams(params),
1966
+ });
1967
+ };
1968
+ class ZodTuple extends ZodType {
1969
+ _parse(input) {
1970
+ const { status, ctx } = this._processInputParams(input);
1971
+ if (ctx.parsedType !== ZodParsedType.array) {
1972
+ addIssueToContext(ctx, {
1973
+ code: ZodIssueCode.invalid_type,
1974
+ expected: ZodParsedType.array,
1975
+ received: ctx.parsedType,
1976
+ });
1977
+ return INVALID;
1978
+ }
1979
+ if (ctx.data.length < this._def.items.length) {
1980
+ addIssueToContext(ctx, {
1981
+ code: ZodIssueCode.too_small,
1982
+ minimum: this._def.items.length,
1983
+ inclusive: true,
1984
+ type: "array",
1985
+ });
1986
+ return INVALID;
1987
+ }
1988
+ const rest = this._def.rest;
1989
+ if (!rest && ctx.data.length > this._def.items.length) {
1990
+ addIssueToContext(ctx, {
1991
+ code: ZodIssueCode.too_big,
1992
+ maximum: this._def.items.length,
1993
+ inclusive: true,
1994
+ type: "array",
1995
+ });
1996
+ status.dirty();
1997
+ }
1998
+ const items = ctx.data
1999
+ .map((item, itemIndex) => {
2000
+ const schema = this._def.items[itemIndex] || this._def.rest;
2001
+ if (!schema)
2002
+ return null;
2003
+ return schema._parse(new ParseInputLazyPath(ctx, item, ctx.path, itemIndex));
2004
+ })
2005
+ .filter((x) => !!x); // filter nulls
2006
+ if (ctx.common.async) {
2007
+ return Promise.all(items).then((results) => {
2008
+ return ParseStatus.mergeArray(status, results);
2009
+ });
2010
+ }
2011
+ else {
2012
+ return ParseStatus.mergeArray(status, items);
2013
+ }
2014
+ }
2015
+ get items() {
2016
+ return this._def.items;
2017
+ }
2018
+ rest(rest) {
2019
+ return new ZodTuple({
2020
+ ...this._def,
2021
+ rest,
2022
+ });
2023
+ }
2024
+ }
2025
+ ZodTuple.create = (schemas, params) => {
2026
+ return new ZodTuple({
2027
+ items: schemas,
2028
+ typeName: exports.ZodFirstPartyTypeKind.ZodTuple,
2029
+ rest: null,
2030
+ ...processCreateParams(params),
2031
+ });
2032
+ };
2033
+ class ZodRecord extends ZodType {
2034
+ get keySchema() {
2035
+ return this._def.keyType;
2036
+ }
2037
+ get valueSchema() {
2038
+ return this._def.valueType;
2039
+ }
2040
+ _parse(input) {
2041
+ const { status, ctx } = this._processInputParams(input);
2042
+ if (ctx.parsedType !== ZodParsedType.object) {
2043
+ addIssueToContext(ctx, {
2044
+ code: ZodIssueCode.invalid_type,
2045
+ expected: ZodParsedType.object,
2046
+ received: ctx.parsedType,
2047
+ });
2048
+ return INVALID;
2049
+ }
2050
+ const pairs = [];
2051
+ const keyType = this._def.keyType;
2052
+ const valueType = this._def.valueType;
2053
+ for (const key in ctx.data) {
2054
+ pairs.push({
2055
+ key: keyType._parse(new ParseInputLazyPath(ctx, key, ctx.path, key)),
2056
+ value: valueType._parse(new ParseInputLazyPath(ctx, ctx.data[key], ctx.path, key)),
2057
+ });
2058
+ }
2059
+ if (ctx.common.async) {
2060
+ return ParseStatus.mergeObjectAsync(status, pairs);
2061
+ }
2062
+ else {
2063
+ return ParseStatus.mergeObjectSync(status, pairs);
2064
+ }
2065
+ }
2066
+ get element() {
2067
+ return this._def.valueType;
2068
+ }
2069
+ static create(first, second, third) {
2070
+ if (second instanceof ZodType) {
2071
+ return new ZodRecord({
2072
+ keyType: first,
2073
+ valueType: second,
2074
+ typeName: exports.ZodFirstPartyTypeKind.ZodRecord,
2075
+ ...processCreateParams(third),
2076
+ });
2077
+ }
2078
+ return new ZodRecord({
2079
+ keyType: ZodString.create(),
2080
+ valueType: first,
2081
+ typeName: exports.ZodFirstPartyTypeKind.ZodRecord,
2082
+ ...processCreateParams(second),
2083
+ });
2084
+ }
2085
+ }
2086
+ class ZodMap extends ZodType {
2087
+ _parse(input) {
2088
+ const { status, ctx } = this._processInputParams(input);
2089
+ if (ctx.parsedType !== ZodParsedType.map) {
2090
+ addIssueToContext(ctx, {
2091
+ code: ZodIssueCode.invalid_type,
2092
+ expected: ZodParsedType.map,
2093
+ received: ctx.parsedType,
2094
+ });
2095
+ return INVALID;
2096
+ }
2097
+ const keyType = this._def.keyType;
2098
+ const valueType = this._def.valueType;
2099
+ const pairs = [...ctx.data.entries()].map(([key, value], index) => {
2100
+ return {
2101
+ key: keyType._parse(new ParseInputLazyPath(ctx, key, ctx.path, [index, "key"])),
2102
+ value: valueType._parse(new ParseInputLazyPath(ctx, value, ctx.path, [index, "value"])),
2103
+ };
2104
+ });
2105
+ if (ctx.common.async) {
2106
+ const finalMap = new Map();
2107
+ return Promise.resolve().then(async () => {
2108
+ for (const pair of pairs) {
2109
+ const key = await pair.key;
2110
+ const value = await pair.value;
2111
+ if (key.status === "aborted" || value.status === "aborted") {
2112
+ return INVALID;
2113
+ }
2114
+ if (key.status === "dirty" || value.status === "dirty") {
2115
+ status.dirty();
2116
+ }
2117
+ finalMap.set(key.value, value.value);
2118
+ }
2119
+ return { status: status.value, value: finalMap };
2120
+ });
2121
+ }
2122
+ else {
2123
+ const finalMap = new Map();
2124
+ for (const pair of pairs) {
2125
+ const key = pair.key;
2126
+ const value = pair.value;
2127
+ if (key.status === "aborted" || value.status === "aborted") {
2128
+ return INVALID;
2129
+ }
2130
+ if (key.status === "dirty" || value.status === "dirty") {
2131
+ status.dirty();
2132
+ }
2133
+ finalMap.set(key.value, value.value);
2134
+ }
2135
+ return { status: status.value, value: finalMap };
2136
+ }
2137
+ }
2138
+ }
2139
+ ZodMap.create = (keyType, valueType, params) => {
2140
+ return new ZodMap({
2141
+ valueType,
2142
+ keyType,
2143
+ typeName: exports.ZodFirstPartyTypeKind.ZodMap,
2144
+ ...processCreateParams(params),
2145
+ });
2146
+ };
2147
+ class ZodSet extends ZodType {
2148
+ _parse(input) {
2149
+ const { status, ctx } = this._processInputParams(input);
2150
+ if (ctx.parsedType !== ZodParsedType.set) {
2151
+ addIssueToContext(ctx, {
2152
+ code: ZodIssueCode.invalid_type,
2153
+ expected: ZodParsedType.set,
2154
+ received: ctx.parsedType,
2155
+ });
2156
+ return INVALID;
2157
+ }
2158
+ const def = this._def;
2159
+ if (def.minSize !== null) {
2160
+ if (ctx.data.size < def.minSize.value) {
2161
+ addIssueToContext(ctx, {
2162
+ code: ZodIssueCode.too_small,
2163
+ minimum: def.minSize.value,
2164
+ type: "set",
2165
+ inclusive: true,
2166
+ message: def.minSize.message,
2167
+ });
2168
+ status.dirty();
2169
+ }
2170
+ }
2171
+ if (def.maxSize !== null) {
2172
+ if (ctx.data.size > def.maxSize.value) {
2173
+ addIssueToContext(ctx, {
2174
+ code: ZodIssueCode.too_big,
2175
+ maximum: def.maxSize.value,
2176
+ type: "set",
2177
+ inclusive: true,
2178
+ message: def.maxSize.message,
2179
+ });
2180
+ status.dirty();
2181
+ }
2182
+ }
2183
+ const valueType = this._def.valueType;
2184
+ function finalizeSet(elements) {
2185
+ const parsedSet = new Set();
2186
+ for (const element of elements) {
2187
+ if (element.status === "aborted")
2188
+ return INVALID;
2189
+ if (element.status === "dirty")
2190
+ status.dirty();
2191
+ parsedSet.add(element.value);
2192
+ }
2193
+ return { status: status.value, value: parsedSet };
2194
+ }
2195
+ const elements = [...ctx.data.values()].map((item, i) => valueType._parse(new ParseInputLazyPath(ctx, item, ctx.path, i)));
2196
+ if (ctx.common.async) {
2197
+ return Promise.all(elements).then((elements) => finalizeSet(elements));
2198
+ }
2199
+ else {
2200
+ return finalizeSet(elements);
2201
+ }
2202
+ }
2203
+ min(minSize, message) {
2204
+ return new ZodSet({
2205
+ ...this._def,
2206
+ minSize: { value: minSize, message: errorUtil.toString(message) },
2207
+ });
2208
+ }
2209
+ max(maxSize, message) {
2210
+ return new ZodSet({
2211
+ ...this._def,
2212
+ maxSize: { value: maxSize, message: errorUtil.toString(message) },
2213
+ });
2214
+ }
2215
+ size(size, message) {
2216
+ return this.min(size, message).max(size, message);
2217
+ }
2218
+ nonempty(message) {
2219
+ return this.min(1, message);
2220
+ }
2221
+ }
2222
+ ZodSet.create = (valueType, params) => {
2223
+ return new ZodSet({
2224
+ valueType,
2225
+ minSize: null,
2226
+ maxSize: null,
2227
+ typeName: exports.ZodFirstPartyTypeKind.ZodSet,
2228
+ ...processCreateParams(params),
2229
+ });
2230
+ };
2231
+ class ZodFunction extends ZodType {
2232
+ constructor() {
2233
+ super(...arguments);
2234
+ this.validate = this.implement;
2235
+ }
2236
+ _parse(input) {
2237
+ const { ctx } = this._processInputParams(input);
2238
+ if (ctx.parsedType !== ZodParsedType.function) {
2239
+ addIssueToContext(ctx, {
2240
+ code: ZodIssueCode.invalid_type,
2241
+ expected: ZodParsedType.function,
2242
+ received: ctx.parsedType,
2243
+ });
2244
+ return INVALID;
2245
+ }
2246
+ function makeArgsIssue(args, error) {
2247
+ return makeIssue({
2248
+ data: args,
2249
+ path: ctx.path,
2250
+ errorMaps: [
2251
+ ctx.common.contextualErrorMap,
2252
+ ctx.schemaErrorMap,
2253
+ exports.overrideErrorMap,
2254
+ defaultErrorMap,
2255
+ ].filter((x) => !!x),
2256
+ issueData: {
2257
+ code: ZodIssueCode.invalid_arguments,
2258
+ argumentsError: error,
2259
+ },
2260
+ });
2261
+ }
2262
+ function makeReturnsIssue(returns, error) {
2263
+ return makeIssue({
2264
+ data: returns,
2265
+ path: ctx.path,
2266
+ errorMaps: [
2267
+ ctx.common.contextualErrorMap,
2268
+ ctx.schemaErrorMap,
2269
+ exports.overrideErrorMap,
2270
+ defaultErrorMap,
2271
+ ].filter((x) => !!x),
2272
+ issueData: {
2273
+ code: ZodIssueCode.invalid_return_type,
2274
+ returnTypeError: error,
2275
+ },
2276
+ });
2277
+ }
2278
+ const params = { errorMap: ctx.common.contextualErrorMap };
2279
+ const fn = ctx.data;
2280
+ if (this._def.returns instanceof ZodPromise) {
2281
+ return OK(async (...args) => {
2282
+ const error = new ZodError([]);
2283
+ const parsedArgs = await this._def.args
2284
+ .parseAsync(args, params)
2285
+ .catch((e) => {
2286
+ error.addIssue(makeArgsIssue(args, e));
2287
+ throw error;
2288
+ });
2289
+ const result = await fn(...parsedArgs);
2290
+ const parsedReturns = await this._def.returns._def.type
2291
+ .parseAsync(result, params)
2292
+ .catch((e) => {
2293
+ error.addIssue(makeReturnsIssue(result, e));
2294
+ throw error;
2295
+ });
2296
+ return parsedReturns;
2297
+ });
2298
+ }
2299
+ else {
2300
+ return OK((...args) => {
2301
+ const parsedArgs = this._def.args.safeParse(args, params);
2302
+ if (!parsedArgs.success) {
2303
+ throw new ZodError([makeArgsIssue(args, parsedArgs.error)]);
2304
+ }
2305
+ const result = fn(...parsedArgs.data);
2306
+ const parsedReturns = this._def.returns.safeParse(result, params);
2307
+ if (!parsedReturns.success) {
2308
+ throw new ZodError([makeReturnsIssue(result, parsedReturns.error)]);
2309
+ }
2310
+ return parsedReturns.data;
2311
+ });
2312
+ }
2313
+ }
2314
+ parameters() {
2315
+ return this._def.args;
2316
+ }
2317
+ returnType() {
2318
+ return this._def.returns;
2319
+ }
2320
+ args(...items) {
2321
+ return new ZodFunction({
2322
+ ...this._def,
2323
+ args: ZodTuple.create(items).rest(ZodUnknown.create()),
2324
+ });
2325
+ }
2326
+ returns(returnType) {
2327
+ return new ZodFunction({
2328
+ ...this._def,
2329
+ returns: returnType,
2330
+ });
2331
+ }
2332
+ implement(func) {
2333
+ const validatedFunc = this.parse(func);
2334
+ return validatedFunc;
2335
+ }
2336
+ strictImplement(func) {
2337
+ const validatedFunc = this.parse(func);
2338
+ return validatedFunc;
2339
+ }
2340
+ }
2341
+ ZodFunction.create = (args, returns, params) => {
2342
+ return new ZodFunction({
2343
+ args: (args
2344
+ ? args.rest(ZodUnknown.create())
2345
+ : ZodTuple.create([]).rest(ZodUnknown.create())),
2346
+ returns: returns || ZodUnknown.create(),
2347
+ typeName: exports.ZodFirstPartyTypeKind.ZodFunction,
2348
+ ...processCreateParams(params),
2349
+ });
2350
+ };
2351
+ class ZodLazy extends ZodType {
2352
+ get schema() {
2353
+ return this._def.getter();
2354
+ }
2355
+ _parse(input) {
2356
+ const { ctx } = this._processInputParams(input);
2357
+ const lazySchema = this._def.getter();
2358
+ return lazySchema._parse({ data: ctx.data, path: ctx.path, parent: ctx });
2359
+ }
2360
+ }
2361
+ ZodLazy.create = (getter, params) => {
2362
+ return new ZodLazy({
2363
+ getter: getter,
2364
+ typeName: exports.ZodFirstPartyTypeKind.ZodLazy,
2365
+ ...processCreateParams(params),
2366
+ });
2367
+ };
2368
+ class ZodLiteral extends ZodType {
2369
+ _parse(input) {
2370
+ if (input.data !== this._def.value) {
2371
+ const ctx = this._getOrReturnCtx(input);
2372
+ addIssueToContext(ctx, {
2373
+ code: ZodIssueCode.invalid_literal,
2374
+ expected: this._def.value,
2375
+ });
2376
+ return INVALID;
2377
+ }
2378
+ return { status: "valid", value: input.data };
2379
+ }
2380
+ get value() {
2381
+ return this._def.value;
2382
+ }
2383
+ }
2384
+ ZodLiteral.create = (value, params) => {
2385
+ return new ZodLiteral({
2386
+ value: value,
2387
+ typeName: exports.ZodFirstPartyTypeKind.ZodLiteral,
2388
+ ...processCreateParams(params),
2389
+ });
2390
+ };
2391
+ function createZodEnum(values) {
2392
+ return new ZodEnum({
2393
+ values: values,
2394
+ typeName: exports.ZodFirstPartyTypeKind.ZodEnum,
2395
+ });
2396
+ }
2397
+ class ZodEnum extends ZodType {
2398
+ _parse(input) {
2399
+ if (this._def.values.indexOf(input.data) === -1) {
2400
+ const ctx = this._getOrReturnCtx(input);
2401
+ addIssueToContext(ctx, {
2402
+ code: ZodIssueCode.invalid_enum_value,
2403
+ options: this._def.values,
2404
+ });
2405
+ return INVALID;
2406
+ }
2407
+ return OK(input.data);
2408
+ }
2409
+ get options() {
2410
+ return this._def.values;
2411
+ }
2412
+ get enum() {
2413
+ const enumValues = {};
2414
+ for (const val of this._def.values) {
2415
+ enumValues[val] = val;
2416
+ }
2417
+ return enumValues;
2418
+ }
2419
+ get Values() {
2420
+ const enumValues = {};
2421
+ for (const val of this._def.values) {
2422
+ enumValues[val] = val;
2423
+ }
2424
+ return enumValues;
2425
+ }
2426
+ get Enum() {
2427
+ const enumValues = {};
2428
+ for (const val of this._def.values) {
2429
+ enumValues[val] = val;
2430
+ }
2431
+ return enumValues;
2432
+ }
2433
+ }
2434
+ ZodEnum.create = createZodEnum;
2435
+ class ZodNativeEnum extends ZodType {
2436
+ _parse(input) {
2437
+ const nativeEnumValues = util.getValidEnumValues(this._def.values);
2438
+ if (nativeEnumValues.indexOf(input.data) === -1) {
2439
+ const ctx = this._getOrReturnCtx(input);
2440
+ addIssueToContext(ctx, {
2441
+ code: ZodIssueCode.invalid_enum_value,
2442
+ options: util.objectValues(nativeEnumValues),
2443
+ });
2444
+ return INVALID;
2445
+ }
2446
+ return OK(input.data);
2447
+ }
2448
+ get enum() {
2449
+ return this._def.values;
2450
+ }
2451
+ }
2452
+ ZodNativeEnum.create = (values, params) => {
2453
+ return new ZodNativeEnum({
2454
+ values: values,
2455
+ typeName: exports.ZodFirstPartyTypeKind.ZodNativeEnum,
2456
+ ...processCreateParams(params),
2457
+ });
2458
+ };
2459
+ class ZodPromise extends ZodType {
2460
+ _parse(input) {
2461
+ const { ctx } = this._processInputParams(input);
2462
+ if (ctx.parsedType !== ZodParsedType.promise &&
2463
+ ctx.common.async === false) {
2464
+ addIssueToContext(ctx, {
2465
+ code: ZodIssueCode.invalid_type,
2466
+ expected: ZodParsedType.promise,
2467
+ received: ctx.parsedType,
2468
+ });
2469
+ return INVALID;
2470
+ }
2471
+ const promisified = ctx.parsedType === ZodParsedType.promise
2472
+ ? ctx.data
2473
+ : Promise.resolve(ctx.data);
2474
+ return OK(promisified.then((data) => {
2475
+ return this._def.type.parseAsync(data, {
2476
+ path: ctx.path,
2477
+ errorMap: ctx.common.contextualErrorMap,
2478
+ });
2479
+ }));
2480
+ }
2481
+ }
2482
+ ZodPromise.create = (schema, params) => {
2483
+ return new ZodPromise({
2484
+ type: schema,
2485
+ typeName: exports.ZodFirstPartyTypeKind.ZodPromise,
2486
+ ...processCreateParams(params),
2487
+ });
2488
+ };
2489
+ class ZodEffects extends ZodType {
2490
+ innerType() {
2491
+ return this._def.schema;
2492
+ }
2493
+ _parse(input) {
2494
+ const { status, ctx } = this._processInputParams(input);
2495
+ const effect = this._def.effect || null;
2496
+ if (effect.type === "preprocess") {
2497
+ const processed = effect.transform(ctx.data);
2498
+ if (ctx.common.async) {
2499
+ return Promise.resolve(processed).then((processed) => {
2500
+ return this._def.schema._parseAsync({
2501
+ data: processed,
2502
+ path: ctx.path,
2503
+ parent: ctx,
2504
+ });
2505
+ });
2506
+ }
2507
+ else {
2508
+ return this._def.schema._parseSync({
2509
+ data: processed,
2510
+ path: ctx.path,
2511
+ parent: ctx,
2512
+ });
2513
+ }
2514
+ }
2515
+ const checkCtx = {
2516
+ addIssue: (arg) => {
2517
+ addIssueToContext(ctx, arg);
2518
+ if (arg.fatal) {
2519
+ status.abort();
2520
+ }
2521
+ else {
2522
+ status.dirty();
2523
+ }
2524
+ },
2525
+ get path() {
2526
+ return ctx.path;
2527
+ },
2528
+ };
2529
+ checkCtx.addIssue = checkCtx.addIssue.bind(checkCtx);
2530
+ if (effect.type === "refinement") {
2531
+ const executeRefinement = (acc
2532
+ // effect: RefinementEffect<any>
2533
+ ) => {
2534
+ const result = effect.refinement(acc, checkCtx);
2535
+ if (ctx.common.async) {
2536
+ return Promise.resolve(result);
2537
+ }
2538
+ if (result instanceof Promise) {
2539
+ throw new Error("Async refinement encountered during synchronous parse operation. Use .parseAsync instead.");
2540
+ }
2541
+ return acc;
2542
+ };
2543
+ if (ctx.common.async === false) {
2544
+ const inner = this._def.schema._parseSync({
2545
+ data: ctx.data,
2546
+ path: ctx.path,
2547
+ parent: ctx,
2548
+ });
2549
+ if (inner.status === "aborted")
2550
+ return INVALID;
2551
+ if (inner.status === "dirty")
2552
+ status.dirty();
2553
+ // return value is ignored
2554
+ executeRefinement(inner.value);
2555
+ return { status: status.value, value: inner.value };
2556
+ }
2557
+ else {
2558
+ return this._def.schema
2559
+ ._parseAsync({ data: ctx.data, path: ctx.path, parent: ctx })
2560
+ .then((inner) => {
2561
+ if (inner.status === "aborted")
2562
+ return INVALID;
2563
+ if (inner.status === "dirty")
2564
+ status.dirty();
2565
+ return executeRefinement(inner.value).then(() => {
2566
+ return { status: status.value, value: inner.value };
2567
+ });
2568
+ });
2569
+ }
2570
+ }
2571
+ if (effect.type === "transform") {
2572
+ if (ctx.common.async === false) {
2573
+ const base = this._def.schema._parseSync({
2574
+ data: ctx.data,
2575
+ path: ctx.path,
2576
+ parent: ctx,
2577
+ });
2578
+ // if (base.status === "aborted") return INVALID;
2579
+ // if (base.status === "dirty") {
2580
+ // return { status: "dirty", value: base.value };
2581
+ // }
2582
+ if (!isValid(base))
2583
+ return base;
2584
+ const result = effect.transform(base.value, checkCtx);
2585
+ if (result instanceof Promise) {
2586
+ throw new Error(`Asynchronous transform encountered during synchronous parse operation. Use .parseAsync instead.`);
2587
+ }
2588
+ return { status: status.value, value: result };
2589
+ }
2590
+ else {
2591
+ return this._def.schema
2592
+ ._parseAsync({ data: ctx.data, path: ctx.path, parent: ctx })
2593
+ .then((base) => {
2594
+ if (!isValid(base))
2595
+ return base;
2596
+ // if (base.status === "aborted") return INVALID;
2597
+ // if (base.status === "dirty") {
2598
+ // return { status: "dirty", value: base.value };
2599
+ // }
2600
+ return Promise.resolve(effect.transform(base.value, checkCtx)).then(OK);
2601
+ });
2602
+ }
2603
+ }
2604
+ util.assertNever(effect);
2605
+ }
2606
+ }
2607
+ ZodEffects.create = (schema, effect, params) => {
2608
+ return new ZodEffects({
2609
+ schema,
2610
+ typeName: exports.ZodFirstPartyTypeKind.ZodEffects,
2611
+ effect,
2612
+ ...processCreateParams(params),
2613
+ });
2614
+ };
2615
+ ZodEffects.createWithPreprocess = (preprocess, schema, params) => {
2616
+ return new ZodEffects({
2617
+ schema,
2618
+ effect: { type: "preprocess", transform: preprocess },
2619
+ typeName: exports.ZodFirstPartyTypeKind.ZodEffects,
2620
+ ...processCreateParams(params),
2621
+ });
2622
+ };
2623
+ class ZodOptional extends ZodType {
2624
+ _parse(input) {
2625
+ const parsedType = this._getType(input);
2626
+ if (parsedType === ZodParsedType.undefined) {
2627
+ return OK(undefined);
2628
+ }
2629
+ return this._def.innerType._parse(input);
2630
+ }
2631
+ unwrap() {
2632
+ return this._def.innerType;
2633
+ }
2634
+ }
2635
+ ZodOptional.create = (type, params) => {
2636
+ return new ZodOptional({
2637
+ innerType: type,
2638
+ typeName: exports.ZodFirstPartyTypeKind.ZodOptional,
2639
+ ...processCreateParams(params),
2640
+ });
2641
+ };
2642
+ class ZodNullable extends ZodType {
2643
+ _parse(input) {
2644
+ const parsedType = this._getType(input);
2645
+ if (parsedType === ZodParsedType.null) {
2646
+ return OK(null);
2647
+ }
2648
+ return this._def.innerType._parse(input);
2649
+ }
2650
+ unwrap() {
2651
+ return this._def.innerType;
2652
+ }
2653
+ }
2654
+ ZodNullable.create = (type, params) => {
2655
+ return new ZodNullable({
2656
+ innerType: type,
2657
+ typeName: exports.ZodFirstPartyTypeKind.ZodNullable,
2658
+ ...processCreateParams(params),
2659
+ });
2660
+ };
2661
+ class ZodDefault extends ZodType {
2662
+ _parse(input) {
2663
+ const { ctx } = this._processInputParams(input);
2664
+ let data = ctx.data;
2665
+ if (ctx.parsedType === ZodParsedType.undefined) {
2666
+ data = this._def.defaultValue();
2667
+ }
2668
+ return this._def.innerType._parse({
2669
+ data,
2670
+ path: ctx.path,
2671
+ parent: ctx,
2672
+ });
2673
+ }
2674
+ removeDefault() {
2675
+ return this._def.innerType;
2676
+ }
2677
+ }
2678
+ ZodDefault.create = (type, params) => {
2679
+ return new ZodOptional({
2680
+ innerType: type,
2681
+ typeName: exports.ZodFirstPartyTypeKind.ZodOptional,
2682
+ ...processCreateParams(params),
2683
+ });
2684
+ };
2685
+ class ZodNaN extends ZodType {
2686
+ _parse(input) {
2687
+ const parsedType = this._getType(input);
2688
+ if (parsedType !== ZodParsedType.nan) {
2689
+ const ctx = this._getOrReturnCtx(input);
2690
+ addIssueToContext(ctx, {
2691
+ code: ZodIssueCode.invalid_type,
2692
+ expected: ZodParsedType.nan,
2693
+ received: ctx.parsedType,
2694
+ });
2695
+ return INVALID;
2696
+ }
2697
+ return { status: "valid", value: input.data };
2698
+ }
2699
+ }
2700
+ ZodNaN.create = (params) => {
2701
+ return new ZodNaN({
2702
+ typeName: exports.ZodFirstPartyTypeKind.ZodNaN,
2703
+ ...processCreateParams(params),
2704
+ });
2705
+ };
2706
+ const custom = (check, params) => {
2707
+ if (check)
2708
+ return ZodAny.create().refine(check, params);
2709
+ return ZodAny.create();
2710
+ };
2711
+ const late = {
2712
+ object: ZodObject.lazycreate,
2713
+ };
2714
+ exports.ZodFirstPartyTypeKind = void 0;
2715
+ (function (ZodFirstPartyTypeKind) {
2716
+ ZodFirstPartyTypeKind["ZodString"] = "ZodString";
2717
+ ZodFirstPartyTypeKind["ZodNumber"] = "ZodNumber";
2718
+ ZodFirstPartyTypeKind["ZodNaN"] = "ZodNaN";
2719
+ ZodFirstPartyTypeKind["ZodBigInt"] = "ZodBigInt";
2720
+ ZodFirstPartyTypeKind["ZodBoolean"] = "ZodBoolean";
2721
+ ZodFirstPartyTypeKind["ZodDate"] = "ZodDate";
2722
+ ZodFirstPartyTypeKind["ZodUndefined"] = "ZodUndefined";
2723
+ ZodFirstPartyTypeKind["ZodNull"] = "ZodNull";
2724
+ ZodFirstPartyTypeKind["ZodAny"] = "ZodAny";
2725
+ ZodFirstPartyTypeKind["ZodUnknown"] = "ZodUnknown";
2726
+ ZodFirstPartyTypeKind["ZodNever"] = "ZodNever";
2727
+ ZodFirstPartyTypeKind["ZodVoid"] = "ZodVoid";
2728
+ ZodFirstPartyTypeKind["ZodArray"] = "ZodArray";
2729
+ ZodFirstPartyTypeKind["ZodObject"] = "ZodObject";
2730
+ ZodFirstPartyTypeKind["ZodUnion"] = "ZodUnion";
2731
+ ZodFirstPartyTypeKind["ZodDiscriminatedUnion"] = "ZodDiscriminatedUnion";
2732
+ ZodFirstPartyTypeKind["ZodIntersection"] = "ZodIntersection";
2733
+ ZodFirstPartyTypeKind["ZodTuple"] = "ZodTuple";
2734
+ ZodFirstPartyTypeKind["ZodRecord"] = "ZodRecord";
2735
+ ZodFirstPartyTypeKind["ZodMap"] = "ZodMap";
2736
+ ZodFirstPartyTypeKind["ZodSet"] = "ZodSet";
2737
+ ZodFirstPartyTypeKind["ZodFunction"] = "ZodFunction";
2738
+ ZodFirstPartyTypeKind["ZodLazy"] = "ZodLazy";
2739
+ ZodFirstPartyTypeKind["ZodLiteral"] = "ZodLiteral";
2740
+ ZodFirstPartyTypeKind["ZodEnum"] = "ZodEnum";
2741
+ ZodFirstPartyTypeKind["ZodEffects"] = "ZodEffects";
2742
+ ZodFirstPartyTypeKind["ZodNativeEnum"] = "ZodNativeEnum";
2743
+ ZodFirstPartyTypeKind["ZodOptional"] = "ZodOptional";
2744
+ ZodFirstPartyTypeKind["ZodNullable"] = "ZodNullable";
2745
+ ZodFirstPartyTypeKind["ZodDefault"] = "ZodDefault";
2746
+ ZodFirstPartyTypeKind["ZodPromise"] = "ZodPromise";
2747
+ })(exports.ZodFirstPartyTypeKind || (exports.ZodFirstPartyTypeKind = {}));
2748
+ const instanceOfType = (cls, params = {
2749
+ message: `Input not instance of ${cls.name}`,
2750
+ }) => custom((data) => data instanceof cls, params);
2751
+ const stringType = ZodString.create;
2752
+ const numberType = ZodNumber.create;
2753
+ const nanType = ZodNaN.create;
2754
+ const bigIntType = ZodBigInt.create;
2755
+ const booleanType = ZodBoolean.create;
2756
+ const dateType = ZodDate.create;
2757
+ const undefinedType = ZodUndefined.create;
2758
+ const nullType = ZodNull.create;
2759
+ const anyType = ZodAny.create;
2760
+ const unknownType = ZodUnknown.create;
2761
+ const neverType = ZodNever.create;
2762
+ const voidType = ZodVoid.create;
2763
+ const arrayType = ZodArray.create;
2764
+ const objectType = ZodObject.create;
2765
+ const strictObjectType = ZodObject.strictCreate;
2766
+ const unionType = ZodUnion.create;
2767
+ const discriminatedUnionType = ZodDiscriminatedUnion.create;
2768
+ const intersectionType = ZodIntersection.create;
2769
+ const tupleType = ZodTuple.create;
2770
+ const recordType = ZodRecord.create;
2771
+ const mapType = ZodMap.create;
2772
+ const setType = ZodSet.create;
2773
+ const functionType = ZodFunction.create;
2774
+ const lazyType = ZodLazy.create;
2775
+ const literalType = ZodLiteral.create;
2776
+ const enumType = ZodEnum.create;
2777
+ const nativeEnumType = ZodNativeEnum.create;
2778
+ const promiseType = ZodPromise.create;
2779
+ const effectsType = ZodEffects.create;
2780
+ const optionalType = ZodOptional.create;
2781
+ const nullableType = ZodNullable.create;
2782
+ const preprocessType = ZodEffects.createWithPreprocess;
2783
+ const ostring = () => stringType().optional();
2784
+ const onumber = () => numberType().optional();
2785
+ const oboolean = () => booleanType().optional();
2786
+
2787
+ var mod = /*#__PURE__*/Object.freeze({
2788
+ __proto__: null,
2789
+ ZodParsedType: ZodParsedType,
2790
+ getParsedType: getParsedType,
2791
+ makeIssue: makeIssue,
2792
+ EMPTY_PATH: EMPTY_PATH,
2793
+ addIssueToContext: addIssueToContext,
2794
+ ParseStatus: ParseStatus,
2795
+ INVALID: INVALID,
2796
+ DIRTY: DIRTY,
2797
+ OK: OK,
2798
+ isAborted: isAborted,
2799
+ isDirty: isDirty,
2800
+ isValid: isValid,
2801
+ isAsync: isAsync,
2802
+ ZodType: ZodType,
2803
+ ZodString: ZodString,
2804
+ ZodNumber: ZodNumber,
2805
+ ZodBigInt: ZodBigInt,
2806
+ ZodBoolean: ZodBoolean,
2807
+ ZodDate: ZodDate,
2808
+ ZodUndefined: ZodUndefined,
2809
+ ZodNull: ZodNull,
2810
+ ZodAny: ZodAny,
2811
+ ZodUnknown: ZodUnknown,
2812
+ ZodNever: ZodNever,
2813
+ ZodVoid: ZodVoid,
2814
+ ZodArray: ZodArray,
2815
+ get objectUtil () { return exports.objectUtil; },
2816
+ ZodObject: ZodObject,
2817
+ ZodUnion: ZodUnion,
2818
+ ZodDiscriminatedUnion: ZodDiscriminatedUnion,
2819
+ ZodIntersection: ZodIntersection,
2820
+ ZodTuple: ZodTuple,
2821
+ ZodRecord: ZodRecord,
2822
+ ZodMap: ZodMap,
2823
+ ZodSet: ZodSet,
2824
+ ZodFunction: ZodFunction,
2825
+ ZodLazy: ZodLazy,
2826
+ ZodLiteral: ZodLiteral,
2827
+ ZodEnum: ZodEnum,
2828
+ ZodNativeEnum: ZodNativeEnum,
2829
+ ZodPromise: ZodPromise,
2830
+ ZodEffects: ZodEffects,
2831
+ ZodTransformer: ZodEffects,
2832
+ ZodOptional: ZodOptional,
2833
+ ZodNullable: ZodNullable,
2834
+ ZodDefault: ZodDefault,
2835
+ ZodNaN: ZodNaN,
2836
+ custom: custom,
2837
+ Schema: ZodType,
2838
+ ZodSchema: ZodType,
2839
+ late: late,
2840
+ get ZodFirstPartyTypeKind () { return exports.ZodFirstPartyTypeKind; },
2841
+ any: anyType,
2842
+ array: arrayType,
2843
+ bigint: bigIntType,
2844
+ boolean: booleanType,
2845
+ date: dateType,
2846
+ discriminatedUnion: discriminatedUnionType,
2847
+ effect: effectsType,
2848
+ 'enum': enumType,
2849
+ 'function': functionType,
2850
+ 'instanceof': instanceOfType,
2851
+ intersection: intersectionType,
2852
+ lazy: lazyType,
2853
+ literal: literalType,
2854
+ map: mapType,
2855
+ nan: nanType,
2856
+ nativeEnum: nativeEnumType,
2857
+ never: neverType,
2858
+ 'null': nullType,
2859
+ nullable: nullableType,
2860
+ number: numberType,
2861
+ object: objectType,
2862
+ oboolean: oboolean,
2863
+ onumber: onumber,
2864
+ optional: optionalType,
2865
+ ostring: ostring,
2866
+ preprocess: preprocessType,
2867
+ promise: promiseType,
2868
+ record: recordType,
2869
+ set: setType,
2870
+ strictObject: strictObjectType,
2871
+ string: stringType,
2872
+ transformer: effectsType,
2873
+ tuple: tupleType,
2874
+ 'undefined': undefinedType,
2875
+ union: unionType,
2876
+ unknown: unknownType,
2877
+ 'void': voidType,
2878
+ ZodIssueCode: ZodIssueCode,
2879
+ quotelessJson: quotelessJson,
2880
+ ZodError: ZodError,
2881
+ defaultErrorMap: defaultErrorMap,
2882
+ get overrideErrorMap () { return exports.overrideErrorMap; },
2883
+ setErrorMap: setErrorMap
2884
+ });
2885
+
2886
+ exports.DIRTY = DIRTY;
2887
+ exports.EMPTY_PATH = EMPTY_PATH;
2888
+ exports.INVALID = INVALID;
2889
+ exports.OK = OK;
2890
+ exports.ParseStatus = ParseStatus;
2891
+ exports.Schema = ZodType;
2892
+ exports.ZodAny = ZodAny;
2893
+ exports.ZodArray = ZodArray;
2894
+ exports.ZodBigInt = ZodBigInt;
2895
+ exports.ZodBoolean = ZodBoolean;
2896
+ exports.ZodDate = ZodDate;
2897
+ exports.ZodDefault = ZodDefault;
2898
+ exports.ZodDiscriminatedUnion = ZodDiscriminatedUnion;
2899
+ exports.ZodEffects = ZodEffects;
2900
+ exports.ZodEnum = ZodEnum;
2901
+ exports.ZodError = ZodError;
2902
+ exports.ZodFunction = ZodFunction;
2903
+ exports.ZodIntersection = ZodIntersection;
2904
+ exports.ZodIssueCode = ZodIssueCode;
2905
+ exports.ZodLazy = ZodLazy;
2906
+ exports.ZodLiteral = ZodLiteral;
2907
+ exports.ZodMap = ZodMap;
2908
+ exports.ZodNaN = ZodNaN;
2909
+ exports.ZodNativeEnum = ZodNativeEnum;
2910
+ exports.ZodNever = ZodNever;
2911
+ exports.ZodNull = ZodNull;
2912
+ exports.ZodNullable = ZodNullable;
2913
+ exports.ZodNumber = ZodNumber;
2914
+ exports.ZodObject = ZodObject;
2915
+ exports.ZodOptional = ZodOptional;
2916
+ exports.ZodParsedType = ZodParsedType;
2917
+ exports.ZodPromise = ZodPromise;
2918
+ exports.ZodRecord = ZodRecord;
2919
+ exports.ZodSchema = ZodType;
2920
+ exports.ZodSet = ZodSet;
2921
+ exports.ZodString = ZodString;
2922
+ exports.ZodTransformer = ZodEffects;
2923
+ exports.ZodTuple = ZodTuple;
2924
+ exports.ZodType = ZodType;
2925
+ exports.ZodUndefined = ZodUndefined;
2926
+ exports.ZodUnion = ZodUnion;
2927
+ exports.ZodUnknown = ZodUnknown;
2928
+ exports.ZodVoid = ZodVoid;
2929
+ exports.addIssueToContext = addIssueToContext;
2930
+ exports.any = anyType;
2931
+ exports.array = arrayType;
2932
+ exports.bigint = bigIntType;
2933
+ exports.boolean = booleanType;
2934
+ exports.custom = custom;
2935
+ exports.date = dateType;
2936
+ exports["default"] = mod;
2937
+ exports.defaultErrorMap = defaultErrorMap;
2938
+ exports.discriminatedUnion = discriminatedUnionType;
2939
+ exports.effect = effectsType;
2940
+ exports["enum"] = enumType;
2941
+ exports["function"] = functionType;
2942
+ exports.getParsedType = getParsedType;
2943
+ exports["instanceof"] = instanceOfType;
2944
+ exports.intersection = intersectionType;
2945
+ exports.isAborted = isAborted;
2946
+ exports.isAsync = isAsync;
2947
+ exports.isDirty = isDirty;
2948
+ exports.isValid = isValid;
2949
+ exports.late = late;
2950
+ exports.lazy = lazyType;
2951
+ exports.literal = literalType;
2952
+ exports.makeIssue = makeIssue;
2953
+ exports.map = mapType;
2954
+ exports.nan = nanType;
2955
+ exports.nativeEnum = nativeEnumType;
2956
+ exports.never = neverType;
2957
+ exports["null"] = nullType;
2958
+ exports.nullable = nullableType;
2959
+ exports.number = numberType;
2960
+ exports.object = objectType;
2961
+ exports.oboolean = oboolean;
2962
+ exports.onumber = onumber;
2963
+ exports.optional = optionalType;
2964
+ exports.ostring = ostring;
2965
+ exports.preprocess = preprocessType;
2966
+ exports.promise = promiseType;
2967
+ exports.quotelessJson = quotelessJson;
2968
+ exports.record = recordType;
2969
+ exports.set = setType;
2970
+ exports.setErrorMap = setErrorMap;
2971
+ exports.strictObject = strictObjectType;
2972
+ exports.string = stringType;
2973
+ exports.transformer = effectsType;
2974
+ exports.tuple = tupleType;
2975
+ exports["undefined"] = undefinedType;
2976
+ exports.union = unionType;
2977
+ exports.unknown = unknownType;
2978
+ exports["void"] = voidType;
2979
+ exports.z = mod;
2980
+
2981
+ Object.defineProperty(exports, '__esModule', { value: true });
2982
+
2983
+ }));