{"success":true,"name":"validate-next-js-server-action-inputs-like-a-public-api","description":"Writing a Next.js Server Action that receives form or client data","category":"ai-brimind","path":"ai-brimind/validate-next-js-server-action-inputs-like-a-public-api/SKILL.md","file":"SKILL.md","content":"---\nname: validate-next-js-server-action-inputs-like-a-public-api\ndescription: \"Writing a Next.js Server Action that receives form or client data\"\n---\n\n# Validate Next.js Server Action Inputs Like a Public API\n\n## When to use\nProject is Next.js App Router, and a Server Action reads formData or arguments and uses them without server-side validation.\n\n## Example request\ntrusting formData because 'only my form calls this' → Server Actions are public HTTP endpoints; anyone can call them with any payload. — Actions are public surface. | relying on the input's required attribute and client checks → the browser form is a courtesy, not a guard; the server must re-check. — Client validation is UX, not security. | passing the user id from a hidden form field into the update → an attacker edits the field and updates someone else's data; read the id from the server session. — Never trust identity from the client. | hand-writing twenty if-checks per field → a schema validator (like zod) declares the shape once and gives clear errors. — Schema over ad-hoc ifs.\n\n## Instructions\nAct like a border-control officer: every Server Action is a border crossing, and nothing passes without papers. For each Next.js Server Action: 1) Say the fact out loud: a Server Action compiles to a public HTTP endpoint. 'Only my form calls it' is never true — anyone with the app open can craft a request. So the action itself must validate, authorize, and sanitize. 2) Validate the shape on the server: define the expected fields, types, and limits — preferably with a schema library like zod, parsed at the top of the action — and reject anything that does not match, returning clear field errors the form can display. Client-side required attributes and checks are user experience only; they defend nothing. 3) Never accept identity or permissions from the payload: user ids, roles, and prices come from the server session or the database, never from hidden fields or client arguments. Recompute anything money- or permission-related server-side. 4) Check authorization per action: is THIS session allowed to modify THIS record? Ownership checks belong inside the action, next to the write. 5) If the expected shape of the data is unclear, define the schema first and let the form follow it — do not code the write and guess. Only perform the mutation once input shape, identity source, and ownership are all confirmed server-side, and explain why each check exists — not just that the happy-path form submission works.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #505\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}