Skip to content

Commit

Permalink
Allow creation of discriminated unions with a readonly array of optio…
Browse files Browse the repository at this point in the history
…ns (#3535)

Signed-off-by: Andrew Haines <andrew@haines.org.nz>
  • Loading branch information
haines authored Dec 10, 2024
1 parent b85686a commit 6407bed
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
11 changes: 11 additions & 0 deletions deno/lib/__tests__/discriminated-unions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,3 +308,14 @@ test("optional and nullable", () => {
if (value.key === "b") value.b;
if (value.key === null) value.b;
});

test("readonly array of options", () => {
const options = [
z.object({ type: z.literal("x"), val: z.literal(1) }),
z.object({ type: z.literal("y"), val: z.literal(2) }),
] as const;

expect(
z.discriminatedUnion("type", options).parse({ type: "x", val: 1 })
).toEqual({ type: "x", val: 1 });
});
6 changes: 3 additions & 3 deletions deno/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3165,7 +3165,7 @@ export type ZodDiscriminatedUnionOption<Discriminator extends string> =

export interface ZodDiscriminatedUnionDef<
Discriminator extends string,
Options extends ZodDiscriminatedUnionOption<string>[] = ZodDiscriminatedUnionOption<string>[]
Options extends readonly ZodDiscriminatedUnionOption<string>[] = ZodDiscriminatedUnionOption<string>[]
> extends ZodTypeDef {
discriminator: Discriminator;
options: Options;
Expand All @@ -3175,7 +3175,7 @@ export interface ZodDiscriminatedUnionDef<

export class ZodDiscriminatedUnion<
Discriminator extends string,
Options extends ZodDiscriminatedUnionOption<Discriminator>[]
Options extends readonly ZodDiscriminatedUnionOption<Discriminator>[]
> extends ZodType<
output<Options[number]>,
ZodDiscriminatedUnionDef<Discriminator, Options>,
Expand Down Expand Up @@ -3245,7 +3245,7 @@ export class ZodDiscriminatedUnion<
*/
static create<
Discriminator extends string,
Types extends [
Types extends readonly [
ZodDiscriminatedUnionOption<Discriminator>,
...ZodDiscriminatedUnionOption<Discriminator>[]
]
Expand Down
11 changes: 11 additions & 0 deletions src/__tests__/discriminated-unions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,3 +307,14 @@ test("optional and nullable", () => {
if (value.key === "b") value.b;
if (value.key === null) value.b;
});

test("readonly array of options", () => {
const options = [
z.object({ type: z.literal("x"), val: z.literal(1) }),
z.object({ type: z.literal("y"), val: z.literal(2) }),
] as const;

expect(
z.discriminatedUnion("type", options).parse({ type: "x", val: 1 })
).toEqual({ type: "x", val: 1 });
});
6 changes: 3 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3165,7 +3165,7 @@ export type ZodDiscriminatedUnionOption<Discriminator extends string> =

export interface ZodDiscriminatedUnionDef<
Discriminator extends string,
Options extends ZodDiscriminatedUnionOption<string>[] = ZodDiscriminatedUnionOption<string>[]
Options extends readonly ZodDiscriminatedUnionOption<string>[] = ZodDiscriminatedUnionOption<string>[]
> extends ZodTypeDef {
discriminator: Discriminator;
options: Options;
Expand All @@ -3175,7 +3175,7 @@ export interface ZodDiscriminatedUnionDef<

export class ZodDiscriminatedUnion<
Discriminator extends string,
Options extends ZodDiscriminatedUnionOption<Discriminator>[]
Options extends readonly ZodDiscriminatedUnionOption<Discriminator>[]
> extends ZodType<
output<Options[number]>,
ZodDiscriminatedUnionDef<Discriminator, Options>,
Expand Down Expand Up @@ -3245,7 +3245,7 @@ export class ZodDiscriminatedUnion<
*/
static create<
Discriminator extends string,
Types extends [
Types extends readonly [
ZodDiscriminatedUnionOption<Discriminator>,
...ZodDiscriminatedUnionOption<Discriminator>[]
]
Expand Down

0 comments on commit 6407bed

Please sign in to comment.