Skip to content

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?

A toy endpointText
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

Guided practice

Practice, not scored

With the raw value "101", where does the request first fail?

Choose one answer

Exit check

Counts toward completion

A test swaps the real store for a fake. In this design, which stage changes?

Choose one answer

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.

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