|
| 1 | +import type { Infer, ValidationAdapter } from '$lib/adapters/adapters.js'; |
| 2 | +import { zod } from '$lib/adapters/zod4.js'; |
| 3 | +import { superValidate } from '$lib/superValidate.js'; |
| 4 | +import { stringify } from 'devalue'; |
| 5 | +import { assert, describe, expect, test } from 'vitest'; |
| 6 | +import { z } from 'zod/v4'; |
| 7 | + |
| 8 | +async function validate<T extends Record<string, unknown>>( |
| 9 | + data: T, |
| 10 | + schema: ValidationAdapter<T>, |
| 11 | + strict = false |
| 12 | +) { |
| 13 | + const formInput = new FormData(); |
| 14 | + |
| 15 | + formInput.set('__superform_json', stringify(data)); |
| 16 | + try { |
| 17 | + return await superValidate(formInput, schema, { strict }); |
| 18 | + } catch (err) { |
| 19 | + console.error(err); |
| 20 | + // |
| 21 | + throw err; |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +describe('New discriminatedUnion features', () => { |
| 26 | + test('Unions and pipes', async () => { |
| 27 | + const MyResult = z.discriminatedUnion('status', [ |
| 28 | + // simple literal |
| 29 | + z.object({ status: z.literal('aaa'), data: z.string() }), |
| 30 | + // union discriminator |
| 31 | + z.object({ status: z.union([z.literal('bbb'), z.literal('ccc')]) }), |
| 32 | + // pipe discriminator |
| 33 | + z.object({ status: z.literal('fail').transform((val) => val.toUpperCase()) }) |
| 34 | + ]); |
| 35 | + |
| 36 | + const form = await validate({ status: 'bbb' }, zod(MyResult)); |
| 37 | + expect(form.valid).toBe(true); |
| 38 | + }); |
| 39 | + |
| 40 | + test('Composed unions', async () => { |
| 41 | + const BaseError = z.object({ status: z.literal('failed'), message: z.string() }); |
| 42 | + |
| 43 | + const MyResult = z.discriminatedUnion('status', [ |
| 44 | + z.object({ status: z.literal('success'), data: z.string() }), |
| 45 | + z.discriminatedUnion('code', [ |
| 46 | + BaseError.extend({ code: z.literal(400) }), |
| 47 | + BaseError.extend({ code: z.literal(401) }), |
| 48 | + BaseError.extend({ code: z.literal(500) }) |
| 49 | + ]) |
| 50 | + ]); |
| 51 | + |
| 52 | + const form = await validate({ status: 'failed', message: 'FAIL', code: 401 }, zod(MyResult)); |
| 53 | + expect(form.valid).toBe(true); |
| 54 | + |
| 55 | + assert(form.data.status === 'failed'); |
| 56 | + assert(form.data.message === 'FAIL'); |
| 57 | + assert(form.data.code === 401); |
| 58 | + }); |
| 59 | +}); |
| 60 | + |
| 61 | +describe('Default discriminated union values 1', () => { |
| 62 | + const schema = z.discriminatedUnion('type', [ |
| 63 | + z.object({ type: z.literal('empty') }), |
| 64 | + z.object({ type: z.literal('extra'), options: z.string().array() }) |
| 65 | + ]); |
| 66 | + |
| 67 | + test('Union with schema 1', async () => { |
| 68 | + const form = await validate({ type: 'empty' }, zod(schema)); |
| 69 | + expect(form.valid).toBe(true); |
| 70 | + expect(form.data).toEqual({ type: 'empty' }); |
| 71 | + }); |
| 72 | + |
| 73 | + test('Union with schema 2', async () => { |
| 74 | + const form = await validate({ type: 'extra' } as Infer<typeof schema>, zod(schema), true); |
| 75 | + expect(form.valid).toBe(false); |
| 76 | + expect(form.data).toEqual({ type: 'extra', options: [] }); |
| 77 | + }); |
| 78 | + |
| 79 | + test('Nested discriminated union with default value', async () => { |
| 80 | + const nested = z.object({ |
| 81 | + addresses: z.object({ |
| 82 | + additional: z |
| 83 | + .discriminatedUnion('type', [ |
| 84 | + z.object({ |
| 85 | + type: z.literal('poBox'), |
| 86 | + name: z.string().min(1, 'min len').max(10, 'max len') |
| 87 | + }), |
| 88 | + z.object({ |
| 89 | + type: z.literal('none') |
| 90 | + }) |
| 91 | + ]) |
| 92 | + .default({ |
| 93 | + type: 'none' |
| 94 | + }) |
| 95 | + }) |
| 96 | + }); |
| 97 | + |
| 98 | + const form1 = await superValidate(zod(nested)); |
| 99 | + expect(form1.data.addresses.additional).toEqual({ type: 'none' }); |
| 100 | + |
| 101 | + const form2 = await validate( |
| 102 | + { |
| 103 | + addresses: { |
| 104 | + additional: { |
| 105 | + type: 'poBox', |
| 106 | + name: '#123' |
| 107 | + } |
| 108 | + } |
| 109 | + }, |
| 110 | + zod(nested) |
| 111 | + ); |
| 112 | + |
| 113 | + expect(form2.valid).toBe(true); |
| 114 | + assert(form2.data.addresses.additional?.type === 'poBox'); |
| 115 | + expect(form2.data.addresses.additional.name).toBe('#123'); |
| 116 | + }); |
| 117 | +}); |
| 118 | + |
| 119 | +describe('Default discriminated union values 2', () => { |
| 120 | + const ZodSchema = z.object({ |
| 121 | + addresses: z.object({ |
| 122 | + additional: z.discriminatedUnion('type', [ |
| 123 | + z.object({ |
| 124 | + type: z.literal('poBox'), |
| 125 | + name: z.string().min(1, 'min len').max(10, 'max len') |
| 126 | + }), |
| 127 | + z.object({ |
| 128 | + type: z.literal('none') |
| 129 | + }) |
| 130 | + ]) |
| 131 | + }) |
| 132 | + }); |
| 133 | + const FormSchema = zod(ZodSchema); |
| 134 | + type FormSchema = (typeof FormSchema)['defaults']; |
| 135 | + |
| 136 | + test('Bad', async () => { |
| 137 | + const data = { |
| 138 | + addresses: { |
| 139 | + additional: { |
| 140 | + type: 'poBox', |
| 141 | + name: '' |
| 142 | + } |
| 143 | + } |
| 144 | + } satisfies FormSchema; |
| 145 | + await validate(data, FormSchema); |
| 146 | + }); |
| 147 | + |
| 148 | + test('Good', async () => { |
| 149 | + const data = { |
| 150 | + addresses: { |
| 151 | + additional: { |
| 152 | + type: 'none' |
| 153 | + } |
| 154 | + } |
| 155 | + } satisfies FormSchema; |
| 156 | + await validate(data, FormSchema); |
| 157 | + }); |
| 158 | +}); |
| 159 | + |
| 160 | +test('Default value with *matching* type in nested discriminated union with superRefine', async () => { |
| 161 | + const ZodSchema2 = z |
| 162 | + .object({ |
| 163 | + type: z.literal('additional'), |
| 164 | + additional: z |
| 165 | + .discriminatedUnion('type', [ |
| 166 | + z.object({ |
| 167 | + type: z.literal('same'), |
| 168 | + address: z.string().nullable() |
| 169 | + }), |
| 170 | + z.object({ |
| 171 | + type: z.literal('different'), |
| 172 | + address: z.string() |
| 173 | + }) |
| 174 | + ]) |
| 175 | + .default({ |
| 176 | + type: 'same', |
| 177 | + address: null |
| 178 | + }) |
| 179 | + }) |
| 180 | + .superRefine((_data, ctx) => { |
| 181 | + ctx.addIssue({ |
| 182 | + code: z.ZodIssueCode.custom, |
| 183 | + path: ['addresses', 'additional', 'name'], |
| 184 | + message: 'error' |
| 185 | + }); |
| 186 | + }); |
| 187 | + |
| 188 | + const FormSchema = zod(ZodSchema2); |
| 189 | + type FormSchema = (typeof FormSchema)['defaults']; |
| 190 | + const data = { |
| 191 | + type: 'additional', |
| 192 | + additional: { |
| 193 | + type: 'different', |
| 194 | + address: '123 Main St' |
| 195 | + } |
| 196 | + } satisfies FormSchema; |
| 197 | + await validate(data, FormSchema); |
| 198 | +}); |
0 commit comments