Skip to content

Commit 49a0d7b

Browse files
authored
fix: discriminated union for zod v4 mini (#784)
1 parent bc09647 commit 49a0d7b

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

zod/src/__tests__/__fixtures__/data-v4-mini.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,15 @@ export const schema = z
4343
message: 'Invalid date',
4444
}),
4545
),
46+
auth: z.discriminatedUnion('type', [
47+
z.object({
48+
type: z.literal('registered'),
49+
passwordHash: z.string(),
50+
}),
51+
z.object({
52+
type: z.literal('guest'),
53+
}),
54+
]),
4655
})
4756
.check(
4857
z.refine((obj) => obj.password === obj.repeatPassword, {
@@ -68,6 +77,10 @@ export const validData = {
6877
},
6978
],
7079
dateStr: '2020-01-01',
80+
auth: {
81+
type: 'registered',
82+
passwordHash: 'hash',
83+
},
7184
} satisfies z.input<typeof schema>;
7285

7386
export const invalidData = {
@@ -76,6 +89,7 @@ export const invalidData = {
7689
birthYear: 'birthYear',
7790
like: [{ id: 'z' }],
7891
url: 'abc',
92+
auth: { type: 'invalid' },
7993
} as unknown as z.input<typeof schema>;
8094

8195
export const fields: Record<InternalFieldName, Field['_f']> = {

zod/src/zod.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ function parseZod4Issues(
9191
const _path = path.join('.');
9292

9393
if (!errors[_path]) {
94-
if (error.code === 'invalid_union') {
94+
if (error.code === 'invalid_union' && error.errors.length > 0) {
9595
const unionError = error.errors[0][0];
9696

9797
errors[_path] = {

0 commit comments

Comments
 (0)