Skip to content

feat(validation): runtime body_model validation with 422 errors #100

Description

@ZhuchkaTriplesix

Context

  • Route decorators accept body_model (Pydantic v2) and body_schema (dict) for OpenAPI (oxyroute/app.py, src/lib.rs).
  • JSON body is parsed in src/dispatch.rs but not validated against the model/schema at runtime.

Problem

OpenAPI documents request shapes that the framework does not enforce. Handlers must manually validate Pydantic models, unlike FastAPI-style DX.

Proposed change

  • Store validation metadata on RouteEntry at registration.
  • Before the handler runs, validate parsed JSON against body_model (Pydantic) or optional JSON Schema.
  • Return 422 with a normalized error shape on validation failure.
  • Decide handler API: validated model instance vs existing json dict (document breaking vs additive choice).

Files

  • oxyroute/app.py, src/lib.rs, src/dispatch.rs, src/state.rs
  • docs/openapi.md, docs/handlers.md, tests (Pydantic v2)

Acceptance criteria

  • Invalid payload returns 422 without calling the route handler.
  • Valid payload reaches handler with documented shape.
  • OpenAPI generation unchanged or improved.
  • Tests cover Pydantic validation errors.

Framework impact

Less boilerplate in handlers; closer to FastAPI request validation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions