Skip to content

Commit

Permalink
Merge pull request #84 from eht16/issue83_handle_conversion_errors
Browse files Browse the repository at this point in the history
Handle errors on converting invalid integer/float values
  • Loading branch information
jizhouli authored Jan 6, 2017
2 parents e3bb944 + 63a7971 commit d40c5c9
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions swagger_py_codegen/templates/flask/validators.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ class FlaskValidatorAdaptor(object):
def __init__(self, schema):
self.validator = Draft4Validator(schema)

def validate_number(self, type_, value):
try:
return type_(value)
except ValueError:
return value

def type_convert(self, obj):
if obj is None:
return None
Expand All @@ -41,10 +47,10 @@ class FlaskValidatorAdaptor(object):
result = dict()

convert_funs = {
'integer': lambda v: int(v[0]),
'integer': lambda v: self.validate_number(int, v[0]),
'boolean': lambda v: v[0].lower() not in ['n', 'no', 'false', '', '0'],
'null': lambda v: None,
'number': lambda v: float(v[0]),
'number': lambda v: self.validate_number(float, v[0]),
'string': lambda v: v[0]
}

Expand Down

0 comments on commit d40c5c9

Please sign in to comment.