File tree Expand file tree Collapse file tree 1 file changed +12
-1
lines changed
flask_parameter_validation/parameter_types Expand file tree Collapse file tree 1 file changed +12
-1
lines changed Original file line number Diff line number Diff line change 2
2
Base Parameter class.
3
3
Should only be used as child class for othe params.
4
4
"""
5
+ import re
5
6
6
7
7
8
class ValidationError (Exception ):
@@ -21,7 +22,8 @@ def __init__(
21
22
min_int = None , # int: min number (if val is int)
22
23
max_int = None , # int: max number (if val is int)
23
24
whitelist = None , # str: character whitelist
24
- blacklist = None # str: character blacklist
25
+ blacklist = None , # str: character blacklist
26
+ pattern = None , # str: regexp pattern
25
27
):
26
28
self .default = default
27
29
self .min_length = min_length
@@ -30,6 +32,7 @@ def __init__(
30
32
self .max_int = max_int
31
33
self .whitelist = whitelist
32
34
self .blacklist = blacklist
35
+ self .pattern = pattern
33
36
34
37
# Validator
35
38
def validate (self , values ):
@@ -77,4 +80,12 @@ def validate(self, values):
77
80
raise ValidationError (
78
81
f"must be smaller than { self .max_int } ."
79
82
)
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
+
80
91
return True
You can’t perform that action at this time.
0 commit comments