assets_guardian.core.domain.models.validator

Functions

validate_field(instance,Β field,Β expected_type,Β *)

Validates the type and non-emptiness of a dataclass field.

assets_guardian.core.domain.models.validator.validate_field(instance: object, field: str, expected_type: type | tuple[type, ...], *, optional: bool = False, empty: bool = False) None[source]

Validates the type and non-emptiness of a dataclass field.

The validation is performed in three ordered steps:
  1. If optional is True and the value is None, the field is valid.

  2. If the value is not of the expected type, a TypeError is raised.

  3. If empty is False and the value is falsy (empty string, empty list, etc.),

    a ValueError is raised. Numeric types (int, float) are excluded from this check to allow 0 and 0.0.

Parameters:
  • instance – Instance of the dataclass to validate.

  • field – Name of the field to check.

  • expected_type – Expected type or tuple of types for the field.

  • optional – If True, accepts None as a valid value. Defaults to False.

  • empty – If True, allows falsy values (empty string, empty list, etc.). Defaults to False (strict).

Raises:
  • TypeError – If the field is not of the expected type (or None when optional is False).

  • ValueError – If the field has a falsy non-numeric value and empty is False.