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
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ export class TypeScriptZodTargetLanguage extends TargetLanguage<
return true;
}

public get supportsUnionsWithMultipleObjectTypes(): boolean {
return true;
}

protected makeRenderer<Lang extends LanguageName = "typescript-zod">(
renderContext: RenderContext,
untypedOptionValues: RendererOptions<Lang>,
Expand Down
10 changes: 9 additions & 1 deletion test/unit/named-class-union.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ function schemaWithReusedRef(): object {
}

async function generate(
lang: "python" | "typescript",
lang: "python" | "typescript" | "typescript-zod",
operation: "oneOf" | "anyOf" = "oneOf",
schema: object = schemaWithUnion(operation),
): Promise<string> {
Expand Down Expand Up @@ -114,6 +114,14 @@ describe("named class unions (issue #1646)", () => {
expect(output).toContain("baz: Optional[str] = None");
});

test("TypeScript Zod preserves the named alternatives", async () => {
const output = await generate("typescript-zod");

expect(output).toContain("export const FooSchema = z.object({");
expect(output).toContain("export const BarSchema = z.object({");
expect(output).toContain('"op": z.union([FooSchema, BarSchema])');
});

test("languages that do not opt in keep the existing behavior", async () => {
const output = await generate("typescript");

Expand Down