Skip to content

Commit fdd094d

Browse files
authored
Merge pull request #1 from iml1111/master
Allow regex pattern for validation.
2 parents 5bfd4b4 + 8a1e719 commit fdd094d

File tree

4 files changed

+18
-5
lines changed

4 files changed

+18
-5
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ build/
33
dist/
44
__pycache__/
55
*.egg-info
6-
*.pyc
6+
*.pyc
7+
test.py

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ All parameters can have default values, and automatic validation.
6363
* min_int: int, Specifies the minimum number for an int input
6464
* max_int: int, Specifies the maximum number for an int input
6565
* whitelist: str, A string containing allowed characters for the value
66-
* blacklist: str, A string containing forbidden characters for the value
66+
* blacklist: str, A string containing forbidden characters for the value
67+
* pattern: str, A regex pattern to test for string matches
6768

6869
`File` has the following options:
6970
* content_types: array of strings, an array of allowed content types.

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

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111

1212
setup(
1313
name='Flask-Parameter-Validation',
14-
version='1.0.23',
14+
version='1.0.24',
1515
url='https://github.com/Ge0rg3/Flask-Parameter-Validation',
1616
license='MIT',
1717
author='George Omnet',
18-
author_email='[email protected]',
18+
author_email='[email protected]',
1919
description='Get and validate all Flask input parameters with ease.',
2020
long_description=long_description,
2121
long_description_content_type='text/markdown',

0 commit comments

Comments
 (0)