Skip to content

Commit 76e5a11

Browse files
committed
feat: allow passing a validator function to perform complex or customized validations on parameters
1 parent 11f49aa commit 76e5a11

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ All parameters can have default values, and automatic validation.
6565
* whitelist: str, A string containing allowed characters for the value
6666
* blacklist: str, A string containing forbidden characters for the value
6767
* pattern: str, A regex pattern to test for string matches
68+
* func: Callable, A function containing a fully customized logic to validate the value
6869

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

flask_parameter_validation/parameter_types/parameter.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""
22
Base Parameter class.
3-
Should only be used as child class for othe params.
3+
Should only be used as child class for other params.
44
"""
55
import re
66

@@ -20,6 +20,7 @@ def __init__(
2020
whitelist=None, # str: character whitelist
2121
blacklist=None, # str: character blacklist
2222
pattern=None, # str: regexp pattern
23+
func=None, # Callable: function performing a fully customized validation
2324
):
2425
self.default = default
2526
self.min_list_length = min_list_length
@@ -31,6 +32,7 @@ def __init__(
3132
self.whitelist = whitelist
3233
self.blacklist = blacklist
3334
self.pattern = pattern
35+
self.func = func
3436

3537
# Validator
3638
def validate(self, value):
@@ -99,6 +101,13 @@ def validate(self, value):
99101
f"pattern does not match: {self.pattern}."
100102
)
101103

104+
# Callable
105+
if self.func is not None:
106+
if not self.func(value):
107+
raise ValueError(
108+
f"value does not match the validator function."
109+
)
110+
102111
return True
103112

104113
def convert(self, value, allowed_types):

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
setup(
1313
name='Flask-Parameter-Validation',
14-
version='2.0.6',
14+
version='2.1.0',
1515
url='https://github.com/Ge0rg3/flask-parameter-validation',
1616
license='MIT',
1717
author='George Omnet',

0 commit comments

Comments
 (0)