Skip to content

Commit d90d96b

Browse files
committed
Fix list value checks bug
1 parent 8af04b7 commit d90d96b

File tree

1 file changed

+59
-40
lines changed

1 file changed

+59
-40
lines changed

flask_parameter_validation/parameter_types/parameter.py

+59-40
Original file line numberDiff line numberDiff line change
@@ -34,51 +34,70 @@ def __init__(
3434

3535
# Validator
3636
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:
37+
if type(value) is list:
38+
values = value
39+
# Min list len
40+
if self.min_list_length is not None:
41+
if len(value) < self.min_list_length:
5342
raise ValueError(
54-
f"must contain only characters: {self.whitelist}"
43+
f"must have at least {self.min_list_length} items."
5544
)
56-
# Blacklist
57-
if self.blacklist is not None:
58-
for bad in self.blacklist:
59-
if bad in str(value):
45+
# Max list len
46+
if self.max_list_length is not None:
47+
if len(value) > self.max_list_length:
6048
raise ValueError(
61-
f"must not contain: {bad}"
49+
f"must have have a maximum of {self.max_list_length} items."
6250
)
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-
)
51+
else:
52+
values = [value]
7553

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-
)
54+
# Iterate through values given (or just one, if not list)
55+
for value in values:
56+
# Min length
57+
if self.min_str_length is not None:
58+
if len(value) < self.min_str_length:
59+
raise ValueError(
60+
f"must have at least {self.min_str_length} characters."
61+
)
62+
# Max length
63+
if self.max_str_length is not None:
64+
if len(value) > self.max_str_length:
65+
raise ValueError(
66+
f"must have a maximum of {self.max_str_length} characters."
67+
)
68+
# Whitelist
69+
if self.whitelist is not None:
70+
for char in str(value):
71+
if char not in self.whitelist:
72+
raise ValueError(
73+
f"must contain only characters: {self.whitelist}"
74+
)
75+
# Blacklist
76+
if self.blacklist is not None:
77+
for bad in self.blacklist:
78+
if bad in str(value):
79+
raise ValueError(
80+
f"must not contain: {bad}"
81+
)
82+
# Min int
83+
if self.min_int is not None:
84+
if int(value) < self.min_int:
85+
raise ValueError(
86+
f"must be larger than {self.min_int}."
87+
)
88+
# Max int
89+
if self.max_int is not None:
90+
if int(value) > self.max_int:
91+
raise ValueError(
92+
f"must be smaller than {self.max_int}."
93+
)
94+
95+
# Regexp
96+
if self.pattern is not None:
97+
if not re.match(self.pattern, value):
98+
raise ValueError(
99+
f"pattern does not match: {self.pattern}."
100+
)
82101

83102
return True
84103

0 commit comments

Comments
 (0)