Skip to content

Commit 2e60e27

Browse files
committed
tests: other zod validators
1 parent dd7162f commit 2e60e27

File tree

4 files changed

+42
-10
lines changed

4 files changed

+42
-10
lines changed

deno/lib/__tests__/discriminatedUnions.test.ts

+21
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,27 @@ test("valid - discriminator value of various primitive types", () => {
6464
});
6565
});
6666

67+
test("valid - various zod validator discriminators", () => {
68+
const schema = z.discriminatedUnion("type", [
69+
z.object({ type: z.undefined(), val: z.literal(1) }),
70+
z.object({ type: z.null(), val: z.literal(2) }),
71+
z.object({ type: z.enum(["a", "b", "c"]), val: z.literal(3) }),
72+
]);
73+
74+
expect(schema.parse({ type: undefined, val: 1 })).toEqual({
75+
type: undefined,
76+
val: 1,
77+
});
78+
expect(schema.parse({ type: null, val: 2 })).toEqual({
79+
type: null,
80+
val: 2,
81+
});
82+
expect(schema.parse({ type: "c", val: 3 })).toEqual({
83+
type: "c",
84+
val: 3,
85+
});
86+
});
87+
6788
test("valid - wrapped optional discriminator value ", () => {
6889
const schema = z.discriminatedUnion("type", [
6990
z.object({ type: z.literal("1").optional(), val: z.literal(1) }),

deno/lib/types.ts

-5
Original file line numberDiff line numberDiff line change
@@ -2448,11 +2448,6 @@ export class ZodDiscriminatedUnion<
24482448
* @param params
24492449
*/
24502450

2451-
/**
2452-
* if option is a DU
2453-
* - merge in options
2454-
* - merge in map
2455-
*/
24562451
static create<
24572452
Discriminator extends string,
24582453
Types extends [

src/__tests__/discriminatedUnions.test.ts

+21
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,27 @@ test("valid - discriminator value of various primitive types", () => {
6363
});
6464
});
6565

66+
test("valid - various zod validator discriminators", () => {
67+
const schema = z.discriminatedUnion("type", [
68+
z.object({ type: z.undefined(), val: z.literal(1) }),
69+
z.object({ type: z.null(), val: z.literal(2) }),
70+
z.object({ type: z.enum(["a", "b", "c"]), val: z.literal(3) }),
71+
]);
72+
73+
expect(schema.parse({ type: undefined, val: 1 })).toEqual({
74+
type: undefined,
75+
val: 1,
76+
});
77+
expect(schema.parse({ type: null, val: 2 })).toEqual({
78+
type: null,
79+
val: 2,
80+
});
81+
expect(schema.parse({ type: "c", val: 3 })).toEqual({
82+
type: "c",
83+
val: 3,
84+
});
85+
});
86+
6687
test("valid - wrapped optional discriminator value ", () => {
6788
const schema = z.discriminatedUnion("type", [
6889
z.object({ type: z.literal("1").optional(), val: z.literal(1) }),

src/types.ts

-5
Original file line numberDiff line numberDiff line change
@@ -2448,11 +2448,6 @@ export class ZodDiscriminatedUnion<
24482448
* @param params
24492449
*/
24502450

2451-
/**
2452-
* if option is a DU
2453-
* - merge in options
2454-
* - merge in map
2455-
*/
24562451
static create<
24572452
Discriminator extends string,
24582453
Types extends [

0 commit comments

Comments
 (0)