Closed mintchkin closed 9 months ago
fwiw, I also pushed a PR to @hookform/resolvers
that would allow full inference for zod resolvers when it's coupled with this one
const schema = z.object({ username: z.string(), password: z.string() });
const resolver = zodResolver(schema);
export const action = async ({ request }: ActionFunctionArgs) => {
// data is appropriately typed here
const data = await getValidatedFormData(request, resolver);
// ...
}
@mintchkin thank you for this! So the generic is passed to the resolver and it should work as long as the upstream supports type inference?
@AlemTuzlak yeah exactly, the Resolver<>
type in react-hook-form accepts a generic argument for the shape of the object it returns, so it's possible to go
const schema = z.object({ ... }) // -> Schema<T>
const resolver = zodResolver(schema) // -> Resolver<T>
const result = getValidatedFormData(req, resolver) // -> Promise<T>
without losing any type information, as long as zodResolver()
and getValidatedFormData()
are good about forwarding the T
generic along the chain. unfortunately they both drop it right now, but this PR fixes that for getValidatedFormData()
and the upstream PR fixes it for zodResolver()
@mintchkin awesome, thank you for this!
Description
Small update that should allow inferring the schema type from the resolver type when calling
getValidatedFormData
orvalidateFormData
.Partially fixes #55, though there's still an annoying upstream issue in
@hookform/resolvers
that causeszodResolver()
to lack type inference as was called out in this comment (some other resolvers e.g.yupResolver
should work fine)Before:
After:
Type of change
Please delete options that are not relevant.
Checklist: