Validator that fails when a value is None or an empty string.
RequiredValidator
Bases: ValidationRule
Fails when the value is None or an empty string.
Source code in src/forging_blocks/domain/validators/required_validator.py
| class RequiredValidator(ValidationRule):
"""Fails when the value is ``None`` or an empty string."""
def __init__(self, field: str) -> None:
self._field = field
def validate(self, value: Any) -> list[RuleViolationError]:
if value is None or value == "":
return [
RuleViolationError(
ErrorMessage(f"'{self._field}' is required."),
ErrorMetadata(context={"field": self._field, "code": "required"}),
)
]
return []
|