zod 3.14.0 → 3.14.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.
- package/lib/benchmarks/index.js +2 -0
- package/lib/benchmarks/primitives.d.ts +5 -0
- package/lib/benchmarks/primitives.js +78 -0
- package/lib/helpers/parseUtil.d.ts +6 -3
- package/lib/helpers/parseUtil.js +2 -2
- package/lib/index.mjs +152 -99
- package/lib/types.d.ts +2 -0
- package/lib/types.js +150 -97
- package/package.json +1 -1
package/lib/types.js
CHANGED
|
@@ -11,10 +11,10 @@ const handleResult = (ctx, result) => {
|
|
|
11
11
|
return { success: true, data: result.value };
|
|
12
12
|
}
|
|
13
13
|
else {
|
|
14
|
-
if (!ctx.issues.length) {
|
|
14
|
+
if (!ctx.common.issues.length) {
|
|
15
15
|
throw new Error("Validation failed but no issues detected.");
|
|
16
16
|
}
|
|
17
|
-
const error = new ZodError_1.ZodError(ctx.issues);
|
|
17
|
+
const error = new ZodError_1.ZodError(ctx.common.issues);
|
|
18
18
|
return { success: false, error };
|
|
19
19
|
}
|
|
20
20
|
};
|
|
@@ -68,11 +68,24 @@ class ZodType {
|
|
|
68
68
|
get description() {
|
|
69
69
|
return this._def.description;
|
|
70
70
|
}
|
|
71
|
+
_getType(input) {
|
|
72
|
+
return (0, parseUtil_1.getParsedType)(input.data);
|
|
73
|
+
}
|
|
74
|
+
_getOrReturnCtx(input, ctx) {
|
|
75
|
+
return (ctx || {
|
|
76
|
+
common: input.parent.common,
|
|
77
|
+
data: input.data,
|
|
78
|
+
parsedType: (0, parseUtil_1.getParsedType)(input.data),
|
|
79
|
+
schemaErrorMap: this._def.errorMap,
|
|
80
|
+
path: input.path,
|
|
81
|
+
parent: input.parent,
|
|
82
|
+
});
|
|
83
|
+
}
|
|
71
84
|
_processInputParams(input) {
|
|
72
85
|
return {
|
|
73
86
|
status: new parseUtil_1.ParseStatus(),
|
|
74
87
|
ctx: {
|
|
75
|
-
|
|
88
|
+
common: input.parent.common,
|
|
76
89
|
data: input.data,
|
|
77
90
|
parsedType: (0, parseUtil_1.getParsedType)(input.data),
|
|
78
91
|
schemaErrorMap: this._def.errorMap,
|
|
@@ -101,11 +114,14 @@ class ZodType {
|
|
|
101
114
|
safeParse(data, params) {
|
|
102
115
|
var _a;
|
|
103
116
|
const ctx = {
|
|
117
|
+
common: {
|
|
118
|
+
issues: [],
|
|
119
|
+
async: (_a = params === null || params === void 0 ? void 0 : params.async) !== null && _a !== void 0 ? _a : false,
|
|
120
|
+
typeCache: typeof Map !== "undefined" ? new Map() : undefined,
|
|
121
|
+
contextualErrorMap: params === null || params === void 0 ? void 0 : params.errorMap,
|
|
122
|
+
},
|
|
104
123
|
path: (params === null || params === void 0 ? void 0 : params.path) || [],
|
|
105
|
-
issues: [],
|
|
106
|
-
contextualErrorMap: params === null || params === void 0 ? void 0 : params.errorMap,
|
|
107
124
|
schemaErrorMap: this._def.errorMap,
|
|
108
|
-
async: (_a = params === null || params === void 0 ? void 0 : params.async) !== null && _a !== void 0 ? _a : false,
|
|
109
125
|
parent: null,
|
|
110
126
|
data,
|
|
111
127
|
parsedType: (0, parseUtil_1.getParsedType)(data),
|
|
@@ -121,11 +137,14 @@ class ZodType {
|
|
|
121
137
|
}
|
|
122
138
|
async safeParseAsync(data, params) {
|
|
123
139
|
const ctx = {
|
|
140
|
+
common: {
|
|
141
|
+
issues: [],
|
|
142
|
+
contextualErrorMap: params === null || params === void 0 ? void 0 : params.errorMap,
|
|
143
|
+
async: true,
|
|
144
|
+
typeCache: typeof Map !== "undefined" ? new Map() : undefined,
|
|
145
|
+
},
|
|
124
146
|
path: (params === null || params === void 0 ? void 0 : params.path) || [],
|
|
125
|
-
issues: [],
|
|
126
|
-
contextualErrorMap: params === null || params === void 0 ? void 0 : params.errorMap,
|
|
127
147
|
schemaErrorMap: this._def.errorMap,
|
|
128
|
-
async: true,
|
|
129
148
|
parent: null,
|
|
130
149
|
data,
|
|
131
150
|
parsedType: (0, parseUtil_1.getParsedType)(data),
|
|
@@ -269,8 +288,9 @@ class ZodString extends ZodType {
|
|
|
269
288
|
this.nonempty = (message) => this.min(1, errorUtil_1.errorUtil.errToObj(message));
|
|
270
289
|
}
|
|
271
290
|
_parse(input) {
|
|
272
|
-
const
|
|
273
|
-
if (
|
|
291
|
+
const parsedType = this._getType(input);
|
|
292
|
+
if (parsedType !== parseUtil_1.ZodParsedType.string) {
|
|
293
|
+
const ctx = this._getOrReturnCtx(input);
|
|
274
294
|
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
275
295
|
code: ZodError_1.ZodIssueCode.invalid_type,
|
|
276
296
|
expected: parseUtil_1.ZodParsedType.string,
|
|
@@ -280,9 +300,12 @@ class ZodString extends ZodType {
|
|
|
280
300
|
);
|
|
281
301
|
return parseUtil_1.INVALID;
|
|
282
302
|
}
|
|
303
|
+
const status = new parseUtil_1.ParseStatus();
|
|
304
|
+
let ctx = undefined;
|
|
283
305
|
for (const check of this._def.checks) {
|
|
284
306
|
if (check.kind === "min") {
|
|
285
|
-
if (
|
|
307
|
+
if (input.data.length < check.value) {
|
|
308
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
286
309
|
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
287
310
|
code: ZodError_1.ZodIssueCode.too_small,
|
|
288
311
|
minimum: check.value,
|
|
@@ -294,7 +317,8 @@ class ZodString extends ZodType {
|
|
|
294
317
|
}
|
|
295
318
|
}
|
|
296
319
|
else if (check.kind === "max") {
|
|
297
|
-
if (
|
|
320
|
+
if (input.data.length > check.value) {
|
|
321
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
298
322
|
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
299
323
|
code: ZodError_1.ZodIssueCode.too_big,
|
|
300
324
|
maximum: check.value,
|
|
@@ -306,7 +330,8 @@ class ZodString extends ZodType {
|
|
|
306
330
|
}
|
|
307
331
|
}
|
|
308
332
|
else if (check.kind === "email") {
|
|
309
|
-
if (!emailRegex.test(
|
|
333
|
+
if (!emailRegex.test(input.data)) {
|
|
334
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
310
335
|
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
311
336
|
validation: "email",
|
|
312
337
|
code: ZodError_1.ZodIssueCode.invalid_string,
|
|
@@ -316,7 +341,8 @@ class ZodString extends ZodType {
|
|
|
316
341
|
}
|
|
317
342
|
}
|
|
318
343
|
else if (check.kind === "uuid") {
|
|
319
|
-
if (!uuidRegex.test(
|
|
344
|
+
if (!uuidRegex.test(input.data)) {
|
|
345
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
320
346
|
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
321
347
|
validation: "uuid",
|
|
322
348
|
code: ZodError_1.ZodIssueCode.invalid_string,
|
|
@@ -326,7 +352,8 @@ class ZodString extends ZodType {
|
|
|
326
352
|
}
|
|
327
353
|
}
|
|
328
354
|
else if (check.kind === "cuid") {
|
|
329
|
-
if (!cuidRegex.test(
|
|
355
|
+
if (!cuidRegex.test(input.data)) {
|
|
356
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
330
357
|
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
331
358
|
validation: "cuid",
|
|
332
359
|
code: ZodError_1.ZodIssueCode.invalid_string,
|
|
@@ -337,9 +364,10 @@ class ZodString extends ZodType {
|
|
|
337
364
|
}
|
|
338
365
|
else if (check.kind === "url") {
|
|
339
366
|
try {
|
|
340
|
-
new URL(
|
|
367
|
+
new URL(input.data);
|
|
341
368
|
}
|
|
342
369
|
catch (_a) {
|
|
370
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
343
371
|
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
344
372
|
validation: "url",
|
|
345
373
|
code: ZodError_1.ZodIssueCode.invalid_string,
|
|
@@ -350,8 +378,9 @@ class ZodString extends ZodType {
|
|
|
350
378
|
}
|
|
351
379
|
else if (check.kind === "regex") {
|
|
352
380
|
check.regex.lastIndex = 0;
|
|
353
|
-
const testResult = check.regex.test(
|
|
381
|
+
const testResult = check.regex.test(input.data);
|
|
354
382
|
if (!testResult) {
|
|
383
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
355
384
|
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
356
385
|
validation: "regex",
|
|
357
386
|
code: ZodError_1.ZodIssueCode.invalid_string,
|
|
@@ -361,7 +390,7 @@ class ZodString extends ZodType {
|
|
|
361
390
|
}
|
|
362
391
|
}
|
|
363
392
|
}
|
|
364
|
-
return { status: status.value, value:
|
|
393
|
+
return { status: status.value, value: input.data };
|
|
365
394
|
}
|
|
366
395
|
_addCheck(check) {
|
|
367
396
|
return new ZodString({
|
|
@@ -465,8 +494,9 @@ class ZodNumber extends ZodType {
|
|
|
465
494
|
this.step = this.multipleOf;
|
|
466
495
|
}
|
|
467
496
|
_parse(input) {
|
|
468
|
-
const
|
|
469
|
-
if (
|
|
497
|
+
const parsedType = this._getType(input);
|
|
498
|
+
if (parsedType !== parseUtil_1.ZodParsedType.number) {
|
|
499
|
+
const ctx = this._getOrReturnCtx(input);
|
|
470
500
|
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
471
501
|
code: ZodError_1.ZodIssueCode.invalid_type,
|
|
472
502
|
expected: parseUtil_1.ZodParsedType.number,
|
|
@@ -474,9 +504,12 @@ class ZodNumber extends ZodType {
|
|
|
474
504
|
});
|
|
475
505
|
return parseUtil_1.INVALID;
|
|
476
506
|
}
|
|
507
|
+
let ctx = undefined;
|
|
508
|
+
const status = new parseUtil_1.ParseStatus();
|
|
477
509
|
for (const check of this._def.checks) {
|
|
478
510
|
if (check.kind === "int") {
|
|
479
|
-
if (!util_1.util.isInteger(
|
|
511
|
+
if (!util_1.util.isInteger(input.data)) {
|
|
512
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
480
513
|
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
481
514
|
code: ZodError_1.ZodIssueCode.invalid_type,
|
|
482
515
|
expected: "integer",
|
|
@@ -488,9 +521,10 @@ class ZodNumber extends ZodType {
|
|
|
488
521
|
}
|
|
489
522
|
else if (check.kind === "min") {
|
|
490
523
|
const tooSmall = check.inclusive
|
|
491
|
-
?
|
|
492
|
-
:
|
|
524
|
+
? input.data < check.value
|
|
525
|
+
: input.data <= check.value;
|
|
493
526
|
if (tooSmall) {
|
|
527
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
494
528
|
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
495
529
|
code: ZodError_1.ZodIssueCode.too_small,
|
|
496
530
|
minimum: check.value,
|
|
@@ -503,9 +537,10 @@ class ZodNumber extends ZodType {
|
|
|
503
537
|
}
|
|
504
538
|
else if (check.kind === "max") {
|
|
505
539
|
const tooBig = check.inclusive
|
|
506
|
-
?
|
|
507
|
-
:
|
|
540
|
+
? input.data > check.value
|
|
541
|
+
: input.data >= check.value;
|
|
508
542
|
if (tooBig) {
|
|
543
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
509
544
|
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
510
545
|
code: ZodError_1.ZodIssueCode.too_big,
|
|
511
546
|
maximum: check.value,
|
|
@@ -517,7 +552,8 @@ class ZodNumber extends ZodType {
|
|
|
517
552
|
}
|
|
518
553
|
}
|
|
519
554
|
else if (check.kind === "multipleOf") {
|
|
520
|
-
if (floatSafeRemainder(
|
|
555
|
+
if (floatSafeRemainder(input.data, check.value) !== 0) {
|
|
556
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
521
557
|
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
522
558
|
code: ZodError_1.ZodIssueCode.not_multiple_of,
|
|
523
559
|
multipleOf: check.value,
|
|
@@ -530,7 +566,7 @@ class ZodNumber extends ZodType {
|
|
|
530
566
|
util_1.util.assertNever(check);
|
|
531
567
|
}
|
|
532
568
|
}
|
|
533
|
-
return { status: status.value, value:
|
|
569
|
+
return { status: status.value, value: input.data };
|
|
534
570
|
}
|
|
535
571
|
gte(value, message) {
|
|
536
572
|
return this.setLimit("min", value, true, errorUtil_1.errorUtil.toString(message));
|
|
@@ -643,8 +679,9 @@ ZodNumber.create = (params) => {
|
|
|
643
679
|
};
|
|
644
680
|
class ZodBigInt extends ZodType {
|
|
645
681
|
_parse(input) {
|
|
646
|
-
const
|
|
647
|
-
if (
|
|
682
|
+
const parsedType = this._getType(input);
|
|
683
|
+
if (parsedType !== parseUtil_1.ZodParsedType.bigint) {
|
|
684
|
+
const ctx = this._getOrReturnCtx(input);
|
|
648
685
|
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
649
686
|
code: ZodError_1.ZodIssueCode.invalid_type,
|
|
650
687
|
expected: parseUtil_1.ZodParsedType.bigint,
|
|
@@ -652,7 +689,7 @@ class ZodBigInt extends ZodType {
|
|
|
652
689
|
});
|
|
653
690
|
return parseUtil_1.INVALID;
|
|
654
691
|
}
|
|
655
|
-
return (0, parseUtil_1.OK)(
|
|
692
|
+
return (0, parseUtil_1.OK)(input.data);
|
|
656
693
|
}
|
|
657
694
|
}
|
|
658
695
|
exports.ZodBigInt = ZodBigInt;
|
|
@@ -664,8 +701,9 @@ ZodBigInt.create = (params) => {
|
|
|
664
701
|
};
|
|
665
702
|
class ZodBoolean extends ZodType {
|
|
666
703
|
_parse(input) {
|
|
667
|
-
const
|
|
668
|
-
if (
|
|
704
|
+
const parsedType = this._getType(input);
|
|
705
|
+
if (parsedType !== parseUtil_1.ZodParsedType.boolean) {
|
|
706
|
+
const ctx = this._getOrReturnCtx(input);
|
|
669
707
|
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
670
708
|
code: ZodError_1.ZodIssueCode.invalid_type,
|
|
671
709
|
expected: parseUtil_1.ZodParsedType.boolean,
|
|
@@ -673,7 +711,7 @@ class ZodBoolean extends ZodType {
|
|
|
673
711
|
});
|
|
674
712
|
return parseUtil_1.INVALID;
|
|
675
713
|
}
|
|
676
|
-
return (0, parseUtil_1.OK)(
|
|
714
|
+
return (0, parseUtil_1.OK)(input.data);
|
|
677
715
|
}
|
|
678
716
|
}
|
|
679
717
|
exports.ZodBoolean = ZodBoolean;
|
|
@@ -685,8 +723,9 @@ ZodBoolean.create = (params) => {
|
|
|
685
723
|
};
|
|
686
724
|
class ZodDate extends ZodType {
|
|
687
725
|
_parse(input) {
|
|
688
|
-
const
|
|
689
|
-
if (
|
|
726
|
+
const parsedType = this._getType(input);
|
|
727
|
+
if (parsedType !== parseUtil_1.ZodParsedType.date) {
|
|
728
|
+
const ctx = this._getOrReturnCtx(input);
|
|
690
729
|
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
691
730
|
code: ZodError_1.ZodIssueCode.invalid_type,
|
|
692
731
|
expected: parseUtil_1.ZodParsedType.date,
|
|
@@ -694,15 +733,16 @@ class ZodDate extends ZodType {
|
|
|
694
733
|
});
|
|
695
734
|
return parseUtil_1.INVALID;
|
|
696
735
|
}
|
|
697
|
-
if (isNaN(
|
|
736
|
+
if (isNaN(input.data.getTime())) {
|
|
737
|
+
const ctx = this._getOrReturnCtx(input);
|
|
698
738
|
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
699
739
|
code: ZodError_1.ZodIssueCode.invalid_date,
|
|
700
740
|
});
|
|
701
741
|
return parseUtil_1.INVALID;
|
|
702
742
|
}
|
|
703
743
|
return {
|
|
704
|
-
status:
|
|
705
|
-
value: new Date(
|
|
744
|
+
status: "valid",
|
|
745
|
+
value: new Date(input.data.getTime()),
|
|
706
746
|
};
|
|
707
747
|
}
|
|
708
748
|
}
|
|
@@ -715,8 +755,9 @@ ZodDate.create = (params) => {
|
|
|
715
755
|
};
|
|
716
756
|
class ZodUndefined extends ZodType {
|
|
717
757
|
_parse(input) {
|
|
718
|
-
const
|
|
719
|
-
if (
|
|
758
|
+
const parsedType = this._getType(input);
|
|
759
|
+
if (parsedType !== parseUtil_1.ZodParsedType.undefined) {
|
|
760
|
+
const ctx = this._getOrReturnCtx(input);
|
|
720
761
|
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
721
762
|
code: ZodError_1.ZodIssueCode.invalid_type,
|
|
722
763
|
expected: parseUtil_1.ZodParsedType.undefined,
|
|
@@ -724,7 +765,7 @@ class ZodUndefined extends ZodType {
|
|
|
724
765
|
});
|
|
725
766
|
return parseUtil_1.INVALID;
|
|
726
767
|
}
|
|
727
|
-
return (0, parseUtil_1.OK)(
|
|
768
|
+
return (0, parseUtil_1.OK)(input.data);
|
|
728
769
|
}
|
|
729
770
|
}
|
|
730
771
|
exports.ZodUndefined = ZodUndefined;
|
|
@@ -736,8 +777,9 @@ ZodUndefined.create = (params) => {
|
|
|
736
777
|
};
|
|
737
778
|
class ZodNull extends ZodType {
|
|
738
779
|
_parse(input) {
|
|
739
|
-
const
|
|
740
|
-
if (
|
|
780
|
+
const parsedType = this._getType(input);
|
|
781
|
+
if (parsedType !== parseUtil_1.ZodParsedType.null) {
|
|
782
|
+
const ctx = this._getOrReturnCtx(input);
|
|
741
783
|
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
742
784
|
code: ZodError_1.ZodIssueCode.invalid_type,
|
|
743
785
|
expected: parseUtil_1.ZodParsedType.null,
|
|
@@ -745,7 +787,7 @@ class ZodNull extends ZodType {
|
|
|
745
787
|
});
|
|
746
788
|
return parseUtil_1.INVALID;
|
|
747
789
|
}
|
|
748
|
-
return (0, parseUtil_1.OK)(
|
|
790
|
+
return (0, parseUtil_1.OK)(input.data);
|
|
749
791
|
}
|
|
750
792
|
}
|
|
751
793
|
exports.ZodNull = ZodNull;
|
|
@@ -762,8 +804,7 @@ class ZodAny extends ZodType {
|
|
|
762
804
|
this._any = true;
|
|
763
805
|
}
|
|
764
806
|
_parse(input) {
|
|
765
|
-
|
|
766
|
-
return (0, parseUtil_1.OK)(ctx.data);
|
|
807
|
+
return (0, parseUtil_1.OK)(input.data);
|
|
767
808
|
}
|
|
768
809
|
}
|
|
769
810
|
exports.ZodAny = ZodAny;
|
|
@@ -780,8 +821,7 @@ class ZodUnknown extends ZodType {
|
|
|
780
821
|
this._unknown = true;
|
|
781
822
|
}
|
|
782
823
|
_parse(input) {
|
|
783
|
-
|
|
784
|
-
return (0, parseUtil_1.OK)(ctx.data);
|
|
824
|
+
return (0, parseUtil_1.OK)(input.data);
|
|
785
825
|
}
|
|
786
826
|
}
|
|
787
827
|
exports.ZodUnknown = ZodUnknown;
|
|
@@ -793,7 +833,7 @@ ZodUnknown.create = (params) => {
|
|
|
793
833
|
};
|
|
794
834
|
class ZodNever extends ZodType {
|
|
795
835
|
_parse(input) {
|
|
796
|
-
const
|
|
836
|
+
const ctx = this._getOrReturnCtx(input);
|
|
797
837
|
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
798
838
|
code: ZodError_1.ZodIssueCode.invalid_type,
|
|
799
839
|
expected: parseUtil_1.ZodParsedType.never,
|
|
@@ -811,8 +851,9 @@ ZodNever.create = (params) => {
|
|
|
811
851
|
};
|
|
812
852
|
class ZodVoid extends ZodType {
|
|
813
853
|
_parse(input) {
|
|
814
|
-
const
|
|
815
|
-
if (
|
|
854
|
+
const parsedType = this._getType(input);
|
|
855
|
+
if (parsedType !== parseUtil_1.ZodParsedType.undefined) {
|
|
856
|
+
const ctx = this._getOrReturnCtx(input);
|
|
816
857
|
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
817
858
|
code: ZodError_1.ZodIssueCode.invalid_type,
|
|
818
859
|
expected: parseUtil_1.ZodParsedType.void,
|
|
@@ -820,7 +861,7 @@ class ZodVoid extends ZodType {
|
|
|
820
861
|
});
|
|
821
862
|
return parseUtil_1.INVALID;
|
|
822
863
|
}
|
|
823
|
-
return (0, parseUtil_1.OK)(
|
|
864
|
+
return (0, parseUtil_1.OK)(input.data);
|
|
824
865
|
}
|
|
825
866
|
}
|
|
826
867
|
exports.ZodVoid = ZodVoid;
|
|
@@ -832,7 +873,7 @@ ZodVoid.create = (params) => {
|
|
|
832
873
|
};
|
|
833
874
|
class ZodArray extends ZodType {
|
|
834
875
|
_parse(input) {
|
|
835
|
-
const {
|
|
876
|
+
const { ctx, status } = this._processInputParams(input);
|
|
836
877
|
const def = this._def;
|
|
837
878
|
if (ctx.parsedType !== parseUtil_1.ZodParsedType.array) {
|
|
838
879
|
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
@@ -866,7 +907,7 @@ class ZodArray extends ZodType {
|
|
|
866
907
|
status.dirty();
|
|
867
908
|
}
|
|
868
909
|
}
|
|
869
|
-
if (ctx.async) {
|
|
910
|
+
if (ctx.common.async) {
|
|
870
911
|
return Promise.all(ctx.data.map((item, i) => {
|
|
871
912
|
return def.type._parseAsync({
|
|
872
913
|
parent: ctx,
|
|
@@ -991,8 +1032,9 @@ class ZodObject extends ZodType {
|
|
|
991
1032
|
return (this._cached = { shape, keys });
|
|
992
1033
|
}
|
|
993
1034
|
_parse(input) {
|
|
994
|
-
const
|
|
995
|
-
if (
|
|
1035
|
+
const parsedType = this._getType(input);
|
|
1036
|
+
if (parsedType !== parseUtil_1.ZodParsedType.object) {
|
|
1037
|
+
const ctx = this._getOrReturnCtx(input);
|
|
996
1038
|
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
997
1039
|
code: ZodError_1.ZodIssueCode.invalid_type,
|
|
998
1040
|
expected: parseUtil_1.ZodParsedType.object,
|
|
@@ -1000,6 +1042,7 @@ class ZodObject extends ZodType {
|
|
|
1000
1042
|
});
|
|
1001
1043
|
return parseUtil_1.INVALID;
|
|
1002
1044
|
}
|
|
1045
|
+
const { status, ctx } = this._processInputParams(input);
|
|
1003
1046
|
const { shape, keys: shapeKeys } = this._getCached();
|
|
1004
1047
|
const dataKeys = util_1.util.objectKeys(ctx.data);
|
|
1005
1048
|
const extraKeys = dataKeys.filter((k) => !shapeKeys.includes(k));
|
|
@@ -1055,7 +1098,7 @@ class ZodObject extends ZodType {
|
|
|
1055
1098
|
});
|
|
1056
1099
|
}
|
|
1057
1100
|
}
|
|
1058
|
-
if (ctx.async) {
|
|
1101
|
+
if (ctx.common.async) {
|
|
1059
1102
|
return Promise.resolve()
|
|
1060
1103
|
.then(async () => {
|
|
1061
1104
|
const syncPairs = [];
|
|
@@ -1251,23 +1294,26 @@ class ZodUnion extends ZodType {
|
|
|
1251
1294
|
for (const result of results) {
|
|
1252
1295
|
if (result.result.status === "dirty") {
|
|
1253
1296
|
// add issues from dirty option
|
|
1254
|
-
ctx.issues.push(...result.ctx.issues);
|
|
1297
|
+
ctx.common.issues.push(...result.ctx.common.issues);
|
|
1255
1298
|
return result.result;
|
|
1256
1299
|
}
|
|
1257
1300
|
}
|
|
1258
1301
|
// return invalid
|
|
1259
|
-
const unionErrors = results.map((result) => new ZodError_1.ZodError(result.ctx.issues));
|
|
1302
|
+
const unionErrors = results.map((result) => new ZodError_1.ZodError(result.ctx.common.issues));
|
|
1260
1303
|
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
1261
1304
|
code: ZodError_1.ZodIssueCode.invalid_union,
|
|
1262
1305
|
unionErrors,
|
|
1263
1306
|
});
|
|
1264
1307
|
return parseUtil_1.INVALID;
|
|
1265
1308
|
}
|
|
1266
|
-
if (ctx.async) {
|
|
1309
|
+
if (ctx.common.async) {
|
|
1267
1310
|
return Promise.all(options.map(async (option) => {
|
|
1268
1311
|
const childCtx = {
|
|
1269
1312
|
...ctx,
|
|
1270
|
-
|
|
1313
|
+
common: {
|
|
1314
|
+
...ctx.common,
|
|
1315
|
+
issues: [],
|
|
1316
|
+
},
|
|
1271
1317
|
parent: null,
|
|
1272
1318
|
};
|
|
1273
1319
|
return {
|
|
@@ -1286,7 +1332,10 @@ class ZodUnion extends ZodType {
|
|
|
1286
1332
|
for (const option of options) {
|
|
1287
1333
|
const childCtx = {
|
|
1288
1334
|
...ctx,
|
|
1289
|
-
|
|
1335
|
+
common: {
|
|
1336
|
+
...ctx.common,
|
|
1337
|
+
issues: [],
|
|
1338
|
+
},
|
|
1290
1339
|
parent: null,
|
|
1291
1340
|
};
|
|
1292
1341
|
const result = option._parseSync({
|
|
@@ -1300,12 +1349,12 @@ class ZodUnion extends ZodType {
|
|
|
1300
1349
|
else if (result.status === "dirty" && !dirty) {
|
|
1301
1350
|
dirty = { result, ctx: childCtx };
|
|
1302
1351
|
}
|
|
1303
|
-
if (childCtx.issues.length) {
|
|
1304
|
-
issues.push(childCtx.issues);
|
|
1352
|
+
if (childCtx.common.issues.length) {
|
|
1353
|
+
issues.push(childCtx.common.issues);
|
|
1305
1354
|
}
|
|
1306
1355
|
}
|
|
1307
1356
|
if (dirty) {
|
|
1308
|
-
ctx.issues.push(...dirty.ctx.issues);
|
|
1357
|
+
ctx.common.issues.push(...dirty.ctx.common.issues);
|
|
1309
1358
|
return dirty.result;
|
|
1310
1359
|
}
|
|
1311
1360
|
const unionErrors = issues.map((issues) => new ZodError_1.ZodError(issues));
|
|
@@ -1350,7 +1399,7 @@ class ZodDiscriminatedUnion extends ZodType {
|
|
|
1350
1399
|
});
|
|
1351
1400
|
return parseUtil_1.INVALID;
|
|
1352
1401
|
}
|
|
1353
|
-
if (ctx.async) {
|
|
1402
|
+
if (ctx.common.async) {
|
|
1354
1403
|
return option._parseAsync({
|
|
1355
1404
|
data: ctx.data,
|
|
1356
1405
|
path: ctx.path,
|
|
@@ -1472,7 +1521,7 @@ class ZodIntersection extends ZodType {
|
|
|
1472
1521
|
}
|
|
1473
1522
|
return { status: status.value, value: merged.data };
|
|
1474
1523
|
};
|
|
1475
|
-
if (ctx.async) {
|
|
1524
|
+
if (ctx.common.async) {
|
|
1476
1525
|
return Promise.all([
|
|
1477
1526
|
this._def.left._parseAsync({
|
|
1478
1527
|
data: ctx.data,
|
|
@@ -1550,7 +1599,7 @@ class ZodTuple extends ZodType {
|
|
|
1550
1599
|
});
|
|
1551
1600
|
})
|
|
1552
1601
|
.filter((x) => !!x); // filter nulls
|
|
1553
|
-
if (ctx.async) {
|
|
1602
|
+
if (ctx.common.async) {
|
|
1554
1603
|
return Promise.all(items).then((results) => {
|
|
1555
1604
|
return parseUtil_1.ParseStatus.mergeArray(status, results);
|
|
1556
1605
|
});
|
|
@@ -1612,7 +1661,7 @@ class ZodRecord extends ZodType {
|
|
|
1612
1661
|
}),
|
|
1613
1662
|
});
|
|
1614
1663
|
}
|
|
1615
|
-
if (ctx.async) {
|
|
1664
|
+
if (ctx.common.async) {
|
|
1616
1665
|
return parseUtil_1.ParseStatus.mergeObjectAsync(status, pairs);
|
|
1617
1666
|
}
|
|
1618
1667
|
else {
|
|
@@ -1667,7 +1716,7 @@ class ZodMap extends ZodType {
|
|
|
1667
1716
|
}),
|
|
1668
1717
|
};
|
|
1669
1718
|
});
|
|
1670
|
-
if (ctx.async) {
|
|
1719
|
+
if (ctx.common.async) {
|
|
1671
1720
|
const finalMap = new Map();
|
|
1672
1721
|
return Promise.resolve().then(async () => {
|
|
1673
1722
|
for (const pair of pairs) {
|
|
@@ -1759,7 +1808,7 @@ class ZodSet extends ZodType {
|
|
|
1759
1808
|
return { status: status.value, value: parsedSet };
|
|
1760
1809
|
}
|
|
1761
1810
|
const elements = [...ctx.data.values()].map((item, i) => valueType._parse({ data: item, path: [...ctx.path, i], parent: ctx }));
|
|
1762
|
-
if (ctx.async) {
|
|
1811
|
+
if (ctx.common.async) {
|
|
1763
1812
|
return Promise.all(elements).then((elements) => finalizeSet(elements));
|
|
1764
1813
|
}
|
|
1765
1814
|
else {
|
|
@@ -1815,7 +1864,7 @@ class ZodFunction extends ZodType {
|
|
|
1815
1864
|
data: args,
|
|
1816
1865
|
path: ctx.path,
|
|
1817
1866
|
errorMaps: [
|
|
1818
|
-
ctx.contextualErrorMap,
|
|
1867
|
+
ctx.common.contextualErrorMap,
|
|
1819
1868
|
ctx.schemaErrorMap,
|
|
1820
1869
|
ZodError_1.overrideErrorMap,
|
|
1821
1870
|
ZodError_1.defaultErrorMap,
|
|
@@ -1831,7 +1880,7 @@ class ZodFunction extends ZodType {
|
|
|
1831
1880
|
data: returns,
|
|
1832
1881
|
path: ctx.path,
|
|
1833
1882
|
errorMaps: [
|
|
1834
|
-
ctx.contextualErrorMap,
|
|
1883
|
+
ctx.common.contextualErrorMap,
|
|
1835
1884
|
ctx.schemaErrorMap,
|
|
1836
1885
|
ZodError_1.overrideErrorMap,
|
|
1837
1886
|
ZodError_1.defaultErrorMap,
|
|
@@ -1842,7 +1891,7 @@ class ZodFunction extends ZodType {
|
|
|
1842
1891
|
},
|
|
1843
1892
|
});
|
|
1844
1893
|
}
|
|
1845
|
-
const params = { errorMap: ctx.contextualErrorMap };
|
|
1894
|
+
const params = { errorMap: ctx.common.contextualErrorMap };
|
|
1846
1895
|
const fn = ctx.data;
|
|
1847
1896
|
if (this._def.returns instanceof ZodPromise) {
|
|
1848
1897
|
return (0, parseUtil_1.OK)(async (...args) => {
|
|
@@ -1936,8 +1985,8 @@ ZodLazy.create = (getter, params) => {
|
|
|
1936
1985
|
};
|
|
1937
1986
|
class ZodLiteral extends ZodType {
|
|
1938
1987
|
_parse(input) {
|
|
1939
|
-
|
|
1940
|
-
|
|
1988
|
+
if (input.data !== this._def.value) {
|
|
1989
|
+
const ctx = this._getOrReturnCtx(input);
|
|
1941
1990
|
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
1942
1991
|
code: ZodError_1.ZodIssueCode.invalid_type,
|
|
1943
1992
|
expected: (0, parseUtil_1.getParsedType)(this._def.value),
|
|
@@ -1945,7 +1994,7 @@ class ZodLiteral extends ZodType {
|
|
|
1945
1994
|
});
|
|
1946
1995
|
return parseUtil_1.INVALID;
|
|
1947
1996
|
}
|
|
1948
|
-
return { status:
|
|
1997
|
+
return { status: "valid", value: input.data };
|
|
1949
1998
|
}
|
|
1950
1999
|
get value() {
|
|
1951
2000
|
return this._def.value;
|
|
@@ -1967,15 +2016,15 @@ function createZodEnum(values) {
|
|
|
1967
2016
|
}
|
|
1968
2017
|
class ZodEnum extends ZodType {
|
|
1969
2018
|
_parse(input) {
|
|
1970
|
-
|
|
1971
|
-
|
|
2019
|
+
if (this._def.values.indexOf(input.data) === -1) {
|
|
2020
|
+
const ctx = this._getOrReturnCtx(input);
|
|
1972
2021
|
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
1973
2022
|
code: ZodError_1.ZodIssueCode.invalid_enum_value,
|
|
1974
2023
|
options: this._def.values,
|
|
1975
2024
|
});
|
|
1976
2025
|
return parseUtil_1.INVALID;
|
|
1977
2026
|
}
|
|
1978
|
-
return (0, parseUtil_1.OK)(
|
|
2027
|
+
return (0, parseUtil_1.OK)(input.data);
|
|
1979
2028
|
}
|
|
1980
2029
|
get options() {
|
|
1981
2030
|
return this._def.values;
|
|
@@ -2006,16 +2055,16 @@ exports.ZodEnum = ZodEnum;
|
|
|
2006
2055
|
ZodEnum.create = createZodEnum;
|
|
2007
2056
|
class ZodNativeEnum extends ZodType {
|
|
2008
2057
|
_parse(input) {
|
|
2009
|
-
const { ctx } = this._processInputParams(input);
|
|
2010
2058
|
const nativeEnumValues = util_1.util.getValidEnumValues(this._def.values);
|
|
2011
|
-
if (nativeEnumValues.indexOf(
|
|
2059
|
+
if (nativeEnumValues.indexOf(input.data) === -1) {
|
|
2060
|
+
const ctx = this._getOrReturnCtx(input);
|
|
2012
2061
|
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
2013
2062
|
code: ZodError_1.ZodIssueCode.invalid_enum_value,
|
|
2014
2063
|
options: util_1.util.objectValues(nativeEnumValues),
|
|
2015
2064
|
});
|
|
2016
2065
|
return parseUtil_1.INVALID;
|
|
2017
2066
|
}
|
|
2018
|
-
return (0, parseUtil_1.OK)(
|
|
2067
|
+
return (0, parseUtil_1.OK)(input.data);
|
|
2019
2068
|
}
|
|
2020
2069
|
get enum() {
|
|
2021
2070
|
return this._def.values;
|
|
@@ -2032,7 +2081,8 @@ ZodNativeEnum.create = (values, params) => {
|
|
|
2032
2081
|
class ZodPromise extends ZodType {
|
|
2033
2082
|
_parse(input) {
|
|
2034
2083
|
const { ctx } = this._processInputParams(input);
|
|
2035
|
-
if (ctx.parsedType !== parseUtil_1.ZodParsedType.promise &&
|
|
2084
|
+
if (ctx.parsedType !== parseUtil_1.ZodParsedType.promise &&
|
|
2085
|
+
ctx.common.async === false) {
|
|
2036
2086
|
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
2037
2087
|
code: ZodError_1.ZodIssueCode.invalid_type,
|
|
2038
2088
|
expected: parseUtil_1.ZodParsedType.promise,
|
|
@@ -2046,7 +2096,7 @@ class ZodPromise extends ZodType {
|
|
|
2046
2096
|
return (0, parseUtil_1.OK)(promisified.then((data) => {
|
|
2047
2097
|
return this._def.type.parseAsync(data, {
|
|
2048
2098
|
path: ctx.path,
|
|
2049
|
-
errorMap: ctx.contextualErrorMap,
|
|
2099
|
+
errorMap: ctx.common.contextualErrorMap,
|
|
2050
2100
|
});
|
|
2051
2101
|
}));
|
|
2052
2102
|
}
|
|
@@ -2068,7 +2118,7 @@ class ZodEffects extends ZodType {
|
|
|
2068
2118
|
const effect = this._def.effect || null;
|
|
2069
2119
|
if (effect.type === "preprocess") {
|
|
2070
2120
|
const processed = effect.transform(ctx.data);
|
|
2071
|
-
if (ctx.async) {
|
|
2121
|
+
if (ctx.common.async) {
|
|
2072
2122
|
return Promise.resolve(processed).then((processed) => {
|
|
2073
2123
|
return this._def.schema._parseAsync({
|
|
2074
2124
|
data: processed,
|
|
@@ -2105,7 +2155,7 @@ class ZodEffects extends ZodType {
|
|
|
2105
2155
|
// effect: RefinementEffect<any>
|
|
2106
2156
|
) => {
|
|
2107
2157
|
const result = effect.refinement(acc, checkCtx);
|
|
2108
|
-
if (ctx.async) {
|
|
2158
|
+
if (ctx.common.async) {
|
|
2109
2159
|
return Promise.resolve(result);
|
|
2110
2160
|
}
|
|
2111
2161
|
if (result instanceof Promise) {
|
|
@@ -2113,7 +2163,7 @@ class ZodEffects extends ZodType {
|
|
|
2113
2163
|
}
|
|
2114
2164
|
return acc;
|
|
2115
2165
|
};
|
|
2116
|
-
if (ctx.async === false) {
|
|
2166
|
+
if (ctx.common.async === false) {
|
|
2117
2167
|
const inner = this._def.schema._parseSync({
|
|
2118
2168
|
data: ctx.data,
|
|
2119
2169
|
path: ctx.path,
|
|
@@ -2142,7 +2192,7 @@ class ZodEffects extends ZodType {
|
|
|
2142
2192
|
}
|
|
2143
2193
|
}
|
|
2144
2194
|
if (effect.type === "transform") {
|
|
2145
|
-
if (ctx.async === false) {
|
|
2195
|
+
if (ctx.common.async === false) {
|
|
2146
2196
|
const base = this._def.schema._parseSync({
|
|
2147
2197
|
data: ctx.data,
|
|
2148
2198
|
path: ctx.path,
|
|
@@ -2197,10 +2247,11 @@ ZodEffects.createWithPreprocess = (preprocess, schema, params) => {
|
|
|
2197
2247
|
};
|
|
2198
2248
|
class ZodOptional extends ZodType {
|
|
2199
2249
|
_parse(input) {
|
|
2200
|
-
const
|
|
2201
|
-
if (
|
|
2250
|
+
const parsedType = this._getType(input);
|
|
2251
|
+
if (parsedType === parseUtil_1.ZodParsedType.undefined) {
|
|
2202
2252
|
return (0, parseUtil_1.OK)(undefined);
|
|
2203
2253
|
}
|
|
2254
|
+
const { ctx } = this._processInputParams(input);
|
|
2204
2255
|
return this._def.innerType._parse({
|
|
2205
2256
|
data: ctx.data,
|
|
2206
2257
|
path: ctx.path,
|
|
@@ -2221,10 +2272,11 @@ ZodOptional.create = (type, params) => {
|
|
|
2221
2272
|
};
|
|
2222
2273
|
class ZodNullable extends ZodType {
|
|
2223
2274
|
_parse(input) {
|
|
2224
|
-
const
|
|
2225
|
-
if (
|
|
2275
|
+
const parsedType = this._getType(input);
|
|
2276
|
+
if (parsedType === parseUtil_1.ZodParsedType.null) {
|
|
2226
2277
|
return (0, parseUtil_1.OK)(null);
|
|
2227
2278
|
}
|
|
2279
|
+
const { ctx } = this._processInputParams(input);
|
|
2228
2280
|
return this._def.innerType._parse({
|
|
2229
2281
|
data: ctx.data,
|
|
2230
2282
|
path: ctx.path,
|
|
@@ -2270,8 +2322,9 @@ ZodDefault.create = (type, params) => {
|
|
|
2270
2322
|
};
|
|
2271
2323
|
class ZodNaN extends ZodType {
|
|
2272
2324
|
_parse(input) {
|
|
2273
|
-
const
|
|
2274
|
-
if (
|
|
2325
|
+
const parsedType = this._getType(input);
|
|
2326
|
+
if (parsedType !== parseUtil_1.ZodParsedType.nan) {
|
|
2327
|
+
const ctx = this._getOrReturnCtx(input);
|
|
2275
2328
|
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
2276
2329
|
code: ZodError_1.ZodIssueCode.invalid_type,
|
|
2277
2330
|
expected: parseUtil_1.ZodParsedType.nan,
|
|
@@ -2279,7 +2332,7 @@ class ZodNaN extends ZodType {
|
|
|
2279
2332
|
});
|
|
2280
2333
|
return parseUtil_1.INVALID;
|
|
2281
2334
|
}
|
|
2282
|
-
return { status:
|
|
2335
|
+
return { status: "valid", value: input.data };
|
|
2283
2336
|
}
|
|
2284
2337
|
}
|
|
2285
2338
|
exports.ZodNaN = ZodNaN;
|