Z
Zod•2mo ago
neurotech

neurotech - Similar to the above, I have an inp...

Similar to the above, I have an input element with a type of "number". How do I validate it's value as a number when using zod 4 in combination with react-hook-form? z.coerce's input type change to unknown seems to have made this more difficult.
Solution:
Indeed the same thing. Also same answer 😄 Either omit the genrics completely (@hookform/resolvers properly infers input/output now) or pass both input and output types
Jump to solution
5 Replies
neurotech
neurotechOP•2mo ago
const NewInvoiceSchema = z.object({
buyerAddress: z
.string()
.nonempty({ error: "Please enter a buyer address." })
.regex(mainNetRegex, {
error: "Please enter a valid mainnet address.",
}),
quantity: z.coerce.number().min(1),
pricePer: z.coerce.number().min(1),
});

const form = useForm<z.infer<typeof NewInvoiceSchema>>({
resolver: zodResolver(NewInvoiceSchema),
defaultValues: {
buyerAddress: "",
quantity: 0,
pricePer: 0,
},
mode: "all",
reValidateMode: "onChange",
});
const NewInvoiceSchema = z.object({
buyerAddress: z
.string()
.nonempty({ error: "Please enter a buyer address." })
.regex(mainNetRegex, {
error: "Please enter a valid mainnet address.",
}),
quantity: z.coerce.number().min(1),
pricePer: z.coerce.number().min(1),
});

const form = useForm<z.infer<typeof NewInvoiceSchema>>({
resolver: zodResolver(NewInvoiceSchema),
defaultValues: {
buyerAddress: "",
quantity: 0,
pricePer: 0,
},
mode: "all",
reValidateMode: "onChange",
});
neurotech
neurotechOP•2mo ago
No description
Solution
janglad
janglad•2mo ago
Indeed the same thing. Also same answer 😄 Either omit the genrics completely (@hookform/resolvers properly infers input/output now) or pass both input and output types
neurotech
neurotechOP•2mo ago
Ah that worked, I tried the solution from the other thread earlier but still had errors. Not sure what I did differently this time lol I appreciate you replying, thanks mate
neurotech
neurotechOP•2mo ago
@janglad are you around?

Did you find this page helpful?