Replies: 1 comment
-
|
The type error is intentional by design. TanStack Form infers the form data type from There are a few practical ways to fix this: Option 1: Make the schema's input type accept const schema = z.object({
price: z
.number()
.int('Price must be an integer')
.min(0)
.max(1000000)
.optional()
.refine((v): v is number => v !== undefined, { message: 'Price is required' }),
});
const form = useForm({
defaultValues: { price: undefined as number | undefined },
validators: { onSubmit: schema },
});
Option 2: Field-level validation (avoids schema altogether) const form = useForm({
defaultValues: { price: undefined as number | undefined },
});
// In your JSX:
<form.Field
name="price"
validators={{
onSubmit: ({ value }) => {
if (value === undefined || value === null) return 'Price is required';
if (!Number.isInteger(value)) return 'Price must be an integer';
if (value < 0 || value > 1_000_000) return 'Price must be between 0 and 1,000,000';
},
}}
>Field-level validators don't go through StandardSchemaV1 so there's no type mismatch. Option 3: If your input is an HTML Use coercion and cast the default to match: const schema = z.object({
price: z.coerce.number<string>().int('Price must be an integer').min(0).max(1000000),
});
const form = useForm({
defaultValues: { price: '' as unknown as number },
validators: { onSubmit: schema },
});
The underlying limitation — that form types are inferred from |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
How make
const schema = z.object({ price: z.number().int('Price must be an integer').min(0).max(1000000), })const form = useForm({ defaultValues: { price: undefined }, validators: { onSubmit: schema }, })An error occurred in the current code
Beta Was this translation helpful? Give feedback.
All reactions