[docs]@abc.abstractmethoddef__call__(self,value:Any):""" All specific validators should implement this method. :param value: The given value to be validated. :raises ValidationError: if validation failed. """
[docs]classRegexValidator(Validator):""" A validator to validate the given value whether match regex or not. """def__init__(self,pattern:str,flags:Union[int,re.RegexFlag]):self.regex=re.compile(pattern,flags)def__call__(self,value:Any):ifnotself.regex.match(value):raiseValidationError(f"Value '{value}' does not match regex '{self.regex.pattern}'")
[docs]classMaxLengthValidator(Validator):""" A validator to validate the length of given value whether greater than max_length or not. """def__init__(self,max_length:int):self.max_length=max_lengthdef__call__(self,value:str):ifvalueisNone:raiseValidationError("Value must not be None")iflen(value)>self.max_length:raiseValidationError(f"Length of '{value}' {len(value)} > {self.max_length}")
[docs]classMinLengthValidator(Validator):""" A validator to validate the length of given value whether less than min_length or not. """def__init__(self,min_length:int):self.min_length=min_lengthdef__call__(self,value:str):ifvalueisNone:raiseValidationError("Value must not be None")iflen(value)<self.min_length:raiseValidationError(f"Length of '{value}' {len(value)} < {self.min_length}")
[docs]classMinValueValidator(Validator):""" Min value validator for FloatField, IntField, SmallIntField, BigIntField """def__init__(self,min_value:Union[int,float,Decimal]):ifnotisinstance(min_value,(int,float,Decimal)):raiseValidationError("Value must be a numeric value and is required")self.min_value=min_valuedef__call__(self,value:Union[int,float,Decimal]):ifnotisinstance(value,(int,float,Decimal)):raiseValidationError("Value must be a numeric value and is required")ifvalue<self.min_value:raiseValidationError(f"Value should be greater or equal to {self.min_value}")
[docs]classMaxValueValidator(Validator):""" Max value validator for FloatField, IntField, SmallIntField, BigIntField """def__init__(self,max_value:Union[int,float,Decimal]):ifnotisinstance(max_value,(int,float,Decimal)):raiseValidationError("Value must be a numeric value and is required")self.max_value=max_valuedef__call__(self,value:Union[int,float,Decimal]):ifnotisinstance(value,(int,float,Decimal)):raiseValidationError("Value must be a numeric value and is required")ifvalue>self.max_value:raiseValidationError(f"Value should be less or equal to {self.max_value}")
[docs]classCommaSeparatedIntegerListValidator(Validator):""" A validator to validate whether the given value is valid comma separated integer list or not. """def__init__(self,allow_negative:bool=False):pattern=r"^%(neg)s\d+(?:%(sep)s%(neg)s\d+)*\Z"%{"neg":"(-)?"ifallow_negativeelse"","sep":re.escape(","),}self.regex=RegexValidator(pattern,re.I)def__call__(self,value:str):self.regex(value)
[docs]defvalidate_ipv4_address(value:Any):""" A validator to validate whether the given value is valid IPv4Address or not. :raises ValidationError: if value is invalid IPv4Address. """try:ipaddress.IPv4Address(value)exceptValueError:raiseValidationError(f"'{value}' is not a valid IPv4 address.")
[docs]defvalidate_ipv6_address(value:Any):""" A validator to validate whether the given value is valid IPv6Address or not. :raises ValidationError: if value is invalid IPv6Address. """try:ipaddress.IPv6Address(value)exceptValueError:raiseValidationError(f"'{value}' is not a valid IPv6 address.")
[docs]defvalidate_ipv46_address(value:Any):""" A validator to validate whether the given value is valid IPv4Address or IPv6Address or not. :raises ValidationError: if value is invalid IPv4Address or IPv6Address. """try:validate_ipv4_address(value)exceptValidationError:try:validate_ipv6_address(value)exceptValidationError:raiseValidationError(f"'{value}' is not a valid IPv4 or IPv6 address.")