Open History to see the last commit
1import { describe, expectTypeOf, it } from 'vitest'
2import { z } from 'zod'
3import { formOptions } from '../src'
4import { parseStandardSchema, validateServerValues } from '../src/internals'
5import type {
6 FieldValidatorContext,
7 FieldValidators,
8 FormGroupValidators,
9 FormValidators,
10 ToFormErrorTypes,
11 ToFormGroupErrorTypes,
12 ToFormGroupOutputs,
13 ToFormOutputs,
14 ValidationErrorMap,
15 ValidationIssue,
16 ValidationOutput,
17} from '../src'
18β
19type Data = { name: string }
20β
21function defineValidators<const T extends FormValidators<Data>>(
22 validators: T,
23): T {
24 return validators
25}
26β
27function defineGroupValidators<const T extends FormGroupValidators<Data>>(
28 validators: T,
29): T {
30 return validators
31}
32β
33describe('validator output types', () => {
34 it('retains the schema output type in parsed output wrappers', () => {
35 const result = parseStandardSchema(
36 z.object({ name: z.string() }),
37 { name: 'Tony' },
38 'form',
39 )
40β
41 expectTypeOf<
42 Extract<Awaited<typeof result>, ValidationOutput<unknown>>
43 >().toEqualTypeOf<ValidationOutput<{ name: string }>>()
44 })
45β
46 it('infers mixed schema and sync/async function outputs without raw error returns', () => {
47 formOptions({
48 defaultValues: { name: '' },
49 validators: [
50 { run: z.object({ name: z.string() }), triggers: [] },
51 {
52 run: ({ value, createOutput }) => createOutput(value.name.length),
53 triggers: [],
54 },
55 { run: () => undefined, triggers: [] },
56 {
57 run: async ({ value, createOutput }) => {
58 if (!value.name) return 'Required'
59 return createOutput({ name: value.name })
60 },
61 triggers: [],
62 },
63 ],
64 onSubmit: ({ validatorOutputs }) => {
65 expectTypeOf(validatorOutputs).toEqualTypeOf<
66 readonly [{ name: string }, number, undefined, { name: string }]
67 >()
68 },
69 })
70 })
71β
72 it('excludes branded payloads from form and routed field error inference', () => {
73 const validators = defineValidators([
74 {
75 run: ({ value, createOutput }) => {
76 if (!value.name)
77 return { fields: { name: { message: 'Required', code: 400 } } }
78 return createOutput({
79 message: 'Data',
80 fields: { name: { message: 'Data', payloadOnly: true } },
81 })
82 },
83 triggers: [],
84 },
85 ])
86β
87 expectTypeOf<ToFormErrorTypes<typeof validators, never>>().toEqualTypeOf<{
88 readonly formError: never
89 readonly fieldError: { message: string; code: number }
90 }>()
91 expectTypeOf<ToFormOutputs<typeof validators>>().toEqualTypeOf<
92 readonly [
93 {
94 message: string
95 fields: { name: { message: string; payloadOnly: boolean } }
96 },
97 ]
98 >()
99β
100 const outputOnly = defineValidators([
101 { run: ({ createOutput }) => createOutput('data'), triggers: [] },
102 ])
103 expectTypeOf<ToFormErrorTypes<typeof outputOnly, never>>().toEqualTypeOf<{
104 readonly formError: never
105 readonly fieldError: never
106 }>()
107 })
108β
109 it('includes undefined for successful scalar returns without an output', () => {
110 const validators = defineValidators([
111 {
112 run: ({ value, createOutput }) =>
113 value.name ? createOutput(1) : undefined,
114 triggers: [],
115 },
116 {
117 run: ({ value, createOutput }) => (value.name ? createOutput(1) : null),
118 triggers: [],
119 },
120 {
121 run: ({ value, createOutput }) =>
122 value.name ? createOutput(1) : false,
123 triggers: [],
124 },
125 { run: ({ createOutput }) => createOutput(undefined), triggers: [] },
126 ])
127β
128 expectTypeOf<ToFormOutputs<typeof validators>>().toEqualTypeOf<
129 readonly [
130 number | undefined,
131 number | undefined,
132 number | undefined,
133 undefined,
134 ]
135 >()
136 })
137β
138 it('accounts for empty collections without discarding their possible errors', () => {
139 const validators = defineValidators([
140 {
141 run: ({ createOutput }): ValidationOutput<number> | ValidationIssue[] =>
142 createOutput(1),
143 triggers: [],
144 },
145 {
146 run: ({
147 createOutput,
148 }): ValidationOutput<number> | ValidationErrorMap<Data> =>
149 createOutput(1),
150 triggers: [],
151 },
152 {
153 run: ({ createOutput }): ValidationOutput<number> | [ValidationIssue] =>
154 createOutput(1),
155 triggers: [],
156 },
157 {
158 run: ({
159 createOutput,
160 }): ValidationOutput<number> | { fields: { name: string } } =>
161 createOutput(1),
162 triggers: [],
163 },
164 {
165 run: ({
166 createOutput,
167 }): ValidationOutput<number> | { fields: { name?: string } } =>
168 createOutput(1),
169 triggers: [],
170 },
171 {
172 run: ({
173 createOutput,
174 }): ValidationOutput<number> | { form: string; fields: {} } =>
175 createOutput(1),
176 triggers: [],
177 },
178 {
179 run: ({
180 createOutput,
181 }):
182 | ValidationOutput<number>
183 | { form: []; fields: { name: undefined } } => createOutput(1),
184 triggers: [],
185 },
186 {
187 run: ({
188 createOutput,
189 }): ValidationOutput<number> | { message: string; fields: {} } =>
190 createOutput(1),
191 triggers: [],
192 },
193 ])
194β
195 expectTypeOf<ToFormOutputs<typeof validators>>().toEqualTypeOf<
196 readonly [
197 number | undefined,
198 number | undefined,
199 number,
200 number,
201 number | undefined,
202 number,
203 number | undefined,
204 number,
205 ]
206 >()
207 type Errors = ToFormErrorTypes<typeof validators, never>
208 expectTypeOf<ValidationIssue>().toExtend<Errors['formError']>()
209 expectTypeOf<ValidationIssue>().toExtend<Errors['fieldError']>()
210 expectTypeOf<Errors['fieldError']>().toExtend<ValidationIssue>()
211 })
212β
213 it('preserves submit eligibility and conservative broad validator outputs', () => {
214 const validators = defineValidators([
215 {
216 run: ({ createOutput }) => createOutput(1),
217 triggers: [],
218 skipOnSubmit: true,
219 },
220 {
221 run: ({ createOutput }) => createOutput(1),
222 triggers: [],
223 skipOnSubmit: false as boolean,
224 },
225 {
226 run: ({ createOutput }) => createOutput(1),
227 triggers: [],
228 skipOnSubmit: () => false,
229 },
230 {
231 run: ({ createOutput }) => createOutput(1),
232 triggers: [],
233 skipOnSubmit: false,
234 },
235 ])
236β
237 expectTypeOf<ToFormOutputs<typeof validators>>().toEqualTypeOf<
238 readonly [undefined, number | undefined, number | undefined, number]
239 >()
240 expectTypeOf<ToFormOutputs<FormValidators<Data>>>().toEqualTypeOf<
241 unknown[]
242 >()
243 })
244β
245 it('infers group outputs and excludes them from group errors', () => {
246 const validators = defineGroupValidators([
247 {
248 run: async ({ value, createOutput }) => {
249 if (!value.name) return 'Required'
250 return createOutput(value.name.length)
251 },
252 triggers: [],
253 },
254 ])
255β
256 expectTypeOf<ToFormGroupOutputs<typeof validators>>().toEqualTypeOf<
257 readonly [number]
258 >()
259 expectTypeOf<ToFormGroupErrorTypes<typeof validators>>().toEqualTypeOf<{
260 readonly formError: ValidationIssue
261 readonly fieldError: ValidationIssue
262 }>()
263 })
264β
265 it('infers server outputs independently of client submit eligibility', async () => {
266 const options = formOptions({
267 defaultValues: { name: '' },
268 validators: [
269 {
270 run: ({ createOutput }) => createOutput('client'),
271 triggers: ['change'],
272 },
273 {
274 run: async ({ value, createOutput }) =>
275 createOutput(value.name.length),
276 triggers: ['server'],
277 skipOnSubmit: true,
278 },
279 ],
280 })
281 const result = await validateServerValues(options, { name: 'Tony' })
282 if (!result.success) throw new Error('Expected success')
283β
284 expectTypeOf(result.validatorOutputs).toEqualTypeOf<
285 readonly [undefined, number]
286 >()
287 })
288β
289 it('does not expose or accept branded outputs in field validators', () => {
290 expectTypeOf<'createOutput'>().not.toExtend<
291 keyof FieldValidatorContext<'name', string, Data>
292 >()
293 type OutputValidator = {
294 run: () => ValidationOutput<string>
295 triggers: []
296 }
297 expectTypeOf<readonly [OutputValidator]>().not.toExtend<
298 FieldValidators<Data, 'name', string>
299 >()
300 })
301})