zod 4.0.14 → 4.0.15
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/schemas.ts +3 -3
- package/src/v4/classic/tests/recursive-types.test.ts +62 -0
- package/src/v4/core/api.ts +4 -4
- package/src/v4/core/core.ts +2 -2
- package/src/v4/core/util.ts +4 -0
- package/src/v4/core/versions.ts +1 -1
- package/v4/classic/schemas.cjs +3 -3
- package/v4/classic/schemas.js +3 -3
- package/v4/core/api.d.cts +4 -4
- package/v4/core/api.d.ts +4 -4
- package/v4/core/core.d.cts +2 -2
- package/v4/core/core.d.ts +2 -2
- package/v4/core/util.cjs +4 -0
- package/v4/core/util.d.cts +1 -0
- package/v4/core/util.d.ts +1 -0
- package/v4/core/util.js +3 -0
- package/v4/core/versions.cjs +1 -1
- package/v4/core/versions.js +1 -1
package/package.json
CHANGED
|
@@ -1149,7 +1149,7 @@ export function object<T extends core.$ZodLooseShape = Partial<Record<never, cor
|
|
|
1149
1149
|
const def: core.$ZodObjectDef = {
|
|
1150
1150
|
type: "object",
|
|
1151
1151
|
get shape() {
|
|
1152
|
-
util.assignProp(this, "shape",
|
|
1152
|
+
util.assignProp(this, "shape", shape ? util.objectClone(shape) : {});
|
|
1153
1153
|
return this.shape;
|
|
1154
1154
|
},
|
|
1155
1155
|
...util.normalizeParams(params),
|
|
@@ -1166,7 +1166,7 @@ export function strictObject<T extends core.$ZodLooseShape>(
|
|
|
1166
1166
|
return new ZodObject({
|
|
1167
1167
|
type: "object",
|
|
1168
1168
|
get shape() {
|
|
1169
|
-
util.assignProp(this, "shape",
|
|
1169
|
+
util.assignProp(this, "shape", util.objectClone(shape));
|
|
1170
1170
|
return this.shape;
|
|
1171
1171
|
},
|
|
1172
1172
|
catchall: never(),
|
|
@@ -1183,7 +1183,7 @@ export function looseObject<T extends core.$ZodLooseShape>(
|
|
|
1183
1183
|
return new ZodObject({
|
|
1184
1184
|
type: "object",
|
|
1185
1185
|
get shape() {
|
|
1186
|
-
util.assignProp(this, "shape",
|
|
1186
|
+
util.assignProp(this, "shape", util.objectClone(shape));
|
|
1187
1187
|
return this.shape;
|
|
1188
1188
|
},
|
|
1189
1189
|
catchall: unknown(),
|
|
@@ -446,6 +446,68 @@ test("recursion compatibility", () => {
|
|
|
446
446
|
});
|
|
447
447
|
});
|
|
448
448
|
|
|
449
|
+
test("recursive object with .check()", () => {
|
|
450
|
+
const Category = z
|
|
451
|
+
.object({
|
|
452
|
+
id: z.string(),
|
|
453
|
+
name: z.string(),
|
|
454
|
+
get subcategories() {
|
|
455
|
+
return z.array(Category).optional();
|
|
456
|
+
},
|
|
457
|
+
})
|
|
458
|
+
.check((ctx) => {
|
|
459
|
+
// Check for duplicate IDs among direct subcategories
|
|
460
|
+
if (ctx.value.subcategories) {
|
|
461
|
+
const siblingIds = new Set<string>();
|
|
462
|
+
ctx.value.subcategories.forEach((sub, index) => {
|
|
463
|
+
if (siblingIds.has(sub.id)) {
|
|
464
|
+
ctx.issues.push({
|
|
465
|
+
code: "custom",
|
|
466
|
+
message: `Duplicate sibling ID found: ${sub.id}`,
|
|
467
|
+
path: ["subcategories", index, "id"],
|
|
468
|
+
input: ctx.value,
|
|
469
|
+
});
|
|
470
|
+
}
|
|
471
|
+
siblingIds.add(sub.id);
|
|
472
|
+
});
|
|
473
|
+
}
|
|
474
|
+
});
|
|
475
|
+
|
|
476
|
+
// Valid - siblings have unique IDs
|
|
477
|
+
const validData = {
|
|
478
|
+
id: "electronics",
|
|
479
|
+
name: "Electronics",
|
|
480
|
+
subcategories: [
|
|
481
|
+
{
|
|
482
|
+
id: "computers",
|
|
483
|
+
name: "Computers",
|
|
484
|
+
subcategories: [
|
|
485
|
+
{ id: "laptops", name: "Laptops" },
|
|
486
|
+
{ id: "desktops", name: "Desktops" },
|
|
487
|
+
],
|
|
488
|
+
},
|
|
489
|
+
{
|
|
490
|
+
id: "phones",
|
|
491
|
+
name: "Phones",
|
|
492
|
+
},
|
|
493
|
+
],
|
|
494
|
+
};
|
|
495
|
+
|
|
496
|
+
// Invalid - duplicate sibling IDs
|
|
497
|
+
const invalidData = {
|
|
498
|
+
id: "electronics",
|
|
499
|
+
name: "Electronics",
|
|
500
|
+
subcategories: [
|
|
501
|
+
{ id: "computers", name: "Computers" },
|
|
502
|
+
{ id: "phones", name: "Phones" },
|
|
503
|
+
{ id: "computers", name: "Computers Again" }, // Duplicate at index 2
|
|
504
|
+
],
|
|
505
|
+
};
|
|
506
|
+
|
|
507
|
+
expect(() => Category.parse(validData)).not.toThrow();
|
|
508
|
+
expect(() => Category.parse(invalidData)).toThrow();
|
|
509
|
+
});
|
|
510
|
+
|
|
449
511
|
// biome-ignore lint: sadf
|
|
450
512
|
export type RecursiveA = z.ZodUnion<
|
|
451
513
|
[
|
package/src/v4/core/api.ts
CHANGED
|
@@ -314,8 +314,8 @@ export function _ksuid<T extends schemas.$ZodKSUID>(
|
|
|
314
314
|
}
|
|
315
315
|
|
|
316
316
|
// IPv4
|
|
317
|
-
export type $ZodIPv4Params = StringFormatParams<schemas.$ZodIPv4, "pattern" | "when">;
|
|
318
|
-
export type $ZodCheckIPv4Params = CheckStringFormatParams<schemas.$ZodIPv4, "pattern" | "when">;
|
|
317
|
+
export type $ZodIPv4Params = StringFormatParams<schemas.$ZodIPv4, "pattern" | "when" | "version">;
|
|
318
|
+
export type $ZodCheckIPv4Params = CheckStringFormatParams<schemas.$ZodIPv4, "pattern" | "when" | "version">;
|
|
319
319
|
export function _ipv4<T extends schemas.$ZodIPv4>(
|
|
320
320
|
Class: util.SchemaClass<T>,
|
|
321
321
|
params?: string | $ZodIPv4Params | $ZodCheckIPv4Params
|
|
@@ -330,8 +330,8 @@ export function _ipv4<T extends schemas.$ZodIPv4>(
|
|
|
330
330
|
}
|
|
331
331
|
|
|
332
332
|
// IPv6
|
|
333
|
-
export type $ZodIPv6Params = StringFormatParams<schemas.$ZodIPv6, "pattern" | "when">;
|
|
334
|
-
export type $ZodCheckIPv6Params = CheckStringFormatParams<schemas.$ZodIPv6, "pattern" | "when">;
|
|
333
|
+
export type $ZodIPv6Params = StringFormatParams<schemas.$ZodIPv6, "pattern" | "when" | "version">;
|
|
334
|
+
export type $ZodCheckIPv6Params = CheckStringFormatParams<schemas.$ZodIPv6, "pattern" | "when" | "version">;
|
|
335
335
|
export function _ipv6<T extends schemas.$ZodIPv6>(
|
|
336
336
|
Class: util.SchemaClass<T>,
|
|
337
337
|
params?: string | $ZodIPv6Params | $ZodCheckIPv6Params
|
package/src/v4/core/core.ts
CHANGED
|
@@ -84,8 +84,8 @@ export class $ZodAsyncError extends Error {
|
|
|
84
84
|
// export type output<T extends schemas.$ZodType> = T["_zod"]["output"];
|
|
85
85
|
// export type input<T extends schemas.$ZodType> = T["_zod"]["input"];
|
|
86
86
|
// export type output<T extends schemas.$ZodType> = T["_zod"]["output"];
|
|
87
|
-
export type input<T> = T extends { _zod: { input: any } } ?
|
|
88
|
-
export type output<T> = T extends { _zod: { output: any } } ?
|
|
87
|
+
export type input<T> = T extends { _zod: { input: any } } ? T["_zod"]["input"] : unknown;
|
|
88
|
+
export type output<T> = T extends { _zod: { output: any } } ? T["_zod"]["output"] : unknown;
|
|
89
89
|
|
|
90
90
|
// Mk2
|
|
91
91
|
// export type input<T> = T extends { _zod: { "~input": any } }
|
package/src/v4/core/util.ts
CHANGED
|
@@ -284,6 +284,10 @@ export function defineLazy<T, K extends keyof T>(object: T, key: K, getter: () =
|
|
|
284
284
|
});
|
|
285
285
|
}
|
|
286
286
|
|
|
287
|
+
export function objectClone(obj: object) {
|
|
288
|
+
return Object.create(Object.getPrototypeOf(obj), Object.getOwnPropertyDescriptors(obj));
|
|
289
|
+
}
|
|
290
|
+
|
|
287
291
|
export function assignProp<T extends object, K extends PropertyKey>(
|
|
288
292
|
target: T,
|
|
289
293
|
prop: K,
|
package/src/v4/core/versions.ts
CHANGED
package/v4/classic/schemas.cjs
CHANGED
|
@@ -615,7 +615,7 @@ function object(shape, params) {
|
|
|
615
615
|
const def = {
|
|
616
616
|
type: "object",
|
|
617
617
|
get shape() {
|
|
618
|
-
index_js_1.util.assignProp(this, "shape",
|
|
618
|
+
index_js_1.util.assignProp(this, "shape", shape ? index_js_1.util.objectClone(shape) : {});
|
|
619
619
|
return this.shape;
|
|
620
620
|
},
|
|
621
621
|
...index_js_1.util.normalizeParams(params),
|
|
@@ -627,7 +627,7 @@ function strictObject(shape, params) {
|
|
|
627
627
|
return new exports.ZodObject({
|
|
628
628
|
type: "object",
|
|
629
629
|
get shape() {
|
|
630
|
-
index_js_1.util.assignProp(this, "shape",
|
|
630
|
+
index_js_1.util.assignProp(this, "shape", index_js_1.util.objectClone(shape));
|
|
631
631
|
return this.shape;
|
|
632
632
|
},
|
|
633
633
|
catchall: never(),
|
|
@@ -639,7 +639,7 @@ function looseObject(shape, params) {
|
|
|
639
639
|
return new exports.ZodObject({
|
|
640
640
|
type: "object",
|
|
641
641
|
get shape() {
|
|
642
|
-
index_js_1.util.assignProp(this, "shape",
|
|
642
|
+
index_js_1.util.assignProp(this, "shape", index_js_1.util.objectClone(shape));
|
|
643
643
|
return this.shape;
|
|
644
644
|
},
|
|
645
645
|
catchall: unknown(),
|
package/v4/classic/schemas.js
CHANGED
|
@@ -509,7 +509,7 @@ export function object(shape, params) {
|
|
|
509
509
|
const def = {
|
|
510
510
|
type: "object",
|
|
511
511
|
get shape() {
|
|
512
|
-
util.assignProp(this, "shape",
|
|
512
|
+
util.assignProp(this, "shape", shape ? util.objectClone(shape) : {});
|
|
513
513
|
return this.shape;
|
|
514
514
|
},
|
|
515
515
|
...util.normalizeParams(params),
|
|
@@ -521,7 +521,7 @@ export function strictObject(shape, params) {
|
|
|
521
521
|
return new ZodObject({
|
|
522
522
|
type: "object",
|
|
523
523
|
get shape() {
|
|
524
|
-
util.assignProp(this, "shape",
|
|
524
|
+
util.assignProp(this, "shape", util.objectClone(shape));
|
|
525
525
|
return this.shape;
|
|
526
526
|
},
|
|
527
527
|
catchall: never(),
|
|
@@ -533,7 +533,7 @@ export function looseObject(shape, params) {
|
|
|
533
533
|
return new ZodObject({
|
|
534
534
|
type: "object",
|
|
535
535
|
get shape() {
|
|
536
|
-
util.assignProp(this, "shape",
|
|
536
|
+
util.assignProp(this, "shape", util.objectClone(shape));
|
|
537
537
|
return this.shape;
|
|
538
538
|
},
|
|
539
539
|
catchall: unknown(),
|
package/v4/core/api.d.cts
CHANGED
|
@@ -63,11 +63,11 @@ export declare function _xid<T extends schemas.$ZodXID>(Class: util.SchemaClass<
|
|
|
63
63
|
export type $ZodKSUIDParams = StringFormatParams<schemas.$ZodKSUID, "when">;
|
|
64
64
|
export type $ZodCheckKSUIDParams = CheckStringFormatParams<schemas.$ZodKSUID, "when">;
|
|
65
65
|
export declare function _ksuid<T extends schemas.$ZodKSUID>(Class: util.SchemaClass<T>, params?: string | $ZodKSUIDParams | $ZodCheckKSUIDParams): T;
|
|
66
|
-
export type $ZodIPv4Params = StringFormatParams<schemas.$ZodIPv4, "pattern" | "when">;
|
|
67
|
-
export type $ZodCheckIPv4Params = CheckStringFormatParams<schemas.$ZodIPv4, "pattern" | "when">;
|
|
66
|
+
export type $ZodIPv4Params = StringFormatParams<schemas.$ZodIPv4, "pattern" | "when" | "version">;
|
|
67
|
+
export type $ZodCheckIPv4Params = CheckStringFormatParams<schemas.$ZodIPv4, "pattern" | "when" | "version">;
|
|
68
68
|
export declare function _ipv4<T extends schemas.$ZodIPv4>(Class: util.SchemaClass<T>, params?: string | $ZodIPv4Params | $ZodCheckIPv4Params): T;
|
|
69
|
-
export type $ZodIPv6Params = StringFormatParams<schemas.$ZodIPv6, "pattern" | "when">;
|
|
70
|
-
export type $ZodCheckIPv6Params = CheckStringFormatParams<schemas.$ZodIPv6, "pattern" | "when">;
|
|
69
|
+
export type $ZodIPv6Params = StringFormatParams<schemas.$ZodIPv6, "pattern" | "when" | "version">;
|
|
70
|
+
export type $ZodCheckIPv6Params = CheckStringFormatParams<schemas.$ZodIPv6, "pattern" | "when" | "version">;
|
|
71
71
|
export declare function _ipv6<T extends schemas.$ZodIPv6>(Class: util.SchemaClass<T>, params?: string | $ZodIPv6Params | $ZodCheckIPv6Params): T;
|
|
72
72
|
export type $ZodCIDRv4Params = StringFormatParams<schemas.$ZodCIDRv4, "pattern" | "when">;
|
|
73
73
|
export type $ZodCheckCIDRv4Params = CheckStringFormatParams<schemas.$ZodCIDRv4, "pattern" | "when">;
|
package/v4/core/api.d.ts
CHANGED
|
@@ -63,11 +63,11 @@ export declare function _xid<T extends schemas.$ZodXID>(Class: util.SchemaClass<
|
|
|
63
63
|
export type $ZodKSUIDParams = StringFormatParams<schemas.$ZodKSUID, "when">;
|
|
64
64
|
export type $ZodCheckKSUIDParams = CheckStringFormatParams<schemas.$ZodKSUID, "when">;
|
|
65
65
|
export declare function _ksuid<T extends schemas.$ZodKSUID>(Class: util.SchemaClass<T>, params?: string | $ZodKSUIDParams | $ZodCheckKSUIDParams): T;
|
|
66
|
-
export type $ZodIPv4Params = StringFormatParams<schemas.$ZodIPv4, "pattern" | "when">;
|
|
67
|
-
export type $ZodCheckIPv4Params = CheckStringFormatParams<schemas.$ZodIPv4, "pattern" | "when">;
|
|
66
|
+
export type $ZodIPv4Params = StringFormatParams<schemas.$ZodIPv4, "pattern" | "when" | "version">;
|
|
67
|
+
export type $ZodCheckIPv4Params = CheckStringFormatParams<schemas.$ZodIPv4, "pattern" | "when" | "version">;
|
|
68
68
|
export declare function _ipv4<T extends schemas.$ZodIPv4>(Class: util.SchemaClass<T>, params?: string | $ZodIPv4Params | $ZodCheckIPv4Params): T;
|
|
69
|
-
export type $ZodIPv6Params = StringFormatParams<schemas.$ZodIPv6, "pattern" | "when">;
|
|
70
|
-
export type $ZodCheckIPv6Params = CheckStringFormatParams<schemas.$ZodIPv6, "pattern" | "when">;
|
|
69
|
+
export type $ZodIPv6Params = StringFormatParams<schemas.$ZodIPv6, "pattern" | "when" | "version">;
|
|
70
|
+
export type $ZodCheckIPv6Params = CheckStringFormatParams<schemas.$ZodIPv6, "pattern" | "when" | "version">;
|
|
71
71
|
export declare function _ipv6<T extends schemas.$ZodIPv6>(Class: util.SchemaClass<T>, params?: string | $ZodIPv6Params | $ZodCheckIPv6Params): T;
|
|
72
72
|
export type $ZodCIDRv4Params = StringFormatParams<schemas.$ZodCIDRv4, "pattern" | "when">;
|
|
73
73
|
export type $ZodCheckCIDRv4Params = CheckStringFormatParams<schemas.$ZodCIDRv4, "pattern" | "when">;
|
package/v4/core/core.d.cts
CHANGED
|
@@ -30,12 +30,12 @@ export type input<T> = T extends {
|
|
|
30
30
|
_zod: {
|
|
31
31
|
input: any;
|
|
32
32
|
};
|
|
33
|
-
} ?
|
|
33
|
+
} ? T["_zod"]["input"] : unknown;
|
|
34
34
|
export type output<T> = T extends {
|
|
35
35
|
_zod: {
|
|
36
36
|
output: any;
|
|
37
37
|
};
|
|
38
|
-
} ?
|
|
38
|
+
} ? T["_zod"]["output"] : unknown;
|
|
39
39
|
export type { output as infer };
|
|
40
40
|
export interface $ZodConfig {
|
|
41
41
|
/** Custom error map. Overrides `config().localeError`. */
|
package/v4/core/core.d.ts
CHANGED
|
@@ -30,12 +30,12 @@ export type input<T> = T extends {
|
|
|
30
30
|
_zod: {
|
|
31
31
|
input: any;
|
|
32
32
|
};
|
|
33
|
-
} ?
|
|
33
|
+
} ? T["_zod"]["input"] : unknown;
|
|
34
34
|
export type output<T> = T extends {
|
|
35
35
|
_zod: {
|
|
36
36
|
output: any;
|
|
37
37
|
};
|
|
38
|
-
} ?
|
|
38
|
+
} ? T["_zod"]["output"] : unknown;
|
|
39
39
|
export type { output as infer };
|
|
40
40
|
export interface $ZodConfig {
|
|
41
41
|
/** Custom error map. Overrides `config().localeError`. */
|
package/v4/core/util.cjs
CHANGED
|
@@ -14,6 +14,7 @@ exports.nullish = nullish;
|
|
|
14
14
|
exports.cleanRegex = cleanRegex;
|
|
15
15
|
exports.floatSafeRemainder = floatSafeRemainder;
|
|
16
16
|
exports.defineLazy = defineLazy;
|
|
17
|
+
exports.objectClone = objectClone;
|
|
17
18
|
exports.assignProp = assignProp;
|
|
18
19
|
exports.mergeDefs = mergeDefs;
|
|
19
20
|
exports.cloneDef = cloneDef;
|
|
@@ -132,6 +133,9 @@ function defineLazy(object, key, getter) {
|
|
|
132
133
|
configurable: true,
|
|
133
134
|
});
|
|
134
135
|
}
|
|
136
|
+
function objectClone(obj) {
|
|
137
|
+
return Object.create(Object.getPrototypeOf(obj), Object.getOwnPropertyDescriptors(obj));
|
|
138
|
+
}
|
|
135
139
|
function assignProp(target, prop, value) {
|
|
136
140
|
Object.defineProperty(target, prop, {
|
|
137
141
|
value,
|
package/v4/core/util.d.cts
CHANGED
|
@@ -120,6 +120,7 @@ export declare function nullish(input: any): boolean;
|
|
|
120
120
|
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
|
+
export declare function objectClone(obj: object): any;
|
|
123
124
|
export declare function assignProp<T extends object, K extends PropertyKey>(target: T, prop: K, value: K extends keyof T ? T[K] : any): void;
|
|
124
125
|
export declare function mergeDefs(...defs: Record<string, any>[]): any;
|
|
125
126
|
export declare function cloneDef(schema: schemas.$ZodType): any;
|
package/v4/core/util.d.ts
CHANGED
|
@@ -120,6 +120,7 @@ export declare function nullish(input: any): boolean;
|
|
|
120
120
|
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
|
+
export declare function objectClone(obj: object): any;
|
|
123
124
|
export declare function assignProp<T extends object, K extends PropertyKey>(target: T, prop: K, value: K extends keyof T ? T[K] : any): void;
|
|
124
125
|
export declare function mergeDefs(...defs: Record<string, any>[]): any;
|
|
125
126
|
export declare function cloneDef(schema: schemas.$ZodType): any;
|
package/v4/core/util.js
CHANGED
|
@@ -86,6 +86,9 @@ export function defineLazy(object, key, getter) {
|
|
|
86
86
|
configurable: true,
|
|
87
87
|
});
|
|
88
88
|
}
|
|
89
|
+
export function objectClone(obj) {
|
|
90
|
+
return Object.create(Object.getPrototypeOf(obj), Object.getOwnPropertyDescriptors(obj));
|
|
91
|
+
}
|
|
89
92
|
export function assignProp(target, prop, value) {
|
|
90
93
|
Object.defineProperty(target, prop, {
|
|
91
94
|
value,
|
package/v4/core/versions.cjs
CHANGED
package/v4/core/versions.js
CHANGED