{"success":true,"name":"validate-input-at-the-border-one-gate-where-bad-data-is-stop","description":"Building an API endpoint that accepts data from forms or clients","category":"ai-brimind","path":"ai-brimind/validate-input-at-the-border-one-gate-where-bad-data-is-stop/SKILL.md","file":"SKILL.md","content":"---\nname: validate-input-at-the-border-one-gate-where-bad-data-is-stop\ndescription: \"Building an API endpoint that accepts data from forms or clients\"\n---\n\n# Validate Input at the Border: One Gate Where Bad Data Is Stopped and Named\n\n## When to use\nBuilding an API endpoint that accepts data from forms or clients\n\n## Example request\nEndpoint trusts that the frontend form validated everything → direct API calls skip the form entirely; frontend validation is UX, backend validation is the actual gate. Missing 'email' crashes deep inside the DB layer with a cryptic error → the problem was detectable at line one of the endpoint; validate at the border and fail with a clear 400 and a named field. Validation checks are sprinkled across five files, each catching different things → nobody knows what 'valid' means for this endpoint; one schema/validator at the top is the single source of truth for the shape. A string arrives where a number is expected and gets saved as-is → later math produces NaN in production; type checks belong at the entry, this is the 'parse, don't assume' idea.\n\n## Instructions\nAct as a border guard for your API: every piece of client data is a visitor, and there is exactly one checkpoint where papers are checked — the top of the endpoint, before any logic or DB call runs. For each endpoint receiving input, do this: (1) Write down the expected shape first: which fields, which types, which are required, what ranges or formats (email format, positive quantity, enum of allowed statuses). If you cannot write this list, you do not understand the endpoint yet — that is the real problem to fix first. (2) Enforce that shape in ONE place at the entry, using the mechanism your stack already offers (a validation library or schema validator your project already uses — check before hand-rolling if-chains; input validation is a solved, named problem). (3) On failure, reject immediately with a 400-style response that names the field and the reason ('quantity must be a positive number') — the frontend developer debugging at midnight is your user here. Never let invalid data travel deeper to crash as a confusing DB error three layers down. (4) Distinguish validation (shape and format — border's job) from business rules (does this coupon apply, is stock sufficient — logic's job, using DB facts). Keep them separate so each stays readable. (5) Keep frontend form validation too, but say what it is: instant feedback for honest users — the backend gate is the one that counts. Only mark the endpoint done once every field used by the logic below has been checked above, and you can state where the single gate is and what exact message a client gets for each way their input can be wrong.\n\n## Metadata\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #196\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}