Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions packages/zod/src/v4/core/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3545,8 +3545,8 @@ export const $ZodReadonly: core.$constructor<$ZodReadonly> = /*@__PURE__*/ core.
$ZodType.init(inst, def);
util.defineLazy(inst._zod, "propValues", () => def.innerType._zod.propValues);
util.defineLazy(inst._zod, "values", () => def.innerType._zod.values);
util.defineLazy(inst._zod, "optin", () => def.innerType._zod.optin);
util.defineLazy(inst._zod, "optout", () => def.innerType._zod.optout);
util.defineLazy(inst._zod, "optin", () => def.innerType?._zod?.optin);
util.defineLazy(inst._zod, "optout", () => def.innerType?._zod?.optout);

inst._zod.parse = (payload, ctx) => {
const result = def.innerType._zod.run(payload, ctx);
Expand Down Expand Up @@ -3767,10 +3767,10 @@ export const $ZodLazy: core.$constructor<$ZodLazy> = /*@__PURE__*/ core.$constru
// return () => _innerType;
// });
util.defineLazy(inst._zod, "innerType", () => def.getter() as $ZodType);
util.defineLazy(inst._zod, "pattern", () => inst._zod.innerType._zod.pattern);
util.defineLazy(inst._zod, "propValues", () => inst._zod.innerType._zod.propValues);
util.defineLazy(inst._zod, "optin", () => inst._zod.innerType._zod.optin ?? undefined);
util.defineLazy(inst._zod, "optout", () => inst._zod.innerType._zod.optout ?? undefined);
util.defineLazy(inst._zod, "pattern", () => inst._zod.innerType?._zod?.pattern);
util.defineLazy(inst._zod, "propValues", () => inst._zod.innerType?._zod?.propValues);
util.defineLazy(inst._zod, "optin", () => inst._zod.innerType?._zod?.optin ?? undefined);
util.defineLazy(inst._zod, "optout", () => inst._zod.innerType?._zod?.optout ?? undefined);
inst._zod.parse = (payload, ctx) => {
const inner = inst._zod.innerType;
return inner._zod.run(payload, ctx);
Expand Down
45 changes: 45 additions & 0 deletions packages/zod/src/v4/core/tests/recursive-tuples.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { describe, expect, it } from "vitest";
import * as z from "zod/v4";

describe("Recursive Tuples Regression #5089", () => {
it("creates recursive tuple without crash", () => {
expect(() => {
const y = z.lazy((): any => z.tuple([y, y]).or(z.string()));
}).not.toThrow();
});

it("parses recursive tuple data correctly", () => {
const y = z.lazy((): any => z.tuple([y, y]).or(z.string()));

// Base case
expect(y.parse("hello")).toBe("hello");

// Recursive cases
expect(() => y.parse(["a", "b"])).not.toThrow();
expect(() => y.parse(["a", ["b", "c"]])).not.toThrow();
});

it("matches #5089 expected behavior", () => {
// Exact code from the issue
expect(() => {
const y = z.lazy((): any => z.tuple([y, y]).or(z.string()));
y.parse(["a", ["b", "c"]]);
}).not.toThrow();
});

it("handles workaround pattern", () => {
// Alternative pattern from issue discussion
expect(() => {
const y = z.lazy((): any => z.string().or(z.lazy(() => z.tuple([y, y]))));
y.parse(["a", ["b", "c"]]);
}).not.toThrow();
});

it("recursive arrays still work (comparison)", () => {
const y = z.lazy((): any => z.array(y).or(z.string()));

expect(y.parse("hello")).toBe("hello");
expect(y.parse(["hello", "world"])).toEqual(["hello", "world"]);
expect(y.parse(["a", ["b", "c"]])).toEqual(["a", ["b", "c"]]);
});
});