5
5
import re
6
6
7
7
8
- class ValidationError (Exception ):
9
- pass
10
-
11
-
12
8
class Parameter :
13
9
14
10
# Parameter initialisation
15
11
def __init__ (
16
12
self ,
17
13
default = None , # any: default parameter value
18
- min_length = None , # int: min parameter length
19
- max_length = None , # int: max parameter length
14
+ min_str_length = None , # int: min parameter length
15
+ max_str_length = None , # int: max parameter length
20
16
min_list_length = None , # int: min number of items in list
21
17
max_list_length = None , # int: max number of items in list
22
18
min_int = None , # int: min number (if val is int)
@@ -26,66 +22,67 @@ def __init__(
26
22
pattern = None , # str: regexp pattern
27
23
):
28
24
self .default = default
29
- self .min_length = min_length
30
- self .max_length = max_length
25
+ self .min_list_length = min_list_length
26
+ self .max_list_length = max_list_length
27
+ self .min_str_length = min_str_length
28
+ self .max_str_length = max_str_length
31
29
self .min_int = min_int
32
30
self .max_int = max_int
33
31
self .whitelist = whitelist
34
32
self .blacklist = blacklist
35
33
self .pattern = pattern
36
34
37
35
# Validator
38
- def validate (self , values ):
39
- # Turn value into list if not already
40
- if type (values ) is not list :
41
- values = [values ]
42
-
43
- # Check all values are valid
44
- for value in values :
45
- # Min length
46
- if self .min_length is not None :
47
- if len (value ) < self .min_length :
48
- raise ValidationError (
49
- f"must be at least { self .min_length } characters."
50
- )
51
- # Max length
52
- if self .max_length is not None :
53
- if len (value ) > self .max_length :
54
- raise ValidationError (
55
- f"must be a maximum of { self .max_length } characters."
36
+ def validate (self , value ):
37
+ # Min length
38
+ if self .min_str_length is not None :
39
+ if len (value ) < self .min_str_length :
40
+ raise ValueError (
41
+ f"must have at least { self .min_str_length } characters."
42
+ )
43
+ # Max length
44
+ if self .max_str_length is not None :
45
+ if len (value ) > self .max_str_length :
46
+ raise ValueError (
47
+ f"must have a maximum of { self .max_str_length } characters."
48
+ )
49
+ # Whitelist
50
+ if self .whitelist is not None :
51
+ for char in str (value ):
52
+ if char not in self .whitelist :
53
+ raise ValueError (
54
+ f"must contain only characters: { self .whitelist } "
56
55
)
57
- # Whitelist
58
- if self .whitelist is not None :
59
- for char in str (value ):
60
- if char not in self .whitelist :
61
- raise ValidationError (
62
- f"must contain only characters: { self .whitelist } "
63
- )
64
- # Blacklist
65
- if self .blacklist is not None :
66
- for bad in self .blacklist :
67
- if bad in str (value ):
68
- raise ValidationError (
69
- f"must not contain: { bad } "
70
- )
71
- # Min int
72
- if self .min_int is not None :
73
- if int (value ) < self .min_int :
74
- raise ValidationError (
75
- f"must be larger than { self .min_int } ."
76
- )
77
- # Max int
78
- if self .max_int is not None :
79
- if int (value ) > self .max_int :
80
- raise ValidationError (
81
- f"must be smaller than { self .max_int } ."
56
+ # Blacklist
57
+ if self .blacklist is not None :
58
+ for bad in self .blacklist :
59
+ if bad in str (value ):
60
+ raise ValueError (
61
+ f"must not contain: { bad } "
82
62
)
63
+ # Min int
64
+ if self .min_int is not None :
65
+ if int (value ) < self .min_int :
66
+ raise ValueError (
67
+ f"must be larger than { self .min_int } ."
68
+ )
69
+ # Max int
70
+ if self .max_int is not None :
71
+ if int (value ) > self .max_int :
72
+ raise ValueError (
73
+ f"must be smaller than { self .max_int } ."
74
+ )
83
75
84
- # Regexp
85
- if self .pattern is not None :
86
- if not re .match (self .pattern , value ):
87
- raise ValidationError (
88
- f"pattern does not match: { self .pattern } ."
89
- )
76
+ # Regexp
77
+ if self .pattern is not None :
78
+ if not re .match (self .pattern , value ):
79
+ raise ValueError (
80
+ f"pattern does not match: { self .pattern } ."
81
+ )
82
+
83
+ return True
90
84
91
- return True
85
+ def convert (self , value , allowed_types ):
86
+ """Some parameter types require manual type conversion (see Query)"""
87
+ return value
88
+
0 commit comments