zod 4.0.7 → 4.0.9
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/package.json +1 -1
- package/src/v4/classic/tests/union.test.ts +45 -0
- package/src/v4/core/schemas.ts +4 -1
- package/src/v4/core/tests/extend.test.ts +18 -0
- package/src/v4/core/util.ts +109 -132
- package/src/v4/core/versions.ts +1 -1
- package/v4/core/schemas.cjs +4 -1
- package/v4/core/schemas.js +4 -1
- package/v4/core/util.cjs +108 -133
- package/v4/core/util.d.cts +1 -0
- package/v4/core/util.d.ts +1 -0
- package/v4/core/util.js +107 -133
- package/v4/core/versions.cjs +1 -1
- package/v4/core/versions.js +1 -1
package/package.json
CHANGED
|
@@ -134,3 +134,48 @@ test("non-aborted errors", () => {
|
|
|
134
134
|
}
|
|
135
135
|
`);
|
|
136
136
|
});
|
|
137
|
+
|
|
138
|
+
test("surface continuable errors only if they exist", () => {
|
|
139
|
+
const schema = z.union([z.boolean(), z.uuid(), z.jwt()]);
|
|
140
|
+
|
|
141
|
+
expect(schema.safeParse("asdf")).toMatchInlineSnapshot(`
|
|
142
|
+
{
|
|
143
|
+
"error": [ZodError: [
|
|
144
|
+
{
|
|
145
|
+
"code": "invalid_union",
|
|
146
|
+
"errors": [
|
|
147
|
+
[
|
|
148
|
+
{
|
|
149
|
+
"expected": "boolean",
|
|
150
|
+
"code": "invalid_type",
|
|
151
|
+
"path": [],
|
|
152
|
+
"message": "Invalid input: expected boolean, received string"
|
|
153
|
+
}
|
|
154
|
+
],
|
|
155
|
+
[
|
|
156
|
+
{
|
|
157
|
+
"origin": "string",
|
|
158
|
+
"code": "invalid_format",
|
|
159
|
+
"format": "uuid",
|
|
160
|
+
"pattern": "/^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-8][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}|00000000-0000-0000-0000-000000000000)$/",
|
|
161
|
+
"path": [],
|
|
162
|
+
"message": "Invalid UUID"
|
|
163
|
+
}
|
|
164
|
+
],
|
|
165
|
+
[
|
|
166
|
+
{
|
|
167
|
+
"code": "invalid_format",
|
|
168
|
+
"format": "jwt",
|
|
169
|
+
"path": [],
|
|
170
|
+
"message": "Invalid JWT"
|
|
171
|
+
}
|
|
172
|
+
]
|
|
173
|
+
],
|
|
174
|
+
"path": [],
|
|
175
|
+
"message": "Invalid input"
|
|
176
|
+
}
|
|
177
|
+
]],
|
|
178
|
+
"success": false,
|
|
179
|
+
}
|
|
180
|
+
`);
|
|
181
|
+
});
|
package/src/v4/core/schemas.ts
CHANGED
|
@@ -1911,7 +1911,7 @@ function handleUnionResults(results: ParsePayload[], final: ParsePayload, inst:
|
|
|
1911
1911
|
}
|
|
1912
1912
|
|
|
1913
1913
|
const nonaborted = results.filter((r) => !util.aborted(r));
|
|
1914
|
-
if (nonaborted.length
|
|
1914
|
+
if (nonaborted.length === 1) {
|
|
1915
1915
|
final.value = nonaborted[0].value;
|
|
1916
1916
|
return nonaborted[0];
|
|
1917
1917
|
}
|
|
@@ -2781,6 +2781,9 @@ export const $ZodLiteral: core.$constructor<$ZodLiteral> = /*@__PURE__*/ core.$c
|
|
|
2781
2781
|
"$ZodLiteral",
|
|
2782
2782
|
(inst, def) => {
|
|
2783
2783
|
$ZodType.init(inst, def);
|
|
2784
|
+
if (def.values.length === 0) {
|
|
2785
|
+
throw new Error("Cannot create literal schema with no valid values");
|
|
2786
|
+
}
|
|
2784
2787
|
|
|
2785
2788
|
inst._zod.values = new Set<util.Literal>(def.values);
|
|
2786
2789
|
inst._zod.pattern = new RegExp(
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { test } from "vitest";
|
|
2
|
+
import * as z from "zod/v4";
|
|
3
|
+
|
|
4
|
+
test("extend chaining preserves and overrides properties", () => {
|
|
5
|
+
const schema1 = z.object({
|
|
6
|
+
email: z.string(),
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
const schema2 = schema1.extend({
|
|
10
|
+
email: schema1.shape.email.check(z.email()),
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
const schema3 = schema2.extend({
|
|
14
|
+
email: schema2.shape.email.or(z.literal("")),
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
schema3.parse({ email: "test@example.com" });
|
|
18
|
+
});
|
package/src/v4/core/util.ts
CHANGED
|
@@ -283,10 +283,19 @@ export function assignProp<T extends object, K extends PropertyKey>(
|
|
|
283
283
|
});
|
|
284
284
|
}
|
|
285
285
|
|
|
286
|
+
export function mergeDefs(...defs: Record<string, any>[]): any {
|
|
287
|
+
const mergedDescriptors: Record<string, PropertyDescriptor> = {};
|
|
288
|
+
|
|
289
|
+
for (const def of defs) {
|
|
290
|
+
const descriptors = Object.getOwnPropertyDescriptors(def);
|
|
291
|
+
Object.assign(mergedDescriptors, descriptors);
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
return Object.defineProperties({}, mergedDescriptors);
|
|
295
|
+
}
|
|
296
|
+
|
|
286
297
|
export function cloneDef(schema: schemas.$ZodType): any {
|
|
287
|
-
|
|
288
|
-
Object.defineProperties(def, Object.getOwnPropertyDescriptors(schema._zod.def));
|
|
289
|
-
return def;
|
|
298
|
+
return mergeDefs(schema._zod.def);
|
|
290
299
|
}
|
|
291
300
|
|
|
292
301
|
export function getElementAtPath(obj: any, path: (string | number)[] | null | undefined): any {
|
|
@@ -545,26 +554,21 @@ export const BIGINT_FORMAT_RANGES: Record<checks.$ZodBigIntFormats, [bigint, big
|
|
|
545
554
|
export function pick(schema: schemas.$ZodObject, mask: Record<string, unknown>): any {
|
|
546
555
|
const currDef = schema._zod.def;
|
|
547
556
|
|
|
548
|
-
const def =
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
if (!(key in currDef.shape)) {
|
|
555
|
-
throw new Error(`Unrecognized key: "${key}"`);
|
|
556
|
-
}
|
|
557
|
-
if (!mask[key]) continue;
|
|
558
|
-
newShape[key] = currDef.shape[key]!;
|
|
557
|
+
const def = mergeDefs(schema._zod.def, {
|
|
558
|
+
get shape() {
|
|
559
|
+
const newShape: Writeable<schemas.$ZodShape> = {};
|
|
560
|
+
for (const key in mask) {
|
|
561
|
+
if (!(key in currDef.shape)) {
|
|
562
|
+
throw new Error(`Unrecognized key: "${key}"`);
|
|
559
563
|
}
|
|
564
|
+
if (!mask[key]) continue;
|
|
565
|
+
newShape[key] = currDef.shape[key]!;
|
|
566
|
+
}
|
|
560
567
|
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
},
|
|
564
|
-
},
|
|
565
|
-
checks: {
|
|
566
|
-
value: [],
|
|
568
|
+
assignProp(this, "shape", newShape); // self-caching
|
|
569
|
+
return newShape;
|
|
567
570
|
},
|
|
571
|
+
checks: [],
|
|
568
572
|
});
|
|
569
573
|
|
|
570
574
|
return clone(schema, def) as any;
|
|
@@ -573,26 +577,21 @@ export function pick(schema: schemas.$ZodObject, mask: Record<string, unknown>):
|
|
|
573
577
|
export function omit(schema: schemas.$ZodObject, mask: object): any {
|
|
574
578
|
const currDef = schema._zod.def;
|
|
575
579
|
|
|
576
|
-
const def =
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
if (!(key in currDef.shape)) {
|
|
583
|
-
throw new Error(`Unrecognized key: "${key}"`);
|
|
584
|
-
}
|
|
585
|
-
if (!(mask as any)[key]) continue;
|
|
586
|
-
|
|
587
|
-
delete newShape[key];
|
|
580
|
+
const def = mergeDefs(schema._zod.def, {
|
|
581
|
+
get shape() {
|
|
582
|
+
const newShape: Writeable<schemas.$ZodShape> = { ...schema._zod.def.shape };
|
|
583
|
+
for (const key in mask) {
|
|
584
|
+
if (!(key in currDef.shape)) {
|
|
585
|
+
throw new Error(`Unrecognized key: "${key}"`);
|
|
588
586
|
}
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
587
|
+
if (!(mask as any)[key]) continue;
|
|
588
|
+
|
|
589
|
+
delete newShape[key];
|
|
590
|
+
}
|
|
591
|
+
assignProp(this, "shape", newShape); // self-caching
|
|
592
|
+
return newShape;
|
|
595
593
|
},
|
|
594
|
+
checks: [],
|
|
596
595
|
});
|
|
597
596
|
|
|
598
597
|
return clone(schema, def);
|
|
@@ -603,40 +602,28 @@ export function extend(schema: schemas.$ZodObject, shape: schemas.$ZodShape): an
|
|
|
603
602
|
throw new Error("Invalid input to extend: expected a plain object");
|
|
604
603
|
}
|
|
605
604
|
|
|
606
|
-
const def =
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
assignProp(this, "shape", _shape); // self-caching
|
|
612
|
-
return _shape;
|
|
613
|
-
},
|
|
614
|
-
},
|
|
615
|
-
checks: {
|
|
616
|
-
value: [],
|
|
605
|
+
const def = mergeDefs(schema._zod.def, {
|
|
606
|
+
get shape() {
|
|
607
|
+
const _shape = { ...schema._zod.def.shape, ...shape };
|
|
608
|
+
assignProp(this, "shape", _shape); // self-caching
|
|
609
|
+
return _shape;
|
|
617
610
|
},
|
|
611
|
+
checks: [],
|
|
618
612
|
});
|
|
619
613
|
return clone(schema, def) as any;
|
|
620
614
|
}
|
|
621
615
|
|
|
622
616
|
export function merge(a: schemas.$ZodObject, b: schemas.$ZodObject): any {
|
|
623
|
-
const def =
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
assignProp(this, "shape", _shape); // self-caching
|
|
629
|
-
return _shape;
|
|
630
|
-
},
|
|
617
|
+
const def = mergeDefs(a._zod.def, {
|
|
618
|
+
get shape() {
|
|
619
|
+
const _shape = { ...a._zod.def.shape, ...b._zod.def.shape };
|
|
620
|
+
assignProp(this, "shape", _shape); // self-caching
|
|
621
|
+
return _shape;
|
|
631
622
|
},
|
|
632
|
-
catchall
|
|
633
|
-
|
|
634
|
-
return b._zod.def.catchall;
|
|
635
|
-
},
|
|
636
|
-
},
|
|
637
|
-
checks: {
|
|
638
|
-
value: [], // delete existing checks
|
|
623
|
+
get catchall() {
|
|
624
|
+
return b._zod.def.catchall;
|
|
639
625
|
},
|
|
626
|
+
checks: [], // delete existing checks
|
|
640
627
|
});
|
|
641
628
|
|
|
642
629
|
return clone(a, def) as any;
|
|
@@ -647,46 +634,41 @@ export function partial(
|
|
|
647
634
|
schema: schemas.$ZodObject,
|
|
648
635
|
mask: object | undefined
|
|
649
636
|
): any {
|
|
650
|
-
const def =
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
if (!(key in oldShape)) {
|
|
660
|
-
throw new Error(`Unrecognized key: "${key}"`);
|
|
661
|
-
}
|
|
662
|
-
if (!(mask as any)[key]) continue;
|
|
663
|
-
// if (oldShape[key]!._zod.optin === "optional") continue;
|
|
664
|
-
shape[key] = Class
|
|
665
|
-
? new Class({
|
|
666
|
-
type: "optional",
|
|
667
|
-
innerType: oldShape[key]!,
|
|
668
|
-
})
|
|
669
|
-
: oldShape[key]!;
|
|
670
|
-
}
|
|
671
|
-
} else {
|
|
672
|
-
for (const key in oldShape) {
|
|
673
|
-
// if (oldShape[key]!._zod.optin === "optional") continue;
|
|
674
|
-
shape[key] = Class
|
|
675
|
-
? new Class({
|
|
676
|
-
type: "optional",
|
|
677
|
-
innerType: oldShape[key]!,
|
|
678
|
-
})
|
|
679
|
-
: oldShape[key]!;
|
|
637
|
+
const def = mergeDefs(schema._zod.def, {
|
|
638
|
+
get shape() {
|
|
639
|
+
const oldShape = schema._zod.def.shape;
|
|
640
|
+
const shape: Writeable<schemas.$ZodShape> = { ...oldShape };
|
|
641
|
+
|
|
642
|
+
if (mask) {
|
|
643
|
+
for (const key in mask) {
|
|
644
|
+
if (!(key in oldShape)) {
|
|
645
|
+
throw new Error(`Unrecognized key: "${key}"`);
|
|
680
646
|
}
|
|
647
|
+
if (!(mask as any)[key]) continue;
|
|
648
|
+
// if (oldShape[key]!._zod.optin === "optional") continue;
|
|
649
|
+
shape[key] = Class
|
|
650
|
+
? new Class({
|
|
651
|
+
type: "optional",
|
|
652
|
+
innerType: oldShape[key]!,
|
|
653
|
+
})
|
|
654
|
+
: oldShape[key]!;
|
|
655
|
+
}
|
|
656
|
+
} else {
|
|
657
|
+
for (const key in oldShape) {
|
|
658
|
+
// if (oldShape[key]!._zod.optin === "optional") continue;
|
|
659
|
+
shape[key] = Class
|
|
660
|
+
? new Class({
|
|
661
|
+
type: "optional",
|
|
662
|
+
innerType: oldShape[key]!,
|
|
663
|
+
})
|
|
664
|
+
: oldShape[key]!;
|
|
681
665
|
}
|
|
666
|
+
}
|
|
682
667
|
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
},
|
|
686
|
-
},
|
|
687
|
-
checks: {
|
|
688
|
-
value: [],
|
|
668
|
+
assignProp(this, "shape", shape); // self-caching
|
|
669
|
+
return shape;
|
|
689
670
|
},
|
|
671
|
+
checks: [],
|
|
690
672
|
});
|
|
691
673
|
|
|
692
674
|
return clone(schema, def) as any;
|
|
@@ -697,42 +679,37 @@ export function required(
|
|
|
697
679
|
schema: schemas.$ZodObject,
|
|
698
680
|
mask: object | undefined
|
|
699
681
|
): any {
|
|
700
|
-
const def =
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
if (!(key in shape)) {
|
|
710
|
-
throw new Error(`Unrecognized key: "${key}"`);
|
|
711
|
-
}
|
|
712
|
-
if (!(mask as any)[key]) continue;
|
|
713
|
-
// overwrite with non-optional
|
|
714
|
-
shape[key] = new Class({
|
|
715
|
-
type: "nonoptional",
|
|
716
|
-
innerType: oldShape[key]!,
|
|
717
|
-
});
|
|
718
|
-
}
|
|
719
|
-
} else {
|
|
720
|
-
for (const key in oldShape) {
|
|
721
|
-
// overwrite with non-optional
|
|
722
|
-
shape[key] = new Class({
|
|
723
|
-
type: "nonoptional",
|
|
724
|
-
innerType: oldShape[key]!,
|
|
725
|
-
});
|
|
682
|
+
const def = mergeDefs(schema._zod.def, {
|
|
683
|
+
get shape() {
|
|
684
|
+
const oldShape = schema._zod.def.shape;
|
|
685
|
+
const shape: Writeable<schemas.$ZodShape> = { ...oldShape };
|
|
686
|
+
|
|
687
|
+
if (mask) {
|
|
688
|
+
for (const key in mask) {
|
|
689
|
+
if (!(key in shape)) {
|
|
690
|
+
throw new Error(`Unrecognized key: "${key}"`);
|
|
726
691
|
}
|
|
692
|
+
if (!(mask as any)[key]) continue;
|
|
693
|
+
// overwrite with non-optional
|
|
694
|
+
shape[key] = new Class({
|
|
695
|
+
type: "nonoptional",
|
|
696
|
+
innerType: oldShape[key]!,
|
|
697
|
+
});
|
|
698
|
+
}
|
|
699
|
+
} else {
|
|
700
|
+
for (const key in oldShape) {
|
|
701
|
+
// overwrite with non-optional
|
|
702
|
+
shape[key] = new Class({
|
|
703
|
+
type: "nonoptional",
|
|
704
|
+
innerType: oldShape[key]!,
|
|
705
|
+
});
|
|
727
706
|
}
|
|
707
|
+
}
|
|
728
708
|
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
},
|
|
732
|
-
},
|
|
733
|
-
checks: {
|
|
734
|
-
value: [],
|
|
709
|
+
assignProp(this, "shape", shape); // self-caching
|
|
710
|
+
return shape;
|
|
735
711
|
},
|
|
712
|
+
checks: [],
|
|
736
713
|
});
|
|
737
714
|
|
|
738
715
|
return clone(schema, def) as any;
|
package/src/v4/core/versions.ts
CHANGED
package/v4/core/schemas.cjs
CHANGED
|
@@ -863,7 +863,7 @@ function handleUnionResults(results, final, inst, ctx) {
|
|
|
863
863
|
}
|
|
864
864
|
}
|
|
865
865
|
const nonaborted = results.filter((r) => !util.aborted(r));
|
|
866
|
-
if (nonaborted.length
|
|
866
|
+
if (nonaborted.length === 1) {
|
|
867
867
|
final.value = nonaborted[0].value;
|
|
868
868
|
return nonaborted[0];
|
|
869
869
|
}
|
|
@@ -1354,6 +1354,9 @@ exports.$ZodEnum = core.$constructor("$ZodEnum", (inst, def) => {
|
|
|
1354
1354
|
});
|
|
1355
1355
|
exports.$ZodLiteral = core.$constructor("$ZodLiteral", (inst, def) => {
|
|
1356
1356
|
exports.$ZodType.init(inst, def);
|
|
1357
|
+
if (def.values.length === 0) {
|
|
1358
|
+
throw new Error("Cannot create literal schema with no valid values");
|
|
1359
|
+
}
|
|
1357
1360
|
inst._zod.values = new Set(def.values);
|
|
1358
1361
|
inst._zod.pattern = new RegExp(`^(${def.values
|
|
1359
1362
|
.map((o) => (typeof o === "string" ? util.escapeRegex(o) : o ? util.escapeRegex(o.toString()) : String(o)))
|
package/v4/core/schemas.js
CHANGED
|
@@ -832,7 +832,7 @@ function handleUnionResults(results, final, inst, ctx) {
|
|
|
832
832
|
}
|
|
833
833
|
}
|
|
834
834
|
const nonaborted = results.filter((r) => !util.aborted(r));
|
|
835
|
-
if (nonaborted.length
|
|
835
|
+
if (nonaborted.length === 1) {
|
|
836
836
|
final.value = nonaborted[0].value;
|
|
837
837
|
return nonaborted[0];
|
|
838
838
|
}
|
|
@@ -1323,6 +1323,9 @@ export const $ZodEnum = /*@__PURE__*/ core.$constructor("$ZodEnum", (inst, def)
|
|
|
1323
1323
|
});
|
|
1324
1324
|
export const $ZodLiteral = /*@__PURE__*/ core.$constructor("$ZodLiteral", (inst, def) => {
|
|
1325
1325
|
$ZodType.init(inst, def);
|
|
1326
|
+
if (def.values.length === 0) {
|
|
1327
|
+
throw new Error("Cannot create literal schema with no valid values");
|
|
1328
|
+
}
|
|
1326
1329
|
inst._zod.values = new Set(def.values);
|
|
1327
1330
|
inst._zod.pattern = new RegExp(`^(${def.values
|
|
1328
1331
|
.map((o) => (typeof o === "string" ? util.escapeRegex(o) : o ? util.escapeRegex(o.toString()) : String(o)))
|
package/v4/core/util.cjs
CHANGED
|
@@ -15,6 +15,7 @@ exports.cleanRegex = cleanRegex;
|
|
|
15
15
|
exports.floatSafeRemainder = floatSafeRemainder;
|
|
16
16
|
exports.defineLazy = defineLazy;
|
|
17
17
|
exports.assignProp = assignProp;
|
|
18
|
+
exports.mergeDefs = mergeDefs;
|
|
18
19
|
exports.cloneDef = cloneDef;
|
|
19
20
|
exports.getElementAtPath = getElementAtPath;
|
|
20
21
|
exports.promiseAllObject = promiseAllObject;
|
|
@@ -128,10 +129,16 @@ function assignProp(target, prop, value) {
|
|
|
128
129
|
configurable: true,
|
|
129
130
|
});
|
|
130
131
|
}
|
|
132
|
+
function mergeDefs(...defs) {
|
|
133
|
+
const mergedDescriptors = {};
|
|
134
|
+
for (const def of defs) {
|
|
135
|
+
const descriptors = Object.getOwnPropertyDescriptors(def);
|
|
136
|
+
Object.assign(mergedDescriptors, descriptors);
|
|
137
|
+
}
|
|
138
|
+
return Object.defineProperties({}, mergedDescriptors);
|
|
139
|
+
}
|
|
131
140
|
function cloneDef(schema) {
|
|
132
|
-
|
|
133
|
-
Object.defineProperties(def, Object.getOwnPropertyDescriptors(schema._zod.def));
|
|
134
|
-
return def;
|
|
141
|
+
return mergeDefs(schema._zod.def);
|
|
135
142
|
}
|
|
136
143
|
function getElementAtPath(obj, path) {
|
|
137
144
|
if (!path)
|
|
@@ -336,51 +343,41 @@ exports.BIGINT_FORMAT_RANGES = {
|
|
|
336
343
|
};
|
|
337
344
|
function pick(schema, mask) {
|
|
338
345
|
const currDef = schema._zod.def;
|
|
339
|
-
const def =
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
if (!(key in currDef.shape)) {
|
|
346
|
-
throw new Error(`Unrecognized key: "${key}"`);
|
|
347
|
-
}
|
|
348
|
-
if (!mask[key])
|
|
349
|
-
continue;
|
|
350
|
-
newShape[key] = currDef.shape[key];
|
|
346
|
+
const def = mergeDefs(schema._zod.def, {
|
|
347
|
+
get shape() {
|
|
348
|
+
const newShape = {};
|
|
349
|
+
for (const key in mask) {
|
|
350
|
+
if (!(key in currDef.shape)) {
|
|
351
|
+
throw new Error(`Unrecognized key: "${key}"`);
|
|
351
352
|
}
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
353
|
+
if (!mask[key])
|
|
354
|
+
continue;
|
|
355
|
+
newShape[key] = currDef.shape[key];
|
|
356
|
+
}
|
|
357
|
+
assignProp(this, "shape", newShape); // self-caching
|
|
358
|
+
return newShape;
|
|
358
359
|
},
|
|
360
|
+
checks: [],
|
|
359
361
|
});
|
|
360
362
|
return clone(schema, def);
|
|
361
363
|
}
|
|
362
364
|
function omit(schema, mask) {
|
|
363
365
|
const currDef = schema._zod.def;
|
|
364
|
-
const def =
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
if (!(key in currDef.shape)) {
|
|
371
|
-
throw new Error(`Unrecognized key: "${key}"`);
|
|
372
|
-
}
|
|
373
|
-
if (!mask[key])
|
|
374
|
-
continue;
|
|
375
|
-
delete newShape[key];
|
|
366
|
+
const def = mergeDefs(schema._zod.def, {
|
|
367
|
+
get shape() {
|
|
368
|
+
const newShape = { ...schema._zod.def.shape };
|
|
369
|
+
for (const key in mask) {
|
|
370
|
+
if (!(key in currDef.shape)) {
|
|
371
|
+
throw new Error(`Unrecognized key: "${key}"`);
|
|
376
372
|
}
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
373
|
+
if (!mask[key])
|
|
374
|
+
continue;
|
|
375
|
+
delete newShape[key];
|
|
376
|
+
}
|
|
377
|
+
assignProp(this, "shape", newShape); // self-caching
|
|
378
|
+
return newShape;
|
|
383
379
|
},
|
|
380
|
+
checks: [],
|
|
384
381
|
});
|
|
385
382
|
return clone(schema, def);
|
|
386
383
|
}
|
|
@@ -388,123 +385,101 @@ function extend(schema, shape) {
|
|
|
388
385
|
if (!isPlainObject(shape)) {
|
|
389
386
|
throw new Error("Invalid input to extend: expected a plain object");
|
|
390
387
|
}
|
|
391
|
-
const def =
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
assignProp(this, "shape", _shape); // self-caching
|
|
397
|
-
return _shape;
|
|
398
|
-
},
|
|
399
|
-
},
|
|
400
|
-
checks: {
|
|
401
|
-
value: [],
|
|
388
|
+
const def = mergeDefs(schema._zod.def, {
|
|
389
|
+
get shape() {
|
|
390
|
+
const _shape = { ...schema._zod.def.shape, ...shape };
|
|
391
|
+
assignProp(this, "shape", _shape); // self-caching
|
|
392
|
+
return _shape;
|
|
402
393
|
},
|
|
394
|
+
checks: [],
|
|
403
395
|
});
|
|
404
396
|
return clone(schema, def);
|
|
405
397
|
}
|
|
406
398
|
function merge(a, b) {
|
|
407
|
-
const def =
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
assignProp(this, "shape", _shape); // self-caching
|
|
413
|
-
return _shape;
|
|
414
|
-
},
|
|
399
|
+
const def = mergeDefs(a._zod.def, {
|
|
400
|
+
get shape() {
|
|
401
|
+
const _shape = { ...a._zod.def.shape, ...b._zod.def.shape };
|
|
402
|
+
assignProp(this, "shape", _shape); // self-caching
|
|
403
|
+
return _shape;
|
|
415
404
|
},
|
|
416
|
-
catchall
|
|
417
|
-
|
|
418
|
-
return b._zod.def.catchall;
|
|
419
|
-
},
|
|
420
|
-
},
|
|
421
|
-
checks: {
|
|
422
|
-
value: [], // delete existing checks
|
|
405
|
+
get catchall() {
|
|
406
|
+
return b._zod.def.catchall;
|
|
423
407
|
},
|
|
408
|
+
checks: [], // delete existing checks
|
|
424
409
|
});
|
|
425
410
|
return clone(a, def);
|
|
426
411
|
}
|
|
427
412
|
function partial(Class, schema, mask) {
|
|
428
|
-
const def =
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
const
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
if (!(key in oldShape)) {
|
|
437
|
-
throw new Error(`Unrecognized key: "${key}"`);
|
|
438
|
-
}
|
|
439
|
-
if (!mask[key])
|
|
440
|
-
continue;
|
|
441
|
-
// if (oldShape[key]!._zod.optin === "optional") continue;
|
|
442
|
-
shape[key] = Class
|
|
443
|
-
? new Class({
|
|
444
|
-
type: "optional",
|
|
445
|
-
innerType: oldShape[key],
|
|
446
|
-
})
|
|
447
|
-
: oldShape[key];
|
|
413
|
+
const def = mergeDefs(schema._zod.def, {
|
|
414
|
+
get shape() {
|
|
415
|
+
const oldShape = schema._zod.def.shape;
|
|
416
|
+
const shape = { ...oldShape };
|
|
417
|
+
if (mask) {
|
|
418
|
+
for (const key in mask) {
|
|
419
|
+
if (!(key in oldShape)) {
|
|
420
|
+
throw new Error(`Unrecognized key: "${key}"`);
|
|
448
421
|
}
|
|
422
|
+
if (!mask[key])
|
|
423
|
+
continue;
|
|
424
|
+
// if (oldShape[key]!._zod.optin === "optional") continue;
|
|
425
|
+
shape[key] = Class
|
|
426
|
+
? new Class({
|
|
427
|
+
type: "optional",
|
|
428
|
+
innerType: oldShape[key],
|
|
429
|
+
})
|
|
430
|
+
: oldShape[key];
|
|
449
431
|
}
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
432
|
+
}
|
|
433
|
+
else {
|
|
434
|
+
for (const key in oldShape) {
|
|
435
|
+
// if (oldShape[key]!._zod.optin === "optional") continue;
|
|
436
|
+
shape[key] = Class
|
|
437
|
+
? new Class({
|
|
438
|
+
type: "optional",
|
|
439
|
+
innerType: oldShape[key],
|
|
440
|
+
})
|
|
441
|
+
: oldShape[key];
|
|
460
442
|
}
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
},
|
|
465
|
-
checks: {
|
|
466
|
-
value: [],
|
|
443
|
+
}
|
|
444
|
+
assignProp(this, "shape", shape); // self-caching
|
|
445
|
+
return shape;
|
|
467
446
|
},
|
|
447
|
+
checks: [],
|
|
468
448
|
});
|
|
469
449
|
return clone(schema, def);
|
|
470
450
|
}
|
|
471
451
|
function required(Class, schema, mask) {
|
|
472
|
-
const def =
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
const
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
if (!(key in shape)) {
|
|
481
|
-
throw new Error(`Unrecognized key: "${key}"`);
|
|
482
|
-
}
|
|
483
|
-
if (!mask[key])
|
|
484
|
-
continue;
|
|
485
|
-
// overwrite with non-optional
|
|
486
|
-
shape[key] = new Class({
|
|
487
|
-
type: "nonoptional",
|
|
488
|
-
innerType: oldShape[key],
|
|
489
|
-
});
|
|
452
|
+
const def = mergeDefs(schema._zod.def, {
|
|
453
|
+
get shape() {
|
|
454
|
+
const oldShape = schema._zod.def.shape;
|
|
455
|
+
const shape = { ...oldShape };
|
|
456
|
+
if (mask) {
|
|
457
|
+
for (const key in mask) {
|
|
458
|
+
if (!(key in shape)) {
|
|
459
|
+
throw new Error(`Unrecognized key: "${key}"`);
|
|
490
460
|
}
|
|
461
|
+
if (!mask[key])
|
|
462
|
+
continue;
|
|
463
|
+
// overwrite with non-optional
|
|
464
|
+
shape[key] = new Class({
|
|
465
|
+
type: "nonoptional",
|
|
466
|
+
innerType: oldShape[key],
|
|
467
|
+
});
|
|
491
468
|
}
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
}
|
|
469
|
+
}
|
|
470
|
+
else {
|
|
471
|
+
for (const key in oldShape) {
|
|
472
|
+
// overwrite with non-optional
|
|
473
|
+
shape[key] = new Class({
|
|
474
|
+
type: "nonoptional",
|
|
475
|
+
innerType: oldShape[key],
|
|
476
|
+
});
|
|
500
477
|
}
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
},
|
|
505
|
-
checks: {
|
|
506
|
-
value: [],
|
|
478
|
+
}
|
|
479
|
+
assignProp(this, "shape", shape); // self-caching
|
|
480
|
+
return shape;
|
|
507
481
|
},
|
|
482
|
+
checks: [],
|
|
508
483
|
});
|
|
509
484
|
return clone(schema, def);
|
|
510
485
|
}
|
package/v4/core/util.d.cts
CHANGED
|
@@ -121,6 +121,7 @@ export declare function cleanRegex(source: string): string;
|
|
|
121
121
|
export declare function floatSafeRemainder(val: number, step: number): number;
|
|
122
122
|
export declare function defineLazy<T, K extends keyof T>(object: T, key: K, getter: () => T[K]): void;
|
|
123
123
|
export declare function assignProp<T extends object, K extends PropertyKey>(target: T, prop: K, value: K extends keyof T ? T[K] : any): void;
|
|
124
|
+
export declare function mergeDefs(...defs: Record<string, any>[]): any;
|
|
124
125
|
export declare function cloneDef(schema: schemas.$ZodType): any;
|
|
125
126
|
export declare function getElementAtPath(obj: any, path: (string | number)[] | null | undefined): any;
|
|
126
127
|
export declare function promiseAllObject<T extends object>(promisesObj: T): Promise<{
|
package/v4/core/util.d.ts
CHANGED
|
@@ -121,6 +121,7 @@ export declare function cleanRegex(source: string): string;
|
|
|
121
121
|
export declare function floatSafeRemainder(val: number, step: number): number;
|
|
122
122
|
export declare function defineLazy<T, K extends keyof T>(object: T, key: K, getter: () => T[K]): void;
|
|
123
123
|
export declare function assignProp<T extends object, K extends PropertyKey>(target: T, prop: K, value: K extends keyof T ? T[K] : any): void;
|
|
124
|
+
export declare function mergeDefs(...defs: Record<string, any>[]): any;
|
|
124
125
|
export declare function cloneDef(schema: schemas.$ZodType): any;
|
|
125
126
|
export declare function getElementAtPath(obj: any, path: (string | number)[] | null | undefined): any;
|
|
126
127
|
export declare function promiseAllObject<T extends object>(promisesObj: T): Promise<{
|
package/v4/core/util.js
CHANGED
|
@@ -83,10 +83,16 @@ export function assignProp(target, prop, value) {
|
|
|
83
83
|
configurable: true,
|
|
84
84
|
});
|
|
85
85
|
}
|
|
86
|
+
export function mergeDefs(...defs) {
|
|
87
|
+
const mergedDescriptors = {};
|
|
88
|
+
for (const def of defs) {
|
|
89
|
+
const descriptors = Object.getOwnPropertyDescriptors(def);
|
|
90
|
+
Object.assign(mergedDescriptors, descriptors);
|
|
91
|
+
}
|
|
92
|
+
return Object.defineProperties({}, mergedDescriptors);
|
|
93
|
+
}
|
|
86
94
|
export function cloneDef(schema) {
|
|
87
|
-
|
|
88
|
-
Object.defineProperties(def, Object.getOwnPropertyDescriptors(schema._zod.def));
|
|
89
|
-
return def;
|
|
95
|
+
return mergeDefs(schema._zod.def);
|
|
90
96
|
}
|
|
91
97
|
export function getElementAtPath(obj, path) {
|
|
92
98
|
if (!path)
|
|
@@ -290,51 +296,41 @@ export const BIGINT_FORMAT_RANGES = {
|
|
|
290
296
|
};
|
|
291
297
|
export function pick(schema, mask) {
|
|
292
298
|
const currDef = schema._zod.def;
|
|
293
|
-
const def =
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
if (!(key in currDef.shape)) {
|
|
300
|
-
throw new Error(`Unrecognized key: "${key}"`);
|
|
301
|
-
}
|
|
302
|
-
if (!mask[key])
|
|
303
|
-
continue;
|
|
304
|
-
newShape[key] = currDef.shape[key];
|
|
299
|
+
const def = mergeDefs(schema._zod.def, {
|
|
300
|
+
get shape() {
|
|
301
|
+
const newShape = {};
|
|
302
|
+
for (const key in mask) {
|
|
303
|
+
if (!(key in currDef.shape)) {
|
|
304
|
+
throw new Error(`Unrecognized key: "${key}"`);
|
|
305
305
|
}
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
306
|
+
if (!mask[key])
|
|
307
|
+
continue;
|
|
308
|
+
newShape[key] = currDef.shape[key];
|
|
309
|
+
}
|
|
310
|
+
assignProp(this, "shape", newShape); // self-caching
|
|
311
|
+
return newShape;
|
|
312
312
|
},
|
|
313
|
+
checks: [],
|
|
313
314
|
});
|
|
314
315
|
return clone(schema, def);
|
|
315
316
|
}
|
|
316
317
|
export function omit(schema, mask) {
|
|
317
318
|
const currDef = schema._zod.def;
|
|
318
|
-
const def =
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
if (!(key in currDef.shape)) {
|
|
325
|
-
throw new Error(`Unrecognized key: "${key}"`);
|
|
326
|
-
}
|
|
327
|
-
if (!mask[key])
|
|
328
|
-
continue;
|
|
329
|
-
delete newShape[key];
|
|
319
|
+
const def = mergeDefs(schema._zod.def, {
|
|
320
|
+
get shape() {
|
|
321
|
+
const newShape = { ...schema._zod.def.shape };
|
|
322
|
+
for (const key in mask) {
|
|
323
|
+
if (!(key in currDef.shape)) {
|
|
324
|
+
throw new Error(`Unrecognized key: "${key}"`);
|
|
330
325
|
}
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
326
|
+
if (!mask[key])
|
|
327
|
+
continue;
|
|
328
|
+
delete newShape[key];
|
|
329
|
+
}
|
|
330
|
+
assignProp(this, "shape", newShape); // self-caching
|
|
331
|
+
return newShape;
|
|
337
332
|
},
|
|
333
|
+
checks: [],
|
|
338
334
|
});
|
|
339
335
|
return clone(schema, def);
|
|
340
336
|
}
|
|
@@ -342,123 +338,101 @@ export function extend(schema, shape) {
|
|
|
342
338
|
if (!isPlainObject(shape)) {
|
|
343
339
|
throw new Error("Invalid input to extend: expected a plain object");
|
|
344
340
|
}
|
|
345
|
-
const def =
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
assignProp(this, "shape", _shape); // self-caching
|
|
351
|
-
return _shape;
|
|
352
|
-
},
|
|
353
|
-
},
|
|
354
|
-
checks: {
|
|
355
|
-
value: [],
|
|
341
|
+
const def = mergeDefs(schema._zod.def, {
|
|
342
|
+
get shape() {
|
|
343
|
+
const _shape = { ...schema._zod.def.shape, ...shape };
|
|
344
|
+
assignProp(this, "shape", _shape); // self-caching
|
|
345
|
+
return _shape;
|
|
356
346
|
},
|
|
347
|
+
checks: [],
|
|
357
348
|
});
|
|
358
349
|
return clone(schema, def);
|
|
359
350
|
}
|
|
360
351
|
export function merge(a, b) {
|
|
361
|
-
const def =
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
assignProp(this, "shape", _shape); // self-caching
|
|
367
|
-
return _shape;
|
|
368
|
-
},
|
|
352
|
+
const def = mergeDefs(a._zod.def, {
|
|
353
|
+
get shape() {
|
|
354
|
+
const _shape = { ...a._zod.def.shape, ...b._zod.def.shape };
|
|
355
|
+
assignProp(this, "shape", _shape); // self-caching
|
|
356
|
+
return _shape;
|
|
369
357
|
},
|
|
370
|
-
catchall
|
|
371
|
-
|
|
372
|
-
return b._zod.def.catchall;
|
|
373
|
-
},
|
|
374
|
-
},
|
|
375
|
-
checks: {
|
|
376
|
-
value: [], // delete existing checks
|
|
358
|
+
get catchall() {
|
|
359
|
+
return b._zod.def.catchall;
|
|
377
360
|
},
|
|
361
|
+
checks: [], // delete existing checks
|
|
378
362
|
});
|
|
379
363
|
return clone(a, def);
|
|
380
364
|
}
|
|
381
365
|
export function partial(Class, schema, mask) {
|
|
382
|
-
const def =
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
const
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
if (!(key in oldShape)) {
|
|
391
|
-
throw new Error(`Unrecognized key: "${key}"`);
|
|
392
|
-
}
|
|
393
|
-
if (!mask[key])
|
|
394
|
-
continue;
|
|
395
|
-
// if (oldShape[key]!._zod.optin === "optional") continue;
|
|
396
|
-
shape[key] = Class
|
|
397
|
-
? new Class({
|
|
398
|
-
type: "optional",
|
|
399
|
-
innerType: oldShape[key],
|
|
400
|
-
})
|
|
401
|
-
: oldShape[key];
|
|
366
|
+
const def = mergeDefs(schema._zod.def, {
|
|
367
|
+
get shape() {
|
|
368
|
+
const oldShape = schema._zod.def.shape;
|
|
369
|
+
const shape = { ...oldShape };
|
|
370
|
+
if (mask) {
|
|
371
|
+
for (const key in mask) {
|
|
372
|
+
if (!(key in oldShape)) {
|
|
373
|
+
throw new Error(`Unrecognized key: "${key}"`);
|
|
402
374
|
}
|
|
375
|
+
if (!mask[key])
|
|
376
|
+
continue;
|
|
377
|
+
// if (oldShape[key]!._zod.optin === "optional") continue;
|
|
378
|
+
shape[key] = Class
|
|
379
|
+
? new Class({
|
|
380
|
+
type: "optional",
|
|
381
|
+
innerType: oldShape[key],
|
|
382
|
+
})
|
|
383
|
+
: oldShape[key];
|
|
403
384
|
}
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
385
|
+
}
|
|
386
|
+
else {
|
|
387
|
+
for (const key in oldShape) {
|
|
388
|
+
// if (oldShape[key]!._zod.optin === "optional") continue;
|
|
389
|
+
shape[key] = Class
|
|
390
|
+
? new Class({
|
|
391
|
+
type: "optional",
|
|
392
|
+
innerType: oldShape[key],
|
|
393
|
+
})
|
|
394
|
+
: oldShape[key];
|
|
414
395
|
}
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
},
|
|
419
|
-
checks: {
|
|
420
|
-
value: [],
|
|
396
|
+
}
|
|
397
|
+
assignProp(this, "shape", shape); // self-caching
|
|
398
|
+
return shape;
|
|
421
399
|
},
|
|
400
|
+
checks: [],
|
|
422
401
|
});
|
|
423
402
|
return clone(schema, def);
|
|
424
403
|
}
|
|
425
404
|
export function required(Class, schema, mask) {
|
|
426
|
-
const def =
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
const
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
if (!(key in shape)) {
|
|
435
|
-
throw new Error(`Unrecognized key: "${key}"`);
|
|
436
|
-
}
|
|
437
|
-
if (!mask[key])
|
|
438
|
-
continue;
|
|
439
|
-
// overwrite with non-optional
|
|
440
|
-
shape[key] = new Class({
|
|
441
|
-
type: "nonoptional",
|
|
442
|
-
innerType: oldShape[key],
|
|
443
|
-
});
|
|
405
|
+
const def = mergeDefs(schema._zod.def, {
|
|
406
|
+
get shape() {
|
|
407
|
+
const oldShape = schema._zod.def.shape;
|
|
408
|
+
const shape = { ...oldShape };
|
|
409
|
+
if (mask) {
|
|
410
|
+
for (const key in mask) {
|
|
411
|
+
if (!(key in shape)) {
|
|
412
|
+
throw new Error(`Unrecognized key: "${key}"`);
|
|
444
413
|
}
|
|
414
|
+
if (!mask[key])
|
|
415
|
+
continue;
|
|
416
|
+
// overwrite with non-optional
|
|
417
|
+
shape[key] = new Class({
|
|
418
|
+
type: "nonoptional",
|
|
419
|
+
innerType: oldShape[key],
|
|
420
|
+
});
|
|
445
421
|
}
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
}
|
|
422
|
+
}
|
|
423
|
+
else {
|
|
424
|
+
for (const key in oldShape) {
|
|
425
|
+
// overwrite with non-optional
|
|
426
|
+
shape[key] = new Class({
|
|
427
|
+
type: "nonoptional",
|
|
428
|
+
innerType: oldShape[key],
|
|
429
|
+
});
|
|
454
430
|
}
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
},
|
|
459
|
-
checks: {
|
|
460
|
-
value: [],
|
|
431
|
+
}
|
|
432
|
+
assignProp(this, "shape", shape); // self-caching
|
|
433
|
+
return shape;
|
|
461
434
|
},
|
|
435
|
+
checks: [],
|
|
462
436
|
});
|
|
463
437
|
return clone(schema, def);
|
|
464
438
|
}
|
package/v4/core/versions.cjs
CHANGED
package/v4/core/versions.js
CHANGED