Agent Wranglers

ARC-53Required

Parse input once at the boundary

Turn outside input into a typed, valid object as soon as it arrives. Code after that point trusts the type and does not check again.

How to do it

Use a schema validator, such as zod, pydantic or Symfony Validator, in the controller, queue consumer or file reader. Reject bad input there with a clear error. Pass only the parsed object onward.

Examples

Python example

# Bad: every function re-checks the raw dictionary
def book(data: dict):
    if "email" not in data: raise ValueError("email")

# Good: parse once, then trust the type
class BookingRequest(BaseModel):
    email: EmailStr
    start: datetime

def book(request: BookingRequest): ...

TypeScript example

import { z } from 'zod';

// Good: parse once, then trust the type
const BookingRequest = z.object({ email: z.email(), start: z.coerce.date() });
type BookingRequest = z.infer<typeof BookingRequest>;

function book(request: BookingRequest) { /* ... */ }
book(BookingRequest.parse(body));

How to check

Each request handler calls a parser before any other logic. A test sends a request body with a missing and a wrong-typed field. It expects a 422 answer that names both fields, as in validation errors.

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/architecture/data-shapes/parse-at-the-boundary">Agent Wranglers clean code and architecture playbook</a>. In code you write, add a comment next to it with the same link.