@@ -16,6 +16,8 @@ def __init__(
16
16
default = None , # any: default parameter value
17
17
min_length = None , # int: min parameter length
18
18
max_length = None , # int: max parameter length
19
+ min_list_length = None , # int: min number of items in list
20
+ max_list_length = None , # int: max number of items in list
19
21
min_int = None , # int: min number (if val is int)
20
22
max_int = None , # int: max number (if val is int)
21
23
whitelist = None , # str: character whitelist
@@ -30,43 +32,49 @@ def __init__(
30
32
self .blacklist = blacklist
31
33
32
34
# Validator
33
- def validate (self , value ):
34
- # Min length
35
- if self .min_length is not None :
36
- if len (value ) < self .min_length :
37
- raise ValidationError (
38
- f"must be at least { self .min_length } characters."
39
- )
40
- # Max length
41
- if self .max_length is not None :
42
- if len (value ) > self .max_length :
43
- raise ValidationError (
44
- f"must be a maximum of { self .max_length } characters."
45
- )
46
- # Whitelist
47
- if self .whitelist is not None :
48
- for char in str (value ):
49
- if char not in self .whitelist :
35
+ def validate (self , values ):
36
+ # Turn value into list if not already
37
+ if type (values ) is not list :
38
+ values = [values ]
39
+
40
+ # Check all values are valid
41
+ for value in values :
42
+ # Min length
43
+ if self .min_length is not None :
44
+ if len (value ) < self .min_length :
45
+ raise ValidationError (
46
+ f"must be at least { self .min_length } characters."
47
+ )
48
+ # Max length
49
+ if self .max_length is not None :
50
+ if len (value ) > self .max_length :
51
+ raise ValidationError (
52
+ f"must be a maximum of { self .max_length } characters."
53
+ )
54
+ # Whitelist
55
+ if self .whitelist is not None :
56
+ for char in str (value ):
57
+ if char not in self .whitelist :
58
+ raise ValidationError (
59
+ f"must contain only characters: { self .whitelist } "
60
+ )
61
+ # Blacklist
62
+ if self .blacklist is not None :
63
+ for bad in self .blacklist :
64
+ if bad in str (value ):
65
+ raise ValidationError (
66
+ f"must not contain: { bad } "
67
+ )
68
+ # Min int
69
+ if self .min_int is not None :
70
+ if int (value ) < self .min_int :
50
71
raise ValidationError (
51
- f"must contain only characters: { self .whitelist } "
72
+ f"must be larger than { self .min_int } . "
52
73
)
53
- # Blacklist
54
- if self .blacklist is not None :
55
- for bad in self .blacklist :
56
- if bad in str (value ):
74
+ # Max int
75
+ if self .max_int is not None :
76
+ if int (value ) > self .max_int :
57
77
raise ValidationError (
58
- f"must not contain: { bad } "
78
+ f"must be smaller than { self . max_int } . "
59
79
)
60
- # Min int
61
- if self .min_int is not None :
62
- if int (value ) < self .min_int :
63
- raise ValidationError (
64
- f"must be larger than { self .min_int } ."
65
- )
66
- # Max int
67
- if self .max_int is not None :
68
- if int (value ) > self .max_int :
69
- raise ValidationError (
70
- f"must be smaller than { self .max_int } ."
71
- )
72
- return True
80
+ return True
0 commit comments