zod 3.17.2 → 3.17.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +70 -20
- package/lib/ZodError.d.ts +5 -1
- package/lib/ZodError.js +15 -2
- package/lib/external.js +1 -5
- package/lib/helpers/errorUtil.d.ts +6 -2
- package/lib/helpers/parseUtil.d.ts +2 -2
- package/lib/helpers/parseUtil.js +2 -2
- package/lib/index.js +1 -5
- package/lib/index.mjs +60 -13
- package/lib/index.umd.js +60 -13
- package/lib/types.d.ts +29 -9
- package/lib/types.js +123 -89
- package/package.json +6 -5
package/lib/types.js
CHANGED
|
@@ -18,7 +18,7 @@ class ParseInputLazyPath {
|
|
|
18
18
|
}
|
|
19
19
|
}
|
|
20
20
|
const handleResult = (ctx, result) => {
|
|
21
|
-
if (
|
|
21
|
+
if (parseUtil_1.isValid(result)) {
|
|
22
22
|
return { success: true, data: result.value };
|
|
23
23
|
}
|
|
24
24
|
else {
|
|
@@ -80,13 +80,13 @@ class ZodType {
|
|
|
80
80
|
return this._def.description;
|
|
81
81
|
}
|
|
82
82
|
_getType(input) {
|
|
83
|
-
return
|
|
83
|
+
return util_1.getParsedType(input.data);
|
|
84
84
|
}
|
|
85
85
|
_getOrReturnCtx(input, ctx) {
|
|
86
86
|
return (ctx || {
|
|
87
87
|
common: input.parent.common,
|
|
88
88
|
data: input.data,
|
|
89
|
-
parsedType:
|
|
89
|
+
parsedType: util_1.getParsedType(input.data),
|
|
90
90
|
schemaErrorMap: this._def.errorMap,
|
|
91
91
|
path: input.path,
|
|
92
92
|
parent: input.parent,
|
|
@@ -98,7 +98,7 @@ class ZodType {
|
|
|
98
98
|
ctx: {
|
|
99
99
|
common: input.parent.common,
|
|
100
100
|
data: input.data,
|
|
101
|
-
parsedType:
|
|
101
|
+
parsedType: util_1.getParsedType(input.data),
|
|
102
102
|
schemaErrorMap: this._def.errorMap,
|
|
103
103
|
path: input.path,
|
|
104
104
|
parent: input.parent,
|
|
@@ -107,7 +107,7 @@ class ZodType {
|
|
|
107
107
|
}
|
|
108
108
|
_parseSync(input) {
|
|
109
109
|
const result = this._parse(input);
|
|
110
|
-
if (
|
|
110
|
+
if (parseUtil_1.isAsync(result)) {
|
|
111
111
|
throw new Error("Synchronous parse encountered promise.");
|
|
112
112
|
}
|
|
113
113
|
return result;
|
|
@@ -134,7 +134,7 @@ class ZodType {
|
|
|
134
134
|
schemaErrorMap: this._def.errorMap,
|
|
135
135
|
parent: null,
|
|
136
136
|
data,
|
|
137
|
-
parsedType:
|
|
137
|
+
parsedType: util_1.getParsedType(data),
|
|
138
138
|
};
|
|
139
139
|
const result = this._parseSync({ data, path: ctx.path, parent: ctx });
|
|
140
140
|
return handleResult(ctx, result);
|
|
@@ -156,10 +156,10 @@ class ZodType {
|
|
|
156
156
|
schemaErrorMap: this._def.errorMap,
|
|
157
157
|
parent: null,
|
|
158
158
|
data,
|
|
159
|
-
parsedType:
|
|
159
|
+
parsedType: util_1.getParsedType(data),
|
|
160
160
|
};
|
|
161
161
|
const maybeAsyncResult = this._parse({ data, path: [], parent: ctx });
|
|
162
|
-
const result = await (
|
|
162
|
+
const result = await (parseUtil_1.isAsync(maybeAsyncResult)
|
|
163
163
|
? maybeAsyncResult
|
|
164
164
|
: Promise.resolve(maybeAsyncResult));
|
|
165
165
|
return handleResult(ctx, result);
|
|
@@ -304,7 +304,7 @@ class ZodString extends ZodType {
|
|
|
304
304
|
const parsedType = this._getType(input);
|
|
305
305
|
if (parsedType !== util_1.ZodParsedType.string) {
|
|
306
306
|
const ctx = this._getOrReturnCtx(input);
|
|
307
|
-
|
|
307
|
+
parseUtil_1.addIssueToContext(ctx, {
|
|
308
308
|
code: ZodError_1.ZodIssueCode.invalid_type,
|
|
309
309
|
expected: util_1.ZodParsedType.string,
|
|
310
310
|
received: ctx.parsedType,
|
|
@@ -319,7 +319,7 @@ class ZodString extends ZodType {
|
|
|
319
319
|
if (check.kind === "min") {
|
|
320
320
|
if (input.data.length < check.value) {
|
|
321
321
|
ctx = this._getOrReturnCtx(input, ctx);
|
|
322
|
-
|
|
322
|
+
parseUtil_1.addIssueToContext(ctx, {
|
|
323
323
|
code: ZodError_1.ZodIssueCode.too_small,
|
|
324
324
|
minimum: check.value,
|
|
325
325
|
type: "string",
|
|
@@ -332,7 +332,7 @@ class ZodString extends ZodType {
|
|
|
332
332
|
else if (check.kind === "max") {
|
|
333
333
|
if (input.data.length > check.value) {
|
|
334
334
|
ctx = this._getOrReturnCtx(input, ctx);
|
|
335
|
-
|
|
335
|
+
parseUtil_1.addIssueToContext(ctx, {
|
|
336
336
|
code: ZodError_1.ZodIssueCode.too_big,
|
|
337
337
|
maximum: check.value,
|
|
338
338
|
type: "string",
|
|
@@ -345,7 +345,7 @@ class ZodString extends ZodType {
|
|
|
345
345
|
else if (check.kind === "email") {
|
|
346
346
|
if (!emailRegex.test(input.data)) {
|
|
347
347
|
ctx = this._getOrReturnCtx(input, ctx);
|
|
348
|
-
|
|
348
|
+
parseUtil_1.addIssueToContext(ctx, {
|
|
349
349
|
validation: "email",
|
|
350
350
|
code: ZodError_1.ZodIssueCode.invalid_string,
|
|
351
351
|
message: check.message,
|
|
@@ -356,7 +356,7 @@ class ZodString extends ZodType {
|
|
|
356
356
|
else if (check.kind === "uuid") {
|
|
357
357
|
if (!uuidRegex.test(input.data)) {
|
|
358
358
|
ctx = this._getOrReturnCtx(input, ctx);
|
|
359
|
-
|
|
359
|
+
parseUtil_1.addIssueToContext(ctx, {
|
|
360
360
|
validation: "uuid",
|
|
361
361
|
code: ZodError_1.ZodIssueCode.invalid_string,
|
|
362
362
|
message: check.message,
|
|
@@ -367,7 +367,7 @@ class ZodString extends ZodType {
|
|
|
367
367
|
else if (check.kind === "cuid") {
|
|
368
368
|
if (!cuidRegex.test(input.data)) {
|
|
369
369
|
ctx = this._getOrReturnCtx(input, ctx);
|
|
370
|
-
|
|
370
|
+
parseUtil_1.addIssueToContext(ctx, {
|
|
371
371
|
validation: "cuid",
|
|
372
372
|
code: ZodError_1.ZodIssueCode.invalid_string,
|
|
373
373
|
message: check.message,
|
|
@@ -381,7 +381,7 @@ class ZodString extends ZodType {
|
|
|
381
381
|
}
|
|
382
382
|
catch (_a) {
|
|
383
383
|
ctx = this._getOrReturnCtx(input, ctx);
|
|
384
|
-
|
|
384
|
+
parseUtil_1.addIssueToContext(ctx, {
|
|
385
385
|
validation: "url",
|
|
386
386
|
code: ZodError_1.ZodIssueCode.invalid_string,
|
|
387
387
|
message: check.message,
|
|
@@ -394,7 +394,7 @@ class ZodString extends ZodType {
|
|
|
394
394
|
const testResult = check.regex.test(input.data);
|
|
395
395
|
if (!testResult) {
|
|
396
396
|
ctx = this._getOrReturnCtx(input, ctx);
|
|
397
|
-
|
|
397
|
+
parseUtil_1.addIssueToContext(ctx, {
|
|
398
398
|
validation: "regex",
|
|
399
399
|
code: ZodError_1.ZodIssueCode.invalid_string,
|
|
400
400
|
message: check.message,
|
|
@@ -405,6 +405,28 @@ class ZodString extends ZodType {
|
|
|
405
405
|
else if (check.kind === "trim") {
|
|
406
406
|
input.data = input.data.trim();
|
|
407
407
|
}
|
|
408
|
+
else if (check.kind === "startsWith") {
|
|
409
|
+
if (!input.data.startsWith(check.value)) {
|
|
410
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
411
|
+
parseUtil_1.addIssueToContext(ctx, {
|
|
412
|
+
code: ZodError_1.ZodIssueCode.invalid_string,
|
|
413
|
+
validation: { startsWith: check.value },
|
|
414
|
+
message: check.message,
|
|
415
|
+
});
|
|
416
|
+
status.dirty();
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
else if (check.kind === "endsWith") {
|
|
420
|
+
if (!input.data.endsWith(check.value)) {
|
|
421
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
422
|
+
parseUtil_1.addIssueToContext(ctx, {
|
|
423
|
+
code: ZodError_1.ZodIssueCode.invalid_string,
|
|
424
|
+
validation: { endsWith: check.value },
|
|
425
|
+
message: check.message,
|
|
426
|
+
});
|
|
427
|
+
status.dirty();
|
|
428
|
+
}
|
|
429
|
+
}
|
|
408
430
|
else {
|
|
409
431
|
util_1.util.assertNever(check);
|
|
410
432
|
}
|
|
@@ -436,6 +458,20 @@ class ZodString extends ZodType {
|
|
|
436
458
|
...errorUtil_1.errorUtil.errToObj(message),
|
|
437
459
|
});
|
|
438
460
|
}
|
|
461
|
+
startsWith(value, message) {
|
|
462
|
+
return this._addCheck({
|
|
463
|
+
kind: "startsWith",
|
|
464
|
+
value: value,
|
|
465
|
+
...errorUtil_1.errorUtil.errToObj(message),
|
|
466
|
+
});
|
|
467
|
+
}
|
|
468
|
+
endsWith(value, message) {
|
|
469
|
+
return this._addCheck({
|
|
470
|
+
kind: "endsWith",
|
|
471
|
+
value: value,
|
|
472
|
+
...errorUtil_1.errorUtil.errToObj(message),
|
|
473
|
+
});
|
|
474
|
+
}
|
|
439
475
|
min(minLength, message) {
|
|
440
476
|
return this._addCheck({
|
|
441
477
|
kind: "min",
|
|
@@ -466,25 +502,23 @@ class ZodString extends ZodType {
|
|
|
466
502
|
return !!this._def.checks.find((ch) => ch.kind === "cuid");
|
|
467
503
|
}
|
|
468
504
|
get minLength() {
|
|
469
|
-
let min =
|
|
470
|
-
this._def.checks
|
|
505
|
+
let min = null;
|
|
506
|
+
for (const ch of this._def.checks) {
|
|
471
507
|
if (ch.kind === "min") {
|
|
472
|
-
if (min === null || ch.value > min)
|
|
508
|
+
if (min === null || ch.value > min)
|
|
473
509
|
min = ch.value;
|
|
474
|
-
}
|
|
475
510
|
}
|
|
476
|
-
}
|
|
511
|
+
}
|
|
477
512
|
return min;
|
|
478
513
|
}
|
|
479
514
|
get maxLength() {
|
|
480
515
|
let max = null;
|
|
481
|
-
this._def.checks
|
|
516
|
+
for (const ch of this._def.checks) {
|
|
482
517
|
if (ch.kind === "max") {
|
|
483
|
-
if (max === null || ch.value < max)
|
|
518
|
+
if (max === null || ch.value < max)
|
|
484
519
|
max = ch.value;
|
|
485
|
-
}
|
|
486
520
|
}
|
|
487
|
-
}
|
|
521
|
+
}
|
|
488
522
|
return max;
|
|
489
523
|
}
|
|
490
524
|
}
|
|
@@ -516,7 +550,7 @@ class ZodNumber extends ZodType {
|
|
|
516
550
|
const parsedType = this._getType(input);
|
|
517
551
|
if (parsedType !== util_1.ZodParsedType.number) {
|
|
518
552
|
const ctx = this._getOrReturnCtx(input);
|
|
519
|
-
|
|
553
|
+
parseUtil_1.addIssueToContext(ctx, {
|
|
520
554
|
code: ZodError_1.ZodIssueCode.invalid_type,
|
|
521
555
|
expected: util_1.ZodParsedType.number,
|
|
522
556
|
received: ctx.parsedType,
|
|
@@ -529,7 +563,7 @@ class ZodNumber extends ZodType {
|
|
|
529
563
|
if (check.kind === "int") {
|
|
530
564
|
if (!util_1.util.isInteger(input.data)) {
|
|
531
565
|
ctx = this._getOrReturnCtx(input, ctx);
|
|
532
|
-
|
|
566
|
+
parseUtil_1.addIssueToContext(ctx, {
|
|
533
567
|
code: ZodError_1.ZodIssueCode.invalid_type,
|
|
534
568
|
expected: "integer",
|
|
535
569
|
received: "float",
|
|
@@ -544,7 +578,7 @@ class ZodNumber extends ZodType {
|
|
|
544
578
|
: input.data <= check.value;
|
|
545
579
|
if (tooSmall) {
|
|
546
580
|
ctx = this._getOrReturnCtx(input, ctx);
|
|
547
|
-
|
|
581
|
+
parseUtil_1.addIssueToContext(ctx, {
|
|
548
582
|
code: ZodError_1.ZodIssueCode.too_small,
|
|
549
583
|
minimum: check.value,
|
|
550
584
|
type: "number",
|
|
@@ -560,7 +594,7 @@ class ZodNumber extends ZodType {
|
|
|
560
594
|
: input.data >= check.value;
|
|
561
595
|
if (tooBig) {
|
|
562
596
|
ctx = this._getOrReturnCtx(input, ctx);
|
|
563
|
-
|
|
597
|
+
parseUtil_1.addIssueToContext(ctx, {
|
|
564
598
|
code: ZodError_1.ZodIssueCode.too_big,
|
|
565
599
|
maximum: check.value,
|
|
566
600
|
type: "number",
|
|
@@ -573,7 +607,7 @@ class ZodNumber extends ZodType {
|
|
|
573
607
|
else if (check.kind === "multipleOf") {
|
|
574
608
|
if (floatSafeRemainder(input.data, check.value) !== 0) {
|
|
575
609
|
ctx = this._getOrReturnCtx(input, ctx);
|
|
576
|
-
|
|
610
|
+
parseUtil_1.addIssueToContext(ctx, {
|
|
577
611
|
code: ZodError_1.ZodIssueCode.not_multiple_of,
|
|
578
612
|
multipleOf: check.value,
|
|
579
613
|
message: check.message,
|
|
@@ -701,14 +735,14 @@ class ZodBigInt extends ZodType {
|
|
|
701
735
|
const parsedType = this._getType(input);
|
|
702
736
|
if (parsedType !== util_1.ZodParsedType.bigint) {
|
|
703
737
|
const ctx = this._getOrReturnCtx(input);
|
|
704
|
-
|
|
738
|
+
parseUtil_1.addIssueToContext(ctx, {
|
|
705
739
|
code: ZodError_1.ZodIssueCode.invalid_type,
|
|
706
740
|
expected: util_1.ZodParsedType.bigint,
|
|
707
741
|
received: ctx.parsedType,
|
|
708
742
|
});
|
|
709
743
|
return parseUtil_1.INVALID;
|
|
710
744
|
}
|
|
711
|
-
return
|
|
745
|
+
return parseUtil_1.OK(input.data);
|
|
712
746
|
}
|
|
713
747
|
}
|
|
714
748
|
exports.ZodBigInt = ZodBigInt;
|
|
@@ -723,14 +757,14 @@ class ZodBoolean extends ZodType {
|
|
|
723
757
|
const parsedType = this._getType(input);
|
|
724
758
|
if (parsedType !== util_1.ZodParsedType.boolean) {
|
|
725
759
|
const ctx = this._getOrReturnCtx(input);
|
|
726
|
-
|
|
760
|
+
parseUtil_1.addIssueToContext(ctx, {
|
|
727
761
|
code: ZodError_1.ZodIssueCode.invalid_type,
|
|
728
762
|
expected: util_1.ZodParsedType.boolean,
|
|
729
763
|
received: ctx.parsedType,
|
|
730
764
|
});
|
|
731
765
|
return parseUtil_1.INVALID;
|
|
732
766
|
}
|
|
733
|
-
return
|
|
767
|
+
return parseUtil_1.OK(input.data);
|
|
734
768
|
}
|
|
735
769
|
}
|
|
736
770
|
exports.ZodBoolean = ZodBoolean;
|
|
@@ -745,7 +779,7 @@ class ZodDate extends ZodType {
|
|
|
745
779
|
const parsedType = this._getType(input);
|
|
746
780
|
if (parsedType !== util_1.ZodParsedType.date) {
|
|
747
781
|
const ctx = this._getOrReturnCtx(input);
|
|
748
|
-
|
|
782
|
+
parseUtil_1.addIssueToContext(ctx, {
|
|
749
783
|
code: ZodError_1.ZodIssueCode.invalid_type,
|
|
750
784
|
expected: util_1.ZodParsedType.date,
|
|
751
785
|
received: ctx.parsedType,
|
|
@@ -754,7 +788,7 @@ class ZodDate extends ZodType {
|
|
|
754
788
|
}
|
|
755
789
|
if (isNaN(input.data.getTime())) {
|
|
756
790
|
const ctx = this._getOrReturnCtx(input);
|
|
757
|
-
|
|
791
|
+
parseUtil_1.addIssueToContext(ctx, {
|
|
758
792
|
code: ZodError_1.ZodIssueCode.invalid_date,
|
|
759
793
|
});
|
|
760
794
|
return parseUtil_1.INVALID;
|
|
@@ -777,14 +811,14 @@ class ZodUndefined extends ZodType {
|
|
|
777
811
|
const parsedType = this._getType(input);
|
|
778
812
|
if (parsedType !== util_1.ZodParsedType.undefined) {
|
|
779
813
|
const ctx = this._getOrReturnCtx(input);
|
|
780
|
-
|
|
814
|
+
parseUtil_1.addIssueToContext(ctx, {
|
|
781
815
|
code: ZodError_1.ZodIssueCode.invalid_type,
|
|
782
816
|
expected: util_1.ZodParsedType.undefined,
|
|
783
817
|
received: ctx.parsedType,
|
|
784
818
|
});
|
|
785
819
|
return parseUtil_1.INVALID;
|
|
786
820
|
}
|
|
787
|
-
return
|
|
821
|
+
return parseUtil_1.OK(input.data);
|
|
788
822
|
}
|
|
789
823
|
}
|
|
790
824
|
exports.ZodUndefined = ZodUndefined;
|
|
@@ -799,14 +833,14 @@ class ZodNull extends ZodType {
|
|
|
799
833
|
const parsedType = this._getType(input);
|
|
800
834
|
if (parsedType !== util_1.ZodParsedType.null) {
|
|
801
835
|
const ctx = this._getOrReturnCtx(input);
|
|
802
|
-
|
|
836
|
+
parseUtil_1.addIssueToContext(ctx, {
|
|
803
837
|
code: ZodError_1.ZodIssueCode.invalid_type,
|
|
804
838
|
expected: util_1.ZodParsedType.null,
|
|
805
839
|
received: ctx.parsedType,
|
|
806
840
|
});
|
|
807
841
|
return parseUtil_1.INVALID;
|
|
808
842
|
}
|
|
809
|
-
return
|
|
843
|
+
return parseUtil_1.OK(input.data);
|
|
810
844
|
}
|
|
811
845
|
}
|
|
812
846
|
exports.ZodNull = ZodNull;
|
|
@@ -823,7 +857,7 @@ class ZodAny extends ZodType {
|
|
|
823
857
|
this._any = true;
|
|
824
858
|
}
|
|
825
859
|
_parse(input) {
|
|
826
|
-
return
|
|
860
|
+
return parseUtil_1.OK(input.data);
|
|
827
861
|
}
|
|
828
862
|
}
|
|
829
863
|
exports.ZodAny = ZodAny;
|
|
@@ -840,7 +874,7 @@ class ZodUnknown extends ZodType {
|
|
|
840
874
|
this._unknown = true;
|
|
841
875
|
}
|
|
842
876
|
_parse(input) {
|
|
843
|
-
return
|
|
877
|
+
return parseUtil_1.OK(input.data);
|
|
844
878
|
}
|
|
845
879
|
}
|
|
846
880
|
exports.ZodUnknown = ZodUnknown;
|
|
@@ -853,7 +887,7 @@ ZodUnknown.create = (params) => {
|
|
|
853
887
|
class ZodNever extends ZodType {
|
|
854
888
|
_parse(input) {
|
|
855
889
|
const ctx = this._getOrReturnCtx(input);
|
|
856
|
-
|
|
890
|
+
parseUtil_1.addIssueToContext(ctx, {
|
|
857
891
|
code: ZodError_1.ZodIssueCode.invalid_type,
|
|
858
892
|
expected: util_1.ZodParsedType.never,
|
|
859
893
|
received: ctx.parsedType,
|
|
@@ -873,14 +907,14 @@ class ZodVoid extends ZodType {
|
|
|
873
907
|
const parsedType = this._getType(input);
|
|
874
908
|
if (parsedType !== util_1.ZodParsedType.undefined) {
|
|
875
909
|
const ctx = this._getOrReturnCtx(input);
|
|
876
|
-
|
|
910
|
+
parseUtil_1.addIssueToContext(ctx, {
|
|
877
911
|
code: ZodError_1.ZodIssueCode.invalid_type,
|
|
878
912
|
expected: util_1.ZodParsedType.void,
|
|
879
913
|
received: ctx.parsedType,
|
|
880
914
|
});
|
|
881
915
|
return parseUtil_1.INVALID;
|
|
882
916
|
}
|
|
883
|
-
return
|
|
917
|
+
return parseUtil_1.OK(input.data);
|
|
884
918
|
}
|
|
885
919
|
}
|
|
886
920
|
exports.ZodVoid = ZodVoid;
|
|
@@ -895,7 +929,7 @@ class ZodArray extends ZodType {
|
|
|
895
929
|
const { ctx, status } = this._processInputParams(input);
|
|
896
930
|
const def = this._def;
|
|
897
931
|
if (ctx.parsedType !== util_1.ZodParsedType.array) {
|
|
898
|
-
|
|
932
|
+
parseUtil_1.addIssueToContext(ctx, {
|
|
899
933
|
code: ZodError_1.ZodIssueCode.invalid_type,
|
|
900
934
|
expected: util_1.ZodParsedType.array,
|
|
901
935
|
received: ctx.parsedType,
|
|
@@ -904,7 +938,7 @@ class ZodArray extends ZodType {
|
|
|
904
938
|
}
|
|
905
939
|
if (def.minLength !== null) {
|
|
906
940
|
if (ctx.data.length < def.minLength.value) {
|
|
907
|
-
|
|
941
|
+
parseUtil_1.addIssueToContext(ctx, {
|
|
908
942
|
code: ZodError_1.ZodIssueCode.too_small,
|
|
909
943
|
minimum: def.minLength.value,
|
|
910
944
|
type: "array",
|
|
@@ -916,7 +950,7 @@ class ZodArray extends ZodType {
|
|
|
916
950
|
}
|
|
917
951
|
if (def.maxLength !== null) {
|
|
918
952
|
if (ctx.data.length > def.maxLength.value) {
|
|
919
|
-
|
|
953
|
+
parseUtil_1.addIssueToContext(ctx, {
|
|
920
954
|
code: ZodError_1.ZodIssueCode.too_big,
|
|
921
955
|
maximum: def.maxLength.value,
|
|
922
956
|
type: "array",
|
|
@@ -982,7 +1016,7 @@ var objectUtil;
|
|
|
982
1016
|
objectUtil.mergeShapes = (first, second) => {
|
|
983
1017
|
return {
|
|
984
1018
|
...first,
|
|
985
|
-
...second,
|
|
1019
|
+
...second,
|
|
986
1020
|
};
|
|
987
1021
|
};
|
|
988
1022
|
})(objectUtil = exports.objectUtil || (exports.objectUtil = {}));
|
|
@@ -1046,7 +1080,7 @@ class ZodObject extends ZodType {
|
|
|
1046
1080
|
const parsedType = this._getType(input);
|
|
1047
1081
|
if (parsedType !== util_1.ZodParsedType.object) {
|
|
1048
1082
|
const ctx = this._getOrReturnCtx(input);
|
|
1049
|
-
|
|
1083
|
+
parseUtil_1.addIssueToContext(ctx, {
|
|
1050
1084
|
code: ZodError_1.ZodIssueCode.invalid_type,
|
|
1051
1085
|
expected: util_1.ZodParsedType.object,
|
|
1052
1086
|
received: ctx.parsedType,
|
|
@@ -1083,7 +1117,7 @@ class ZodObject extends ZodType {
|
|
|
1083
1117
|
}
|
|
1084
1118
|
else if (unknownKeys === "strict") {
|
|
1085
1119
|
if (extraKeys.length > 0) {
|
|
1086
|
-
|
|
1120
|
+
parseUtil_1.addIssueToContext(ctx, {
|
|
1087
1121
|
code: ZodError_1.ZodIssueCode.unrecognized_keys,
|
|
1088
1122
|
keys: extraKeys,
|
|
1089
1123
|
});
|
|
@@ -1313,7 +1347,7 @@ class ZodUnion extends ZodType {
|
|
|
1313
1347
|
}
|
|
1314
1348
|
// return invalid
|
|
1315
1349
|
const unionErrors = results.map((result) => new ZodError_1.ZodError(result.ctx.common.issues));
|
|
1316
|
-
|
|
1350
|
+
parseUtil_1.addIssueToContext(ctx, {
|
|
1317
1351
|
code: ZodError_1.ZodIssueCode.invalid_union,
|
|
1318
1352
|
unionErrors,
|
|
1319
1353
|
});
|
|
@@ -1371,7 +1405,7 @@ class ZodUnion extends ZodType {
|
|
|
1371
1405
|
return dirty.result;
|
|
1372
1406
|
}
|
|
1373
1407
|
const unionErrors = issues.map((issues) => new ZodError_1.ZodError(issues));
|
|
1374
|
-
|
|
1408
|
+
parseUtil_1.addIssueToContext(ctx, {
|
|
1375
1409
|
code: ZodError_1.ZodIssueCode.invalid_union,
|
|
1376
1410
|
unionErrors,
|
|
1377
1411
|
});
|
|
@@ -1394,7 +1428,7 @@ class ZodDiscriminatedUnion extends ZodType {
|
|
|
1394
1428
|
_parse(input) {
|
|
1395
1429
|
const { ctx } = this._processInputParams(input);
|
|
1396
1430
|
if (ctx.parsedType !== util_1.ZodParsedType.object) {
|
|
1397
|
-
|
|
1431
|
+
parseUtil_1.addIssueToContext(ctx, {
|
|
1398
1432
|
code: ZodError_1.ZodIssueCode.invalid_type,
|
|
1399
1433
|
expected: util_1.ZodParsedType.object,
|
|
1400
1434
|
received: ctx.parsedType,
|
|
@@ -1405,7 +1439,7 @@ class ZodDiscriminatedUnion extends ZodType {
|
|
|
1405
1439
|
const discriminatorValue = ctx.data[discriminator];
|
|
1406
1440
|
const option = this.options.get(discriminatorValue);
|
|
1407
1441
|
if (!option) {
|
|
1408
|
-
|
|
1442
|
+
parseUtil_1.addIssueToContext(ctx, {
|
|
1409
1443
|
code: ZodError_1.ZodIssueCode.invalid_union_discriminator,
|
|
1410
1444
|
options: this.validDiscriminatorValues,
|
|
1411
1445
|
path: [discriminator],
|
|
@@ -1470,8 +1504,8 @@ class ZodDiscriminatedUnion extends ZodType {
|
|
|
1470
1504
|
}
|
|
1471
1505
|
exports.ZodDiscriminatedUnion = ZodDiscriminatedUnion;
|
|
1472
1506
|
function mergeValues(a, b) {
|
|
1473
|
-
const aType =
|
|
1474
|
-
const bType =
|
|
1507
|
+
const aType = util_1.getParsedType(a);
|
|
1508
|
+
const bType = util_1.getParsedType(b);
|
|
1475
1509
|
if (a === b) {
|
|
1476
1510
|
return { valid: true, data: a };
|
|
1477
1511
|
}
|
|
@@ -1519,17 +1553,17 @@ class ZodIntersection extends ZodType {
|
|
|
1519
1553
|
_parse(input) {
|
|
1520
1554
|
const { status, ctx } = this._processInputParams(input);
|
|
1521
1555
|
const handleParsed = (parsedLeft, parsedRight) => {
|
|
1522
|
-
if (
|
|
1556
|
+
if (parseUtil_1.isAborted(parsedLeft) || parseUtil_1.isAborted(parsedRight)) {
|
|
1523
1557
|
return parseUtil_1.INVALID;
|
|
1524
1558
|
}
|
|
1525
1559
|
const merged = mergeValues(parsedLeft.value, parsedRight.value);
|
|
1526
1560
|
if (!merged.valid) {
|
|
1527
|
-
|
|
1561
|
+
parseUtil_1.addIssueToContext(ctx, {
|
|
1528
1562
|
code: ZodError_1.ZodIssueCode.invalid_intersection_types,
|
|
1529
1563
|
});
|
|
1530
1564
|
return parseUtil_1.INVALID;
|
|
1531
1565
|
}
|
|
1532
|
-
if (
|
|
1566
|
+
if (parseUtil_1.isDirty(parsedLeft) || parseUtil_1.isDirty(parsedRight)) {
|
|
1533
1567
|
status.dirty();
|
|
1534
1568
|
}
|
|
1535
1569
|
return { status: status.value, value: merged.data };
|
|
@@ -1574,7 +1608,7 @@ class ZodTuple extends ZodType {
|
|
|
1574
1608
|
_parse(input) {
|
|
1575
1609
|
const { status, ctx } = this._processInputParams(input);
|
|
1576
1610
|
if (ctx.parsedType !== util_1.ZodParsedType.array) {
|
|
1577
|
-
|
|
1611
|
+
parseUtil_1.addIssueToContext(ctx, {
|
|
1578
1612
|
code: ZodError_1.ZodIssueCode.invalid_type,
|
|
1579
1613
|
expected: util_1.ZodParsedType.array,
|
|
1580
1614
|
received: ctx.parsedType,
|
|
@@ -1582,7 +1616,7 @@ class ZodTuple extends ZodType {
|
|
|
1582
1616
|
return parseUtil_1.INVALID;
|
|
1583
1617
|
}
|
|
1584
1618
|
if (ctx.data.length < this._def.items.length) {
|
|
1585
|
-
|
|
1619
|
+
parseUtil_1.addIssueToContext(ctx, {
|
|
1586
1620
|
code: ZodError_1.ZodIssueCode.too_small,
|
|
1587
1621
|
minimum: this._def.items.length,
|
|
1588
1622
|
inclusive: true,
|
|
@@ -1592,7 +1626,7 @@ class ZodTuple extends ZodType {
|
|
|
1592
1626
|
}
|
|
1593
1627
|
const rest = this._def.rest;
|
|
1594
1628
|
if (!rest && ctx.data.length > this._def.items.length) {
|
|
1595
|
-
|
|
1629
|
+
parseUtil_1.addIssueToContext(ctx, {
|
|
1596
1630
|
code: ZodError_1.ZodIssueCode.too_big,
|
|
1597
1631
|
maximum: this._def.items.length,
|
|
1598
1632
|
inclusive: true,
|
|
@@ -1646,7 +1680,7 @@ class ZodRecord extends ZodType {
|
|
|
1646
1680
|
_parse(input) {
|
|
1647
1681
|
const { status, ctx } = this._processInputParams(input);
|
|
1648
1682
|
if (ctx.parsedType !== util_1.ZodParsedType.object) {
|
|
1649
|
-
|
|
1683
|
+
parseUtil_1.addIssueToContext(ctx, {
|
|
1650
1684
|
code: ZodError_1.ZodIssueCode.invalid_type,
|
|
1651
1685
|
expected: util_1.ZodParsedType.object,
|
|
1652
1686
|
received: ctx.parsedType,
|
|
@@ -1694,7 +1728,7 @@ class ZodMap extends ZodType {
|
|
|
1694
1728
|
_parse(input) {
|
|
1695
1729
|
const { status, ctx } = this._processInputParams(input);
|
|
1696
1730
|
if (ctx.parsedType !== util_1.ZodParsedType.map) {
|
|
1697
|
-
|
|
1731
|
+
parseUtil_1.addIssueToContext(ctx, {
|
|
1698
1732
|
code: ZodError_1.ZodIssueCode.invalid_type,
|
|
1699
1733
|
expected: util_1.ZodParsedType.map,
|
|
1700
1734
|
received: ctx.parsedType,
|
|
@@ -1756,7 +1790,7 @@ class ZodSet extends ZodType {
|
|
|
1756
1790
|
_parse(input) {
|
|
1757
1791
|
const { status, ctx } = this._processInputParams(input);
|
|
1758
1792
|
if (ctx.parsedType !== util_1.ZodParsedType.set) {
|
|
1759
|
-
|
|
1793
|
+
parseUtil_1.addIssueToContext(ctx, {
|
|
1760
1794
|
code: ZodError_1.ZodIssueCode.invalid_type,
|
|
1761
1795
|
expected: util_1.ZodParsedType.set,
|
|
1762
1796
|
received: ctx.parsedType,
|
|
@@ -1766,7 +1800,7 @@ class ZodSet extends ZodType {
|
|
|
1766
1800
|
const def = this._def;
|
|
1767
1801
|
if (def.minSize !== null) {
|
|
1768
1802
|
if (ctx.data.size < def.minSize.value) {
|
|
1769
|
-
|
|
1803
|
+
parseUtil_1.addIssueToContext(ctx, {
|
|
1770
1804
|
code: ZodError_1.ZodIssueCode.too_small,
|
|
1771
1805
|
minimum: def.minSize.value,
|
|
1772
1806
|
type: "set",
|
|
@@ -1778,7 +1812,7 @@ class ZodSet extends ZodType {
|
|
|
1778
1812
|
}
|
|
1779
1813
|
if (def.maxSize !== null) {
|
|
1780
1814
|
if (ctx.data.size > def.maxSize.value) {
|
|
1781
|
-
|
|
1815
|
+
parseUtil_1.addIssueToContext(ctx, {
|
|
1782
1816
|
code: ZodError_1.ZodIssueCode.too_big,
|
|
1783
1817
|
maximum: def.maxSize.value,
|
|
1784
1818
|
type: "set",
|
|
@@ -1845,7 +1879,7 @@ class ZodFunction extends ZodType {
|
|
|
1845
1879
|
_parse(input) {
|
|
1846
1880
|
const { ctx } = this._processInputParams(input);
|
|
1847
1881
|
if (ctx.parsedType !== util_1.ZodParsedType.function) {
|
|
1848
|
-
|
|
1882
|
+
parseUtil_1.addIssueToContext(ctx, {
|
|
1849
1883
|
code: ZodError_1.ZodIssueCode.invalid_type,
|
|
1850
1884
|
expected: util_1.ZodParsedType.function,
|
|
1851
1885
|
received: ctx.parsedType,
|
|
@@ -1853,7 +1887,7 @@ class ZodFunction extends ZodType {
|
|
|
1853
1887
|
return parseUtil_1.INVALID;
|
|
1854
1888
|
}
|
|
1855
1889
|
function makeArgsIssue(args, error) {
|
|
1856
|
-
return
|
|
1890
|
+
return parseUtil_1.makeIssue({
|
|
1857
1891
|
data: args,
|
|
1858
1892
|
path: ctx.path,
|
|
1859
1893
|
errorMaps: [
|
|
@@ -1869,7 +1903,7 @@ class ZodFunction extends ZodType {
|
|
|
1869
1903
|
});
|
|
1870
1904
|
}
|
|
1871
1905
|
function makeReturnsIssue(returns, error) {
|
|
1872
|
-
return
|
|
1906
|
+
return parseUtil_1.makeIssue({
|
|
1873
1907
|
data: returns,
|
|
1874
1908
|
path: ctx.path,
|
|
1875
1909
|
errorMaps: [
|
|
@@ -1887,7 +1921,7 @@ class ZodFunction extends ZodType {
|
|
|
1887
1921
|
const params = { errorMap: ctx.common.contextualErrorMap };
|
|
1888
1922
|
const fn = ctx.data;
|
|
1889
1923
|
if (this._def.returns instanceof ZodPromise) {
|
|
1890
|
-
return
|
|
1924
|
+
return parseUtil_1.OK(async (...args) => {
|
|
1891
1925
|
const error = new ZodError_1.ZodError([]);
|
|
1892
1926
|
const parsedArgs = await this._def.args
|
|
1893
1927
|
.parseAsync(args, params)
|
|
@@ -1906,7 +1940,7 @@ class ZodFunction extends ZodType {
|
|
|
1906
1940
|
});
|
|
1907
1941
|
}
|
|
1908
1942
|
else {
|
|
1909
|
-
return
|
|
1943
|
+
return parseUtil_1.OK((...args) => {
|
|
1910
1944
|
const parsedArgs = this._def.args.safeParse(args, params);
|
|
1911
1945
|
if (!parsedArgs.success) {
|
|
1912
1946
|
throw new ZodError_1.ZodError([makeArgsIssue(args, parsedArgs.error)]);
|
|
@@ -1980,7 +2014,7 @@ class ZodLiteral extends ZodType {
|
|
|
1980
2014
|
_parse(input) {
|
|
1981
2015
|
if (input.data !== this._def.value) {
|
|
1982
2016
|
const ctx = this._getOrReturnCtx(input);
|
|
1983
|
-
|
|
2017
|
+
parseUtil_1.addIssueToContext(ctx, {
|
|
1984
2018
|
code: ZodError_1.ZodIssueCode.invalid_literal,
|
|
1985
2019
|
expected: this._def.value,
|
|
1986
2020
|
});
|
|
@@ -2012,7 +2046,7 @@ class ZodEnum extends ZodType {
|
|
|
2012
2046
|
if (typeof input.data !== "string") {
|
|
2013
2047
|
const ctx = this._getOrReturnCtx(input);
|
|
2014
2048
|
const expectedValues = this._def.values;
|
|
2015
|
-
|
|
2049
|
+
parseUtil_1.addIssueToContext(ctx, {
|
|
2016
2050
|
expected: util_1.util.joinValues(expectedValues),
|
|
2017
2051
|
received: ctx.parsedType,
|
|
2018
2052
|
code: ZodError_1.ZodIssueCode.invalid_type,
|
|
@@ -2022,14 +2056,14 @@ class ZodEnum extends ZodType {
|
|
|
2022
2056
|
if (this._def.values.indexOf(input.data) === -1) {
|
|
2023
2057
|
const ctx = this._getOrReturnCtx(input);
|
|
2024
2058
|
const expectedValues = this._def.values;
|
|
2025
|
-
|
|
2059
|
+
parseUtil_1.addIssueToContext(ctx, {
|
|
2026
2060
|
received: ctx.data,
|
|
2027
2061
|
code: ZodError_1.ZodIssueCode.invalid_enum_value,
|
|
2028
2062
|
options: expectedValues,
|
|
2029
2063
|
});
|
|
2030
2064
|
return parseUtil_1.INVALID;
|
|
2031
2065
|
}
|
|
2032
|
-
return
|
|
2066
|
+
return parseUtil_1.OK(input.data);
|
|
2033
2067
|
}
|
|
2034
2068
|
get options() {
|
|
2035
2069
|
return this._def.values;
|
|
@@ -2065,7 +2099,7 @@ class ZodNativeEnum extends ZodType {
|
|
|
2065
2099
|
if (ctx.parsedType !== util_1.ZodParsedType.string &&
|
|
2066
2100
|
ctx.parsedType !== util_1.ZodParsedType.number) {
|
|
2067
2101
|
const expectedValues = util_1.util.objectValues(nativeEnumValues);
|
|
2068
|
-
|
|
2102
|
+
parseUtil_1.addIssueToContext(ctx, {
|
|
2069
2103
|
expected: util_1.util.joinValues(expectedValues),
|
|
2070
2104
|
received: ctx.parsedType,
|
|
2071
2105
|
code: ZodError_1.ZodIssueCode.invalid_type,
|
|
@@ -2074,14 +2108,14 @@ class ZodNativeEnum extends ZodType {
|
|
|
2074
2108
|
}
|
|
2075
2109
|
if (nativeEnumValues.indexOf(input.data) === -1) {
|
|
2076
2110
|
const expectedValues = util_1.util.objectValues(nativeEnumValues);
|
|
2077
|
-
|
|
2111
|
+
parseUtil_1.addIssueToContext(ctx, {
|
|
2078
2112
|
received: ctx.data,
|
|
2079
2113
|
code: ZodError_1.ZodIssueCode.invalid_enum_value,
|
|
2080
2114
|
options: expectedValues,
|
|
2081
2115
|
});
|
|
2082
2116
|
return parseUtil_1.INVALID;
|
|
2083
2117
|
}
|
|
2084
|
-
return
|
|
2118
|
+
return parseUtil_1.OK(input.data);
|
|
2085
2119
|
}
|
|
2086
2120
|
get enum() {
|
|
2087
2121
|
return this._def.values;
|
|
@@ -2100,7 +2134,7 @@ class ZodPromise extends ZodType {
|
|
|
2100
2134
|
const { ctx } = this._processInputParams(input);
|
|
2101
2135
|
if (ctx.parsedType !== util_1.ZodParsedType.promise &&
|
|
2102
2136
|
ctx.common.async === false) {
|
|
2103
|
-
|
|
2137
|
+
parseUtil_1.addIssueToContext(ctx, {
|
|
2104
2138
|
code: ZodError_1.ZodIssueCode.invalid_type,
|
|
2105
2139
|
expected: util_1.ZodParsedType.promise,
|
|
2106
2140
|
received: ctx.parsedType,
|
|
@@ -2110,7 +2144,7 @@ class ZodPromise extends ZodType {
|
|
|
2110
2144
|
const promisified = ctx.parsedType === util_1.ZodParsedType.promise
|
|
2111
2145
|
? ctx.data
|
|
2112
2146
|
: Promise.resolve(ctx.data);
|
|
2113
|
-
return
|
|
2147
|
+
return parseUtil_1.OK(promisified.then((data) => {
|
|
2114
2148
|
return this._def.type.parseAsync(data, {
|
|
2115
2149
|
path: ctx.path,
|
|
2116
2150
|
errorMap: ctx.common.contextualErrorMap,
|
|
@@ -2154,7 +2188,7 @@ class ZodEffects extends ZodType {
|
|
|
2154
2188
|
}
|
|
2155
2189
|
const checkCtx = {
|
|
2156
2190
|
addIssue: (arg) => {
|
|
2157
|
-
|
|
2191
|
+
parseUtil_1.addIssueToContext(ctx, arg);
|
|
2158
2192
|
if (arg.fatal) {
|
|
2159
2193
|
status.abort();
|
|
2160
2194
|
}
|
|
@@ -2219,7 +2253,7 @@ class ZodEffects extends ZodType {
|
|
|
2219
2253
|
// if (base.status === "dirty") {
|
|
2220
2254
|
// return { status: "dirty", value: base.value };
|
|
2221
2255
|
// }
|
|
2222
|
-
if (!
|
|
2256
|
+
if (!parseUtil_1.isValid(base))
|
|
2223
2257
|
return base;
|
|
2224
2258
|
const result = effect.transform(base.value, checkCtx);
|
|
2225
2259
|
if (result instanceof Promise) {
|
|
@@ -2231,7 +2265,7 @@ class ZodEffects extends ZodType {
|
|
|
2231
2265
|
return this._def.schema
|
|
2232
2266
|
._parseAsync({ data: ctx.data, path: ctx.path, parent: ctx })
|
|
2233
2267
|
.then((base) => {
|
|
2234
|
-
if (!
|
|
2268
|
+
if (!parseUtil_1.isValid(base))
|
|
2235
2269
|
return base;
|
|
2236
2270
|
// if (base.status === "aborted") return INVALID;
|
|
2237
2271
|
// if (base.status === "dirty") {
|
|
@@ -2266,7 +2300,7 @@ class ZodOptional extends ZodType {
|
|
|
2266
2300
|
_parse(input) {
|
|
2267
2301
|
const parsedType = this._getType(input);
|
|
2268
2302
|
if (parsedType === util_1.ZodParsedType.undefined) {
|
|
2269
|
-
return
|
|
2303
|
+
return parseUtil_1.OK(undefined);
|
|
2270
2304
|
}
|
|
2271
2305
|
return this._def.innerType._parse(input);
|
|
2272
2306
|
}
|
|
@@ -2286,7 +2320,7 @@ class ZodNullable extends ZodType {
|
|
|
2286
2320
|
_parse(input) {
|
|
2287
2321
|
const parsedType = this._getType(input);
|
|
2288
2322
|
if (parsedType === util_1.ZodParsedType.null) {
|
|
2289
|
-
return
|
|
2323
|
+
return parseUtil_1.OK(null);
|
|
2290
2324
|
}
|
|
2291
2325
|
return this._def.innerType._parse(input);
|
|
2292
2326
|
}
|
|
@@ -2332,7 +2366,7 @@ class ZodNaN extends ZodType {
|
|
|
2332
2366
|
const parsedType = this._getType(input);
|
|
2333
2367
|
if (parsedType !== util_1.ZodParsedType.nan) {
|
|
2334
2368
|
const ctx = this._getOrReturnCtx(input);
|
|
2335
|
-
|
|
2369
|
+
parseUtil_1.addIssueToContext(ctx, {
|
|
2336
2370
|
code: ZodError_1.ZodIssueCode.invalid_type,
|
|
2337
2371
|
expected: util_1.ZodParsedType.nan,
|
|
2338
2372
|
received: ctx.parsedType,
|
|
@@ -2400,7 +2434,7 @@ var ZodFirstPartyTypeKind;
|
|
|
2400
2434
|
})(ZodFirstPartyTypeKind = exports.ZodFirstPartyTypeKind || (exports.ZodFirstPartyTypeKind = {}));
|
|
2401
2435
|
const instanceOfType = (cls, params = {
|
|
2402
2436
|
message: `Input not instance of ${cls.name}`,
|
|
2403
|
-
}) =>
|
|
2437
|
+
}) => exports.custom((data) => data instanceof cls, params, true);
|
|
2404
2438
|
exports.instanceof = instanceOfType;
|
|
2405
2439
|
const stringType = ZodString.create;
|
|
2406
2440
|
exports.string = stringType;
|