L15 Input validation and dependencies
21 minSuggested first: L14 Middleware and error paths
Outcome
Separate input parsing, validation, dependency resolution, and handler behavior.
Learn
Parsing turns input into a representation the program can use. Validation checks rules on that representation. Dependency injection supplies a collaborator from outside the handler.
Read each boundary on its own: where raw data enters, which rules reject it, and where the handler receives its collaborator.
Worked example
Follow the raw value "08" through the stages. What does the store receive?
raw query value → parse decimal digits (leading zeros allowed) → check 1 through 100
accepted number → resolve store provider → store.read(number)
parse or range failure → return 400 before resolving the store
Predict first, then show the worked explanation
The parse step accepts decimal digit strings, leading zeros included, and turns "08" into 8.
The range check then accepts 8, so the injected store receives read(8). The toy order is parse, validate,
resolve the store, handle; real frameworks may interleave these steps.
Practice and check
Common mistake
Assuming that successful parsing guarantees acceptable data, or that TypeScript checks an incoming HTTP payload.
Practice on a real repository
Try one of the puzzles below.
- Kinds of request parametersArchitecture · Easy · fastapi-fastapi
- Building the failure result of safeParseFind · Easy · colinhacks-zod
Related topics: Validation and parsing, Dependency injection
Practice is optional. Solving a puzzle doesn't complete a lesson, and lessons never change a puzzle's points.
References
- Narrowing (TypeScript Handbook)
- Basic usage (Zod documentation)
- Dependencies (FastAPI documentation)