validators.length

Length.

@validator
def length( value: str, /, *, min_val: Optional[int] = None, max_val: Optional[int] = None):

Return whether or not the length of given string is within a specified range.

Examples:
>>> length('something', min_val=2)
True
>>> length('something', min_val=9, max_val=9)
True
>>> length('something', max_val=5)
ValidationError(func=length, args={'value': 'something', 'max_val': 5})
Arguments:
  • value: The string to validate.
  • min_val: The minimum required length of the string. If not provided, minimum length will not be checked.
  • max_val: The maximum length of the string. If not provided, maximum length will not be checked.
Returns:

(Literal[True]): If len(value) is in between the given conditions. (ValidationError): If len(value) is not in between the given conditions.

Raises:
  • (ValueError): If either min_val or max_val is negative.