Skip to content

Validators

didactic.api.validates

validates(
    *field_names: str, mode: str = "after"
) -> Callable[
    [Callable[..., FieldValue]], Callable[..., FieldValue]
]

Mark a class method as a Python-side validator for one or more fields.

Parameters:

Name Type Description Default
*field_names str

Names of the fields this validator applies to. At least one is required.

()
mode str

Either "before" (run before type validation, may convert) or "after" (run after, receives the typed value).

'after'

Returns:

Type Description
Callable

A decorator that tags the wrapped method for the metaclass to register on the field spec.

Notes

Validators are deliberately not lifted into the Theory; for cross-language constraints, use __axioms__ or Annotated[T, ...] metadata instead.

Examples:

>>> import didactic.api as dx
>>> class User(dx.Model):
...     email: str
...
...     @dx.validates("email")
...     @classmethod
...     def _email_lower(cls, v: str) -> str:
...         return v.lower()

didactic.api.ValidationError dataclass

ValidationError(
    entries: tuple[ValidationErrorEntry, ...],
    model: type | None = None,
)

Bases: Exception

One or more validation failures, surfaced as a single exception.

didactic collects all failures during construction or mutation and raises them together (no fail-fast). This mirrors Pydantic's behaviour so users who grep "for the list of errors" find one.

Parameters:

Name Type Description Default
entries tuple[ValidationErrorEntry, ...]

The individual failures.

required
model type | None

The model class that failed validation. None when the failure precedes class identification (e.g. discriminated-union dispatch).

None
See Also

ValidationErrorEntry : the per-issue record.

didactic.api.ValidationErrorEntry dataclass

ValidationErrorEntry(
    loc: tuple[str | int, ...],
    type: str,
    msg: str,
    axiom: str | None = None,
    vertex_id: str | None = None,
)

A single validation failure.

Parameters:

Name Type Description Default
loc tuple[str | int, ...]

Path through the schema where the failure was detected, e.g. ("orders", 2, "shipping_address", "postal_code"). Empty tuple means "the model itself".

required
type str

Error category. One of "type_error", "axiom_violation", "missing_required", "extra_field", "converter_error", "validator_error", or a panproto-side identifier.

required
msg str

Human-readable description.

required
axiom str | None

The panproto-Expr term that failed, when applicable. None for non-axiom failures.

None
vertex_id str | None

The schema vertex id where the violation lives, when known.

None