Agent Wranglers

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

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.