[docs]classValidator(ABC,Generic[T]):""" An abstract base class for all validators. Defines the `validate_value` method that must be implemented by subclasses. """
[docs]@abstractmethoddefvalidate_value(self,value:Any)->T:""" Validates the given value and returns the validated value. Args: value (Any): The value to be validated. Returns: T: The validated value. """pass
[docs]classValidationModel(Validator[T],ABC):""" An abstract base class for validation models. Provides a base implementation for initializing the validation model with keyword arguments and validating the arguments. """def__init__(self,**kwargs)->None:""" Initializes the validation model with the given keyword arguments. Args: **kwargs: The keyword arguments to be used for initializing the validation model. """forkey,valueinkwargs.items():setattr(self,key,value)self.validate_arguments()
[docs]defvalidate_arguments(self)->None:""" Validates the arguments used to initialize the validation model. This method must be implemented by subclasses to perform any necessary validation. """pass
[docs]classStateValidator(Validator[T],ABC):""" An abstract base class for state validators. Provides a base implementation for managing the state of a validated value, including validation of modifications. """def__init__(self,value:Any):""" Initializes the state validator with the given value. Args: value (Any): The initial value to be validated and stored. """self._value=self.validate_value(value)
[docs]@abstractmethoddefvalidate_modifications(self,new_value:Any)->None:""" Validates the modification of the value. Args: new_value (Any): The new value to be validated. Raises: ValidationError: If the new value is invalid. """pass
@propertydefvalue(self)->T:""" Returns the validated value. Returns: T: The validated value. """returnself._value@value.setterdefvalue(self,new_value:Any)->None:""" Sets the new value, validating the modification. Args: new_value (Any): The new value to be set. """self._value=self.validate_modifications(new_value)