Sub topic 7
Connecting Zod to the form with zodResolver
This chapter assumes you can build a form with useForm, register, and handleSubmit. It also assumes you can write a Zod object schema and derive a type with z.infer. Here you connect the two so that the schema becomes the form's only source of validation rules.
What a resolver does
A resolver is a function that useForm calls in place of its built-in validation. React Hook Form passes it the current values. The resolver returns either clean values or a set of errors.
zodResolver comes from the @hookform/resolvers package. It wraps a Zod schema in that function shape. It runs the schema against the values and converts each Zod issue into an entry in formState.errors. Each error is keyed by the issue's path, so an issue at ["email"] appears as errors.email.
npm install react-hook-form zod @hookform/resolversWiring it up
Pass the result of zodResolver(schema) to the resolver option. Use the same schema's inferred type as the useForm generic.
import { useForm } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";
import { z } from "zod";
const signUpSchema = z.object({
email: z.string().min(1, "Email is required").email("Enter a valid email"),
password: z.string().min(8, "Use at least 8 characters"),
});
type SignUpValues = z.infer<typeof signUpSchema>;
export function SignUpForm() {
const {
register,
handleSubmit,
formState: { errors },
} = useForm<SignUpValues>({
resolver: zodResolver(signUpSchema),
defaultValues: { email: "", password: "" },
});
return (
<form onSubmit={handleSubmit((values) => console.log(values))} noValidate>
<input type="email" {...register("email")} />
{errors.email && <p role="alert">{errors.email.message}</p>}
<input type="password" {...register("password")} />
{errors.password && <p role="alert">{errors.password.message}</p>}
<button type="submit">Create account</button>
</form>
);
}The noValidate attribute turns off the browser's own checks, so only the schema decides what is valid.
What happens on submit
Your onSubmit callback runs only when the schema passes. The values it receives are the parsed output of the schema, not the raw input.
Common errors
- Passing the schema directly. Write
resolver: zodResolver(schema), notresolver: schema. - Mixing rule sources. Once a resolver is set, rules such as
register("email", { required: true })are ignored. Put every rule in the schema. - Treating empty inputs as missing. An empty text input gives
"", notundefined. A barez.string()accepts it, so add.min(1, "…")for required fields. - Omitting
defaultValues. Without them, fields start asundefined, which produces confusing type errors from Zod and uncontrolled-to-controlled warnings from React. - Mismatched versions. If TypeScript rejects the resolver, upgrade
zodand@hookform/resolverstogether.
Exercise
A form uses zodResolver(schema), and the schema requires name to be a string with at least 1 character. The user leaves name empty and clicks submit. Does onSubmit run, and where does the error appear?
Show answer
onSubmit does not run. The resolver fails the schema, and handleSubmit stores the issue in formState.errors.name. The form re-renders so you can show errors.name.message.