Skip to content
Edit this page

Validation

Validation delegates constraints, coercion, defaults, aliases, nested models, and custom validators to Pydantic. Declare an application model using BaseModel, Field, and Pydantic validators. Titancore adds the outcome API and HTTP error response without a separate rule language.

Application service

Register the factory explicitly to use the Validator facade:

from pydantic import BaseModel, Field

from titancore.facades import Validator
from titancore.validation import ValidationServiceProvider

app.register(ValidationServiceProvider())


class CreateProfile(BaseModel):
    """Declare the fields accepted by this application operation.

    Pydantic owns the constraints and model configuration.
    """

    name: str = Field(min_length=2)
    age: int = Field(ge=18)


# Inside the application's selected scope:
validator = Validator.make({"name": "Ada", "age": "21"}, CreateProfile)
if validator.passes():
    values = validator.validated()
else:
    errors = validator.errors()

The factory is available as app["validator"], as titancore.validation.Factory, or through the factory contract in titancore.contracts.validation. Standalone code can instantiate Factory() without an application.

make evaluates the schema once. passes() and fails() inspect that outcome. errors() returns a dictionary of field names and message lists. Nested locations use dotted paths such as members.0.name. Model-level failures use __root__.

validated() and validate() return validated data or raise titancore.validation.ValidationError. Factory.validate and the facade's validate method combine construction and execution:

values = Validator.validate(data, CreateProfile, strict=True)

Pydantic controls strictness and serialization. Returned values use Python serialization and include schema defaults. Unknown fields use the model's configured policy; Pydantic ignores them by default. Use ConfigDict(extra="forbid") to reject them. Schemas must serialize to a field mapping. Validation does not add asynchronous rules or database queries.

HTTP requests

Injected framework requests expose await request.validate(Schema) and the optional strict argument. The request adapter validates its combined query, JSON, or form input through the same stateless factory. This method works without registering the facade service provider.

from titancore.foundation.http import inject
from titancore.http import Request


@inject
async def create(request: Request) -> dict[str, object]:
    """Validate the fields received by this API operation.

    Returns:
        Validated profile fields.
    """
    return await request.validate(CreateProfile)

The HTTP kernel renders framework validation failures with status 422:

{
  "message": "The given data was invalid.",
  "errors": {"age": ["Input should be greater than or equal to 18"]}
}

Responses include Pydantic's field messages and omit rejected values, native error context, and documentation URLs. Applications should write custom validator messages suitable for API clients. Malformed request bodies still use the existing 400 input response. FastAPI retains its native handling of endpoint model declarations.

See the Pydantic model documentation and validation error documentation for model configuration and supported constraints.