length

validators.length.length(value: str, /, *, min_val: int | None = None, max_val: int | None = 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})
Parameters:
  • 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:

If len(value) is in between the given conditions. (ValidationError): If len(value) is not in between the given conditions.

Return type:

(Literal[True])

Raises:

(ValueError) – If either min_val or max_val is negative.