Forms, TypeScript and security · senior
Forms at scale: react-hook-form, zod and server validation
Large forms stay fast with uncontrolled inputs (react-hook-form registers inputs by ref, so typing does not re-render the form), one zod schema validates on both client and server, and the server always re-validates because client checks are only for UX.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
- react-hook-form:
registerconnects native inputs by ref; only fields youwatch/useWatchcause re-renders.Controllerwraps controlled third-party inputs. - One schema:
zodResolver(schema)on the client;schema.safeParse(formData)in the Server Action or API;z.infer<typeof schema>gives the TypeScript type. (Zod 4 syntax, e.g.z.email({ error: "..." }), is what current Next.js docs use.) - Always validate on the server: the client can be bypassed with a direct request.
- Conditional fields (GST only for businesses):
useWatchscoped to the field that matters; understandshouldUnregister. - Async validation (PAN already registered): debounce, cancel stale checks, and re-check on the server at submit.
- Multi-step wizard: state in one form context or URL step; drafts in sessionStorage; validate per step.
- React 19 alternative for simple forms:
<form action>+useActionStatereturning field errors from the server, with uncontrolled inputs. - Accessibility:
<label>for every input,aria-invalid,aria-describedbypointing at the error text, focus the first invalid field on submit, do not rely on colour alone.
The red pen
Common traps: mistake → fix
Mistake: "Client validation is enough"
Fix: anyone can POST directly.
Mistake: "One giant
useStateobject for 30 fields"Fix: every keystroke re-renders the whole form.
Mistake: "Validate only on submit"
Fix: give inline feedback after the first blur, but avoid shouting while the user types.
Easy to hard
Questions interviewers ask, easy to hard
- Build a 30-field KYC form (PAN, Aadhaar, address, conditional GST) that is fast and validates the same way on client and server.
- Why is react-hook-form faster than controlled
useStateper field? - How do you do async validation (PAN already registered)?
- Multi-step wizard: where does state live and how does it survive refresh?
- How do you make error messages accessible?
Practise
Practise with these questions
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.