Skip to content

Commit fbd4251

Browse files
committed
Fixed test for discriminated union
1 parent 94a28d9 commit fbd4251

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

src/tests/zodUnion.test.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@ import { stringify } from 'devalue';
55
import { describe, expect, test } from 'vitest';
66
import { z } from 'zod';
77

8-
async function validate(data: unknown, schema: ValidationAdapter<Record<string, unknown>>) {
8+
async function validate(
9+
data: unknown,
10+
schema: ValidationAdapter<Record<string, unknown>>,
11+
strict = false
12+
) {
913
const formInput = new FormData();
1014

1115
formInput.set('__superform_json', stringify(data));
1216
try {
13-
return await superValidate(formInput, schema);
17+
return await superValidate(formInput, schema, { strict });
1418
} catch (err) {
1519
console.error(err);
1620
//
@@ -24,8 +28,15 @@ describe('Default discriminated union values 1', () => {
2428
z.object({ type: z.literal('extra'), options: z.string().array() })
2529
]);
2630

27-
test('Union with schema', async () => {
28-
const form = await validate({ type: 'extra' }, zod(schema));
31+
test('Union with schema 1', async () => {
32+
const form = await validate({ type: 'empty' }, zod(schema));
33+
expect(form.valid).toBe(true);
34+
expect(form.data).toEqual({ type: 'empty' });
35+
});
36+
37+
test('Union with schema 2', async () => {
38+
const form = await validate({ type: 'extra' }, zod(schema), true);
39+
expect(form.valid).toBe(false);
2940
expect(form.data).toEqual({ type: 'extra', options: [] });
3041
});
3142
});

0 commit comments

Comments
 (0)