Skip to content

Commit c267708

Browse files
committed
Automagically convert Query params to bool/int/floats if needed
1 parent 4b21b79 commit c267708

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

flask_parameter_validation/parameter_validation.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def nested_func(**kwargs):
6666
else:
6767
return error_response
6868

69-
# If typing's Any, ClassVar or Optional, don't validate type
69+
# If typing's Any or ClassVar, don't validate type
7070
if isinstance(param_annotation, typing._SpecialForm):
7171
valid = True
7272
allowed_types = ["all"]
@@ -80,6 +80,27 @@ def nested_func(**kwargs):
8080
else:
8181
allowed_types = (param_annotation,)
8282

83+
# If query parameter, try converting to match
84+
if param_type.__class__ == Query:
85+
# int conversion
86+
if param_annotation == int:
87+
try:
88+
user_input = int(user_input)
89+
except ValueError:
90+
pass
91+
# float conversion
92+
if param_annotation == float:
93+
try:
94+
user_input = float(user_input)
95+
except ValueError:
96+
pass
97+
# bool conversion
98+
elif param_annotation == bool:
99+
if user_input.lower() == "true":
100+
user_input = True
101+
elif user_input.lower() == "false":
102+
user_input = False
103+
83104
# Check if type matches annotation
84105
annotation_is_list = False
85106
if hasattr(param_annotation, "_name"):

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='1.0.17',
14+
version='1.0.18',
1515
url='https://github.com/Ge0rg3/Flask-Parameter-Validation',
1616
license='MIT',
1717
author='George Omnet',

0 commit comments

Comments
 (0)