Skip to content

Commit

Permalink
tests: other zod validators
Browse files Browse the repository at this point in the history
  • Loading branch information
maxArturo committed Nov 23, 2022
1 parent dd7162f commit 0503ebf
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
21 changes: 21 additions & 0 deletions deno/lib/__tests__/discriminatedUnions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,27 @@ test("valid - discriminator value of various primitive types", () => {
});
});

test("valid - various zod validator discriminators", () => {
const schema = z.discriminatedUnion("type", [
z.object({ type: z.undefined(), val: z.literal(1) }),
z.object({ type: z.null(), val: z.literal(2) }),
z.object({ type: z.enum(["a", "b", "c"]), val: z.literal(3) }),
]);

expect(schema.parse({ type: undefined, val: 1 })).toEqual({
type: undefined,
val: 1,
});
expect(schema.parse({ type: null, val: 2 })).toEqual({
type: null,
val: 2,
});
expect(schema.parse({ type: "c", val: 3 })).toEqual({
type: "c",
val: 3,
});
});

test("valid - wrapped optional discriminator value ", () => {
const schema = z.discriminatedUnion("type", [
z.object({ type: z.literal("1").optional(), val: z.literal(1) }),
Expand Down
21 changes: 21 additions & 0 deletions src/__tests__/discriminatedUnions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,27 @@ test("valid - discriminator value of various primitive types", () => {
});
});

test("valid - various zod validator discriminators", () => {
const schema = z.discriminatedUnion("type", [
z.object({ type: z.undefined(), val: z.literal(1) }),
z.object({ type: z.null(), val: z.literal(2) }),
z.object({ type: z.enum(["a", "b", "c"]), val: z.literal(3) }),
]);

expect(schema.parse({ type: undefined, val: 1 })).toEqual({
type: undefined,
val: 1,
});
expect(schema.parse({ type: null, val: 2 })).toEqual({
type: null,
val: 2,
});
expect(schema.parse({ type: "c", val: 3 })).toEqual({
type: "c",
val: 3,
});
});

test("valid - wrapped optional discriminator value ", () => {
const schema = z.discriminatedUnion("type", [
z.object({ type: z.literal("1").optional(), val: z.literal(1) }),
Expand Down

0 comments on commit 0503ebf

Please sign in to comment.