Ushka

ushka.http.validation

Type-driven validation of request data, with no dependencies.

coerce turns what the client sent (strings from the URL, JSON from the body) into the type a handler declared, or reports every problem at once, each with its location, like ["body", "items", 0, "price"].

Converting is also sanitising. A value declared int, float, bool, UUID, a date, an Enum or a Literal comes out as a plain value of that type, never as Untrusted: it can no longer carry markup or SQL. A str stays Untrusted, because its content is still the client's.

Supported: str int float bool Decimal UUID date datetime time, Enum, Literal, Optional / unions, list set tuple dict, dataclasses and TypedDict (nested), and Annotated[..., Field(...)] for limits.

In this module

class RequestValidationError422: the request did not match what the handler declared.
class FieldLimits for a value. Use inside Annotated[int, Field(ge=1)], or via Query, Path, Header, Cookie, Body and Form, which accept the same.
def validate()Coerces value to tp; raises RequestValidationError listing all problems.
def hints_of()Type hints of a class or function, tolerating hints that cannot be resolved.
def coerce()Returns the converted value, or _MISSING after appending to errors.
def is_model()True for a dataclass or TypedDict type (a whole-object parameter).
def form_to_model_input()Shapes parsed form data for coerce as the dataclass / TypedDict tp.

class RequestValidationError (HTTPError)

422: the request did not match what the handler declared.

errors is a list of {"loc": [...], "msg": str, "type": str}. The client's input is deliberately not echoed back by the default 422 answer.

For an HTML form, fields maps each field name to its messages and values holds what the visitor typed (still Untrusted, so a template escapes it): what a route's on_invalid handler needs to draw the form again.

class Field

Limits for a value. Use inside Annotated[int, Field(ge=1)], or via Query, Path, Header, Cookie, Body and Form, which accept the same.

check

def check(value: Any, loc: list, errors: list) -> bool

def validate

def validate(value: Any, tp: Any, loc: list | tuple = ()) -> Any

Coerces value to tp; raises RequestValidationError listing all problems.

def hints_of

def hints_of(obj: Any) -> dict[str, Any]

Type hints of a class or function, tolerating hints that cannot be resolved.

def coerce

def coerce(value: Any, tp: Any, loc: list, errors: list) -> Any

Returns the converted value, or _MISSING after appending to errors.

def is_model

def is_model(tp: Any) -> bool

True for a dataclass or TypedDict type (a whole-object parameter).

def form_to_model_input

def form_to_model_input(raw: Any, tp: Any) -> dict

Shapes parsed form data for coerce as the dataclass / TypedDict tp.

A browser form differs from JSON in three ways, handled here so the same conversion, limits and no-mass-assignment rule apply afterwards:

  • an unchecked checkbox sends nothing: a missing bool field is False;
  • an unchecked group of checkboxes sends nothing: a missing list field is [];
  • an empty number/date input sends "": for a non-str field that means "not filled in" (the default, or None for an Optional).

Fields the model does not declare are left for coerce to drop.