Repository navigation
Description
Describe the bug
Tested on @tanstack/react-form@2.0.0-alpha.3: When using formOptions.looseSchema, a Zod schema containing an optional object property is accepted as the loose schema but rejected when the same schema is passed to validators[].run.
The reported type error indicates that InferUnionObject changes an optional property such as:
text?: string | undefined;into a required property whose value may be undefined:
text: string | undefined;The same Zod schema can consequently not be used as the runtime validator.
Reproduction
import { formOptions, useForm } from '@tanstack/react-form';
import z from 'zod';
const schema = z.object({
myProp: z.string().optional(),
});
const Test = () => {
const form = useForm(
formOptions.looseSchema(schema, {
defaultValues: {},
validators: [
{
triggers: ['change'],
run: schema, // throws error
},
],
}),
);
return form;
};TypeScript error
[...] Property 'myProp' is optional in type '{ myProp?: string | undefined; }' but required in type 'InferUnionObject<{}, { myProp?: string | undefined; }>'
Your minimal, reproducible example
https://stackblitz.com/edit/vitejs-vite-pg3hqj4e?file=src%2FForm.tsx&terminal=dev
Steps to reproduce
import { formOptions, useForm } from '@tanstack/react-form'; // v2 alpha 3
import z from 'zod';
const schema = z.object({
myProp: z.string().optional(),
});
const Test = () => {
const form = useForm(
formOptions.looseSchema(schema, {
defaultValues: {},
validators: [
{
triggers: ['change'],
run: schema, // throws error
},
],
}),
);
return form;
};Expected behavior
The schema passed to formOptions.looseSchema should also be accepted as a validator in validators[].run.
In particular, an optional schema property:
myProp?: string;
should remain compatible with the editable loose-schema representation, where the property may be omitted or contain undefined.
How often does this bug happen?
Always
Screenshots or Videos
No response
Platform
Linux
TanStack Form adapter
react-form
TanStack Form version
2.0.0-alpha.3
TypeScript version
7.0.2
Additional context
No response
Activity