simdb.remote.core.pydantic_utils module

class simdb.remote.core.pydantic_utils.Body[source]

Bases: _ParamSource

Marker: populate this parameter from the JSON request body.

class simdb.remote.core.pydantic_utils.Header[source]

Bases: _ParamSource

Marker: populate this parameter from request.headers.

class simdb.remote.core.pydantic_utils.Query[source]

Bases: _ParamSource

Marker: populate this parameter from request.args (query string).

exception simdb.remote.core.pydantic_utils.ResponseException(message, return_code=400)[source]

Bases: Exception

Raised when a client error has occurred in the request (HTTP 4xx).

exception simdb.remote.core.pydantic_utils.ServerException(message, return_code=500)[source]

Bases: Exception

Raised when an unexpected server-side error has occurred (HTTP 500).

simdb.remote.core.pydantic_utils.pydantic_validate(ns: Namespace | Api, *, response_model: type[BaseModel] | None = None, error_model: type[BaseModel] = <class 'simdb.remote.models.ErrorResponse'>, client_error_codes: tuple[int, ...]=(400, )) Any[source]

Decorator factory that wires up Pydantic validation for a Flask-RESTX endpoint.

Inspects the decorated function’s signature for parameters annotated with Annotated[SomePydanticModel, Header()], Annotated[SomePydanticModel, Body()] or Annotated[SomePydanticModel, Query()], validates the corresponding parts of the incoming request, and injects the validated model instances as keyword arguments.

All discovered input models are automatically registered with ns for Swagger/OpenAPI documentation. Body models are registered as @ns.expect models; header/query models are registered as parser arguments.

If the function’s return annotation is a BaseModel subclass (or if response_model is provided explicitly) the return value is automatically serialised with model_dump(mode="json") and wrapped in jsonify.

Error handling distinguishes between client errors and server errors:

  • ResponseException (and request validation errors) → HTTP 4xx (default 400). Use return_code to customise (e.g. 404, 422).

  • ServerException → HTTP 5xx (default 500). Use for explicit server-side failures.

  • Any other unhandled Exception → HTTP 500, logged as an error.

Parameters:
ns: Namespace | Api

The Flask-RESTX Namespace (or Api) to register models on.

response_model: type[BaseModel] | None = None

Optional explicit response model. If None the decorator tries to infer it from the function’s return annotation.

error_model: type[BaseModel] = <class 'simdb.remote.models.ErrorResponse'>

Pydantic model used to serialise error responses.

client_error_codes: tuple[int

HTTP status codes to document as client error responses in Swagger.

Return type:

A decorator suitable for use on Flask-RESTX Resource methods.

Example

from typing import Annotated
from simdb.remote.core.pydantic_utils import pydantic_validate, Header, Body

class SimulationList(Resource):
    @pydantic_validate(api)
    def get(
        self,
        user: User,
        pagination: Annotated[PaginationData, Header()],
    ) -> PaginatedResponse:
        ...

    @pydantic_validate(api)
    def post(
        self,
        user: User,
        body: Annotated[SimulationPostData, Body()],
    ) -> SimulationPostResponse:
        ...