Skip to content

Commit e1f1948

Browse files
authored
fix(v4): ensure array defaults are shallow-cloned (#5173)
1 parent 887e37c commit e1f1948

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

packages/zod/src/v4/classic/tests/default.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,14 @@ test("defaulted object schema returns shallow clone", () => {
324324
expect(result1).toEqual(result2);
325325
});
326326

327+
test("defaulted array schema returns shallow clone", () => {
328+
const schema = z.array(z.string()).default(["x"]);
329+
const result1 = schema.parse(undefined);
330+
const result2 = schema.parse(undefined);
331+
expect(result1).not.toBe(result2);
332+
expect(result1).toEqual(result2);
333+
});
334+
327335
test("direction-aware defaults", () => {
328336
const schema = z.string().default("hello");
329337

packages/zod/src/v4/core/util.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,7 @@ export function isPlainObject(o: any): o is Record<PropertyKey, unknown> {
395395

396396
export function shallowClone(o: any): any {
397397
if (isPlainObject(o)) return { ...o };
398+
if (Array.isArray(o)) return [...o];
398399
return o;
399400
}
400401

0 commit comments

Comments
 (0)