Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/validators/length.py: 100%

5 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-07 06:08 +0000

1"""Length.""" 

2# -*- coding: utf-8 -*- 

3 

4# local 

5from .utils import validator 

6from .between import between 

7 

8 

9@validator 

10def length(value: str, /, *, min_val: int = 0, max_val: int = 0): 

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

12 

13 Examples: 

14 >>> length('something', min_val=2) 

15 # Output: True 

16 >>> length('something', min_val=9, max_val=9) 

17 # Output: True 

18 >>> length('something', max_val=5) 

19 # Output: ValidationFailure(func=length, ...) 

20 

21 Args: 

22 value: 

23 The string to validate. 

24 min_val: 

25 The minimum required length of the string. If not provided, 

26 minimum length will not be checked. 

27 max_val: 

28 The maximum length of the string. If not provided, 

29 maximum length will not be checked. 

30 

31 Returns: 

32 (Literal[True]): 

33 If `len(value)` is in between the given conditions. 

34 (ValidationFailure): 

35 If `len(value)` is not in between the given conditions. 

36 

37 > *New in version 0.2.0*. 

38 """ 

39 return between(len(value), min_val=min_val, max_val=max_val) if value else False