Server Actions need validation too
Server Actions in Next.js make a mutation look like calling a function from a form. That convenience hides what it is: an endpoint anyone can call with any payload.
Three things every action does first
1. Check who is calling. Hiding the button in the UI is not access control. The first line of an admin action should be the session check.
2. Validate the input. TypeScript types disappear at runtime. I parse with Zod and stop on failure:
const parsed = schema.safeParse(input);
if (!parsed.success) return { ok: false, error: firstError(parsed.error) };
After that line, parsed.data is the only thing the rest of the function touches.
3. Return a result, do not throw. A thrown error becomes a generic failure in production. Returning { ok, error } lets the form show a useful message.
Then revalidate
After a successful write, call revalidatePath or revalidateTag for what changed. If you forget, the page shows old data and the save looks broken.
Side effects should not sink the action
My contact form saved the message, then sent an analytics event. The analytics call failed because of a misnamed environment variable, and the whole submission reported an error even though the message was stored. Anything that is not the point of the action, analytics, notifications, belongs in a try/catch of its own.
One schema, two uses
The same Zod schema can validate on the client for instant feedback and on the server for safety. Keep it in one file and import it from both sides, so they cannot drift.