Skip to content

Configuration

This page documents every parameter of validate_http and how to combine them in production handlers.

@validate_http(
    body=...,
    query=...,
    path=...,
    headers=...,
    request_model=...,
    response_model=...,
    response_by_alias=True,
    response_exclude_none=False,
    response_exclude_unset=False,
    adapter=...,
    error_formatter=...,
    status_code=200,
    legacy_loc=False,
    max_body_bytes=None,
    require_json_content_type=False,
    aggregate_validation_errors=False,
)

Signature overview

Public keyword-only parameters:

  • body
  • query
  • path
  • headers
  • request_model
  • response_model
  • response_by_alias (default True)
  • response_exclude_none (default False)
  • response_exclude_unset (default False)
  • adapter
  • error_formatter
  • status_code (default 200)
  • legacy_loc (default False)
  • max_body_bytes (default None)
  • require_json_content_type (default False)
  • aggregate_validation_errors (default False)

Keyword-only API

validate_http parameters are keyword-only. Prefer explicit names for readability and maintainability.

Parameter details

body

Use body=YourBodyModel to validate JSON request body content.

class CreateBody(BaseModel):
    name: str


@validate_http(body=CreateBody)
def create(req: func.HttpRequest, body: CreateBody) -> dict[str, str]:
    return {"name": body.name}

Behavior:

  • Empty body -> 422 (missing error)
  • Invalid JSON syntax -> 400
  • Invalid field values -> 422

The model's own Pydantic configuration controls coercion and unknown fields. Plain BaseModel defaults are lax coercion plus extra="ignore". For a closed JSON contract, see Strict API boundary models for the reusable ConfigDict(strict=True, extra="forbid") pattern and exact errors.

max_body_bytes

Set a positive byte limit to reject oversized configured bodies with 413 Payload Too Large before JSON decoding or custom-adapter parsing. The default None preserves unlimited application-level behavior. The byte count uses req.get_body(); a declared Content-Length above the limit can be rejected first, but Azure Functions has already buffered the body before user code runs.

@validate_http(body=CreateBody, max_body_bytes=1_048_576)
def create(req: func.HttpRequest, body: CreateBody) -> dict[str, str]:
    return {"name": body.name}

max_body_bytes is an application contract, not transport-level DoS protection. Configure Azure Functions host and/or API Management request-size limits as the outer enforcement boundary.

require_json_content_type

Set require_json_content_type=True with body= to reject non-empty bodies whose media type is neither application/json nor application/*+json with 415 Unsupported Media Type. Matching is case-insensitive and ignores parameters such as ; charset=utf-8.

Empty bodies without Content-Type continue to the existing 422 missing-body path. A non-empty body without Content-Type receives 415. The default False preserves the existing behavior of attempting JSON parsing regardless of the header.

aggregate_validation_errors

The default False preserves fail-fast validation: the first invalid configured source returns a 422. Set aggregate_validation_errors=True to validate every configured source and return body, query, path, and header errors together in that deterministic order. Errors keep their source-prefixed loc values and the existing error_format_version.

Malformed input still returns 400 immediately, and body policy or unexpected adapter failures still return their existing immediate error response. The handler is never called when any validation error was collected.

query

Use query=QueryModel to validate query-string parameters.

Query values arrive as strings. Keep normal Pydantic coercion for fields such as page: int; a model-wide strict=True rejects ?page=2. You can still use ConfigDict(extra="forbid") to reject undeclared query keys. See Strict API boundary models.

class QueryModel(BaseModel):
    page: int = Field(default=1, ge=1)


@validate_http(query=QueryModel)
def list_items(req: func.HttpRequest, query: QueryModel) -> dict[str, int]:
    return {"page": query.page}

path

Use path=PathModel for route variables from @app.route(route="...").

class PathModel(BaseModel):
    item_id: int = Field(ge=1)


@validate_http(path=PathModel)
def get_item(req: func.HttpRequest, path: PathModel) -> dict[str, int]:
    return {"item_id": path.item_id}

headers

Use headers=HeadersModel to validate inbound request headers.

class HeadersModel(BaseModel):
    model_config = ConfigDict(populate_by_name=True)

    x_request_id: str = Field(alias="x-request-id")


@validate_http(headers=HeadersModel)
def inspect(req: func.HttpRequest, headers: HeadersModel) -> dict[str, str]:
    return {"request_id": headers.x_request_id}

Header names are matched case-insensitively against field names and aliases, so X-Request-ID, x-request-id, and X-REQUEST-ID all populate the same field.

request_model (deprecated)

Deprecated — use body

request_model is a deprecated alias for body and emits a DeprecationWarning. Prefer body= (which injects a parameter named body). request_model will be removed in a future release.

request_model is shorthand for body validation and injects a parameter named req_model.

class RequestModel(BaseModel):
    text: str


# Deprecated form (emits DeprecationWarning):
@validate_http(request_model=RequestModel)
def post(req: func.HttpRequest, req_model: RequestModel) -> dict[str, str]:
    return {"text": req_model.text}


# Preferred form:
@validate_http(body=RequestModel)
def post_preferred(req: func.HttpRequest, body: RequestModel) -> dict[str, str]:
    return {"text": body.text}

Mutual exclusivity

Do not combine request_model with body, query, path, or headers.

response_model

Use response_model=ModelOrType to validate your handler output.

class ResultModel(BaseModel):
    status: str


@validate_http(response_model=ResultModel)
def health(req: func.HttpRequest) -> dict[str, str]:
    return {"status": "ok"}

Also supports generic type forms like list[ResultModel].

Validated response serialization can be customized with:

  • response_by_alias=False to emit Python field names instead of serialization aliases.
  • response_exclude_none=True to omit fields whose value is None.
  • response_exclude_unset=True to omit fields that were not explicitly set.

These options require response_model; setting a non-default value without one raises ValueError when the decorator is created. They apply only after response validation. Returning func.HttpResponse still bypasses validation and serialization, so these options do not modify its body. response_exclude_none and response_exclude_unset affect payload presence only and do not change the declared response schema.

Nested Pydantic models inside dataclass return values are serialized too, so a dataclass holding a BaseModel field renders as plain JSON rather than raising a serialization error.

Returning None with a non-Optional response_model

If the handler returns None while response_model does not allow None, response validation fails and the caller gets a sanitized 500 whose body is the generic {"detail": [{"loc": [], "msg": "Internal Server Error", "type": "server_error"}]}. The real cause is logged server-side, never leaked to the caller. Declare the model as Optional (for example response_model=ResultModel | None) when None is a valid outcome; then returning None produces a 204 No Content instead.

adapter

adapter allows plugging a custom implementation of the internal ValidationAdapter protocol.

Default behavior uses PydanticAdapter() when omitted.

@validate_http(body=RequestModel)  # adapter defaults to PydanticAdapter()
def handler(req: func.HttpRequest, body: RequestModel) -> dict[str, str]:
    return {"text": body.text}

Advanced extension point

Most projects should keep the default adapter.

The complete method, lifetime, serialization, and exception contract is in Writing a custom adapter, including the reusable public conformance suite.

Custom adapters use an explicit exception contract for all four request parsing methods (parse_body, parse_query, parse_path, and parse_headers):

  • raise MalformedRequestError for syntactically malformed client input (400)
  • raise AdapterValidationError for model validation failures (422)
  • let every other exception propagate as an adapter fault (500, logged and sanitized)

Plain ValueError is in the last category. There is no compatibility shim for the former ValueError-to-400 behavior; custom adapters must migrate malformed syntax failures to MalformedRequestError.

error_formatter

error_formatter customizes error response payloads per handler.

from typing import Any


def formatter(exc: Exception, status_code: int) -> dict[str, Any]:
    message = str(exc) if status_code < 500 else "Internal Server Error"
    return {
        "error": {
            "code": f"VALIDATION_{status_code}",
            "message": message,
        }
    }


@validate_http(body=RequestModel, error_formatter=formatter)
def handler_custom(req: func.HttpRequest, body: RequestModel) -> dict[str, str]:
    return {"text": body.text}

The formatter receives the original exception for 4xx responses. For 5xx responses it receives a public InternalServerError containing only "Internal Server Error"; the original is logged once with traceback.

expose_internal_errors

expose_internal_errors defaults to False. Setting it to True passes the original 5xx exception to error_formatter, restoring the old behavior. This is unsafe because a formatter can expose internal details to clients.

status_code

status_code sets the HTTP status code for successful responses (default 200). Use it for creation endpoints and similar cases.

@validate_http(body=RequestModel, response_model=ResultModel, status_code=201)
def create_item(req: func.HttpRequest, body: RequestModel) -> ResultModel:
    return ResultModel(status="created")

legacy_loc

legacy_loc (default False) restores pre-0.13 error locations: loc values omit the leading input-source segment, so you get ["email"] instead of ["body", "email"]. It is a one-cycle migration escape hatch and is ignored when a custom adapter is supplied — configure that adapter directly instead.

@validate_http(body=RequestModel, legacy_loc=True)
def legacy(req: func.HttpRequest, body: RequestModel) -> dict[str, str]:
    return {"text": body.text}

Configuration patterns

Pattern 1: simple body API

@validate_http(body=RequestModel, response_model=ResultModel)
def create(req: func.HttpRequest, body: RequestModel) -> ResultModel:
    return ResultModel(status="created")

Pattern 2: route-driven read API

@validate_http(path=PathModel, query=QueryModel, response_model=ResultModel)
def get(req: func.HttpRequest, path: PathModel, query: QueryModel) -> ResultModel:
    return ResultModel(status=f"item={path.item_id},page={query.page}")

Pattern 3: custom error contract

@validate_http(body=RequestModel, response_model=ResultModel, error_formatter=formatter)
def create_custom(req: func.HttpRequest, body: RequestModel) -> ResultModel:
    return ResultModel(status="created")

Defaults and operational behavior

  • adapter defaults to PydanticAdapter().
  • No response_model means output is serialized without response schema validation.
  • Returning func.HttpResponse bypasses response serialization/validation, including all response_* serialization options.
  • 500-level internal pipeline errors are sanitized by default when no formatter is set.

Choose explicit response models

response_model catches accidental contract drift and is strongly recommended for public APIs.