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
Related rules
- ARC-82 Limit how deep code recurses on input in Clean code and architecture
- API-18 Name every field that failed in API design
- ARC-70 Stop integer arithmetic from overflowing in Clean code and architecture
- ARC-77 Translate outside models at the boundary in Clean code and architecture
- API-12 Accept only fields you know in API design
- API-30 Tell clients to ignore what they don't know in API design
- API-75 Wrap each outside API in one adapter in API design
- NTF-26 Store phone numbers in E.164 in Notifications
- ANA-2 Validate every event before you store it in Product analytics and experiments
- APS-8 Never rebuild objects from untrusted data in Application security
- APS-42 Treat data from other APIs as untrusted 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/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.