Skip to content

Commit 63a7971

Browse files
Handle errors on converting invalid integer/float values
When trying to convert integer/float values and the passed value is invalid (like a string containing non-digit characters), handle the resulting ValueError and use the original value which will cause a proper validation error later on. Fixes #83.
1 parent e3bb944 commit 63a7971

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

swagger_py_codegen/templates/flask/validators.tpl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ class FlaskValidatorAdaptor(object):
3131
def __init__(self, schema):
3232
self.validator = Draft4Validator(schema)
3333

34+
def validate_number(self, type_, value):
35+
try:
36+
return type_(value)
37+
except ValueError:
38+
return value
39+
3440
def type_convert(self, obj):
3541
if obj is None:
3642
return None
@@ -41,10 +47,10 @@ class FlaskValidatorAdaptor(object):
4147
result = dict()
4248

4349
convert_funs = {
44-
'integer': lambda v: int(v[0]),
50+
'integer': lambda v: self.validate_number(int, v[0]),
4551
'boolean': lambda v: v[0].lower() not in ['n', 'no', 'false', '', '0'],
4652
'null': lambda v: None,
47-
'number': lambda v: float(v[0]),
53+
'number': lambda v: self.validate_number(float, v[0]),
4854
'string': lambda v: v[0]
4955
}
5056

0 commit comments

Comments
 (0)