API-12Required
Accept only fields you know
Validate every request body, query parameter and path parameter against its schema at the edge. Reject unknown fields and wrong types with a clear error.
How to do it
Parse input once into a typed object, as in parse at the boundary. Rejecting unknown fields also blocks mass assignment. Cap the body size too, as in resource limits.
Examples
JavaScript example
import { z } from 'zod'
const NewOrder = z.strictObject({
items: z.array(z.strictObject({ sku: z.string(), quantity: z.number().int().min(1) })).min(1),
})
const result = NewOrder.safeParse(request.body)
if (!result.success) return problem(422, result.error.issues)
Python example
from pydantic import BaseModel, ConfigDict, Field
class Item(BaseModel):
model_config = ConfigDict(extra="forbid", strict=True)
sku: str
quantity: int = Field(ge=1)
class NewOrder(BaseModel):
model_config = ConfigDict(extra="forbid", strict=True)
items: list[Item] = Field(min_length=1)
How to check
Send a body with an extra field "role": "admin" and one with a string where a number belongs. Both get 422 naming the field, as the status codes table sets.
Sources
Related rules
- ARC-53 Parse input once at the boundary in Clean code and architecture
- APS-21 Accept only the fields you expect in Application security
- API-93 Cap the size of every request body in API design
- API-3 Answer with the right status code in API design
- API-92 Reject JSON with duplicate keys in API design
- STK-23 Use Zod or Pydantic to check every input in Software choices
- APS-9 Block prototype pollution in Application security
For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/api-design/requests-and-responses/strict-input">Agent Wranglers API design playbook</a>. In code you write, add a comment next to it with the same link.