{"success":true,"name":"validate-every-input-at-the-node-js-api-boundary","description":"Writing a Node.js endpoint that reads body, params, or query.","category":"ai-brimind","path":"ai-brimind/validate-every-input-at-the-node-js-api-boundary/SKILL.md","file":"SKILL.md","content":"---\nname: validate-every-input-at-the-node-js-api-boundary\ndescription: \"Writing a Node.js endpoint that reads body, params, or query.\"\n---\n\n# Validate Every Input at the Node.js API Boundary\n\n## When to use\nProject is a Node.js API, and you are about to use req.body, req.params, or req.query values directly in logic or database calls.\n\n## Example request\nreq.body.age used as a number → clients can send anything, including nothing; validate shape and type first — schema validation (zod/joi). req.query.id dropped into a Mongo query → objects like {$gt:''} can sneak in — this is NoSQL injection; validate it's a plain string. Trusting req.body.role to set permissions → clients can send fields you never asked for — whitelist allowed fields, never spread the body into the DB. Hand-written if-checks for 10 fields → schema validators already own this; declare the shape once.\n\n## Instructions\nAct as a backend developer who treats every byte from the client as hostile until validated. Before using req.body, req.params, or req.query in any logic or query, define the expected shape with a schema validator (zod, joi) at the top of the handler: exact fields, types, and bounds — reject everything else with a clear 4xx. Never spread raw client objects into database calls or updates; whitelist the allowed fields explicitly, because extra fields are how privilege escalation and NoSQL injection ({$gt: ''} style operators) get in. Remember query and param values arrive as strings — parse numbers and booleans deliberately. Distinguish validation (shape is wrong → 400, client's fault) from business errors (shape fine, rule violated → different handling). Prefer one shared schema per route over scattered if-checks. Only pass data onward once it is CONFIRMED to match the declared schema, and explain why the endpoint is now safe against malformed and malicious input, not just the happy path.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #988\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}