assets_guardian.core.domain.models.validatorο
Functions
|
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:
If optional is True and the value is None, the field is valid.
If the value is not of the expected type, a TypeError is raised.
- 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.