{"success":true,"name":"validate-the-shape-of-every-input-with-a-schema-server-side","description":"Writing an endpoint that reads fields from a request body or query","category":"ai-brimind","path":"ai-brimind/validate-the-shape-of-every-input-with-a-schema-server-side/SKILL.md","file":"SKILL.md","content":"---\nname: validate-the-shape-of-every-input-with-a-schema-server-side\ndescription: \"Writing an endpoint that reads fields from a request body or query\"\n---\n\n# Validate the Shape of Every Input With a Schema, Server-Side\n\n## When to use\nYou are handling incoming request data (JSON body, query params, form fields) and are about to use the values without a defined validation step.\n\n## Example request\nReading body.quantity straight into the logic → a negative number, a string, or a huge value arrives and math goes wrong; every field needs type and range rules (schema validation). Accepting any string length for a bio → a 10 MB text bloats the database and slows pages; strings need max lengths (bounds). An enum-like field checked nowhere → status:'banana' enters the database and breaks code later; allowed values must be a whitelist (enumeration). Unknown extra fields passed through to the database → attackers smuggle fields you never planned; strip anything not in the schema (allowlist by default).\n\n## Instructions\nAct like a strict doorman for your endpoint: nothing enters without matching the guest list exactly. Before writing the business logic, define the schema of the input — every field's type, whether it is required, its allowed range or length, its allowed values for enum-like fields — and reject anything extra by default instead of passing unknown fields onward. Use the ecosystem's standard validation library or the framework's built-in validators rather than a pile of hand-written if-statements; declared schemas are readable, reusable, and harder to get wrong. Validate at the entry point, server-side, even if the frontend validates too, and convert types deliberately (string to number, string to date) instead of trusting whatever arrived. Return clear 400-style errors listing which fields failed, without echoing dangerous content back. Remember what this layer is for: it enforces shape and bounds, and it complements — never replaces — sink-specific defenses like parameterized queries and output escaping. Only proceed once every consumed field appears in the schema with explicit rules, and explain how the strictness blocks malformed, oversized, and smuggled inputs rather than just guarding the happy path.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #571\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}