-
-
Notifications
You must be signed in to change notification settings - Fork 180
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot use Zod .transform #416
Comments
any update on this issue? as you can see in the console the error occurred at the zod, which indicates to me the schema could be invalid. |
I'll see if I can put together a reproduction repo later after the weekend and get back to you. Thanks :) |
Please open a new issue thank you |
Same issue here. This does not work: const fileListSchema = z.custom<FileList>((val) => val instanceof FileList);
const schema = z.object({
avatar: fileListSchema.transform((fileList) => fileList.item(0)), // This does not work
});
const { register, handleSubmit, getValues, formState } = useForm<z.infer<typeof schema>>({
resolver: zodResolver(schema),
});
|
@alexgleason Please open a new issue |
I think I found a solution by looking into the useForm generics definition: As we can see we have three generics being passed: const fileListSchema = z.custom<FileList>((val) => val instanceof FileList);
const schema = z.object({
avatar: fileListSchema.transform((fileList) => fileList.item(0)), // This does not work
});
const { register, handleSubmit, getValues, formState } = useForm<z.input<typeof schema>, object, z.output<typeof schema>>({
resolver: zodResolver(schema),
}); Basically you can defined input, context and output as the generics Docs of zod: https://zod.dev/?id=type-inference This way when you defined defautValues with these generics you get the type before transform and when you call handleSubmit it will be after transform. Hopefully this helps others. |
You just saved me a lot of pain. Thank you! |
I am doing this but getting an TS error. I can see in the types for useForm that the TTransformedValues generic type is never passed to the UseFormProps, thus the type is giving my this error. What to do here? It wants the output to be the same as the input. ![]() |
@kainbryanjones There is an ongoing PR #753 to fix this issue. |
Describe the bug
Unable to use
.transform
with the zod resolverTo Reproduce

Use an input and see an error
Codesandbox link (Required)
https://codesandbox.io/s/react-hook-form-zod-resolver-example-forked-khj93c?file=/src/App.tsx
Expected behavior
The transform happens as expected, and errors thrown during transformation are treated as form errors
Desktop (please complete the following information):
Additional context
Replaces #415 (Error carried forward)
The text was updated successfully, but these errors were encountered: