Skip to content

Commit ac26023

Browse files
committed
regexp valdation add
1 parent 5bfd4b4 commit ac26023

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

flask_parameter_validation/parameter_types/parameter.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Base Parameter class.
33
Should only be used as child class for othe params.
44
"""
5+
import re
56

67

78
class ValidationError(Exception):
@@ -21,7 +22,8 @@ def __init__(
2122
min_int=None, # int: min number (if val is int)
2223
max_int=None, # int: max number (if val is int)
2324
whitelist=None, # str: character whitelist
24-
blacklist=None # str: character blacklist
25+
blacklist=None, # str: character blacklist
26+
pattern=None, # str: regexp pattern
2527
):
2628
self.default = default
2729
self.min_length = min_length
@@ -30,6 +32,7 @@ def __init__(
3032
self.max_int = max_int
3133
self.whitelist = whitelist
3234
self.blacklist = blacklist
35+
self.pattern = pattern
3336

3437
# Validator
3538
def validate(self, values):
@@ -77,4 +80,12 @@ def validate(self, values):
7780
raise ValidationError(
7881
f"must be smaller than {self.max_int}."
7982
)
83+
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+
)
90+
8091
return True

0 commit comments

Comments
 (0)