{"success":true,"name":"validate-and-shape-every-api-input-at-the-route-boundary","description":"Writing or editing an API route handler that receives client data","category":"ai-brimind","path":"ai-brimind/validate-and-shape-every-api-input-at-the-route-boundary/SKILL.md","file":"SKILL.md","content":"---\nname: validate-and-shape-every-api-input-at-the-route-boundary\ndescription: \"Writing or editing an API route handler that receives client data\"\n---\n\n# Validate and Shape Every API Input at the Route Boundary\n\n## When to use\nYou are inside a route/endpoint handler that reads body, query, or params, and you are about to use those values without checking them first.\n\n## Example request\nHandler reads req.body.email straight into the database → a missing field crashes deep inside; boundary validation fails fast with a clear 400. — Query param 'limit' arrives as the string '9999999' → parsed blindly it kills the database; type-checking and capping at the edge is input sanitization. — Client sends extra unexpected fields and they get saved → mass-assignment risk; whitelisting only known fields is the guard. — Same validation rules copy-pasted in 5 routes → they drift; a shared validation schema (one module, reused) is the existing mechanism.\n\n## Instructions\nAct as a gatekeeper developer who treats every route handler as a border checkpoint: nothing from the client is trusted until checked. In the handler: 1) List every input the route reads (body fields, query params, path params, headers). 2) Check whether the project already has a validation mechanism (a schema library, a shared validator module, middleware) — reuse it instead of writing manual if-checks; hand-rolled checks should only be thin glue when nothing exists. 3) For each input define: required or optional, type, allowed range/format, and a safe default. Whitelist known fields; drop everything else. 4) On invalid input, fail fast at the top of the handler with a clear 4xx and a message naming the bad field — never let bad data travel deeper into services or the database. 5) If you are unsure what values a field can legally take, check the contract or ask — do not assume. Only proceed to business logic once inputs are confirmed valid, and explain how the boundary check prevents the deeper failure it guards against, not just that 'validation was added'.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #884\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}