Runnable example

Validate untrusted boundary payloads

Parse, sanitize, and validate messy external payloads with field normalization and structured error contracts.

Intermediate10–15 minWeb & APIsPython 3.12+ · Standard library only

The problem

External payloads from webhooks, mobile clients, and LLM tool calls are untrusted. They arrive with mixed camelCase and snake_case keys, dirty currency strings, and invalid enum states. A production boundary must sanitize valid variations and return structured errors without crashing.

The contract

  1. Accept mixed camelCase and snake_case keys and normalize them to one canonical shape.
  2. Clean dirty amount strings before conversion and reject impossible values.
  3. Validate required fields and enum states at the boundary.
  4. Return structured error contracts that name the failing field and reason.

Verify it locally

From the root of the FlyPython repository, reproduce the broken starter first, then verify the reviewed solution:

python examples/pydantic-validation/verify.py starter --expect-failure
python examples/pydantic-validation/verify.py solution

Hand it to a coding agent

  1. Clone the repository and reproduce the starter failure with the command above.
  2. Give the task contract ↗ to your coding agent and allow changes only in starter/validator.py.
  3. Run python examples/pydantic-validation/verify.py starter and treat any failure as unfinished work.
  4. Compare the patch with the reference solution ↗ — the goal is the same contract with a small, readable diff, not identical syntax.

Keep going

Where this example sits in the roadmap.

Reviewed 2026-09-02 · Repository content version 1. Continue with the related learning path, the repository directory, or the MCP migration guide for the specification background.