Skip to content

Commit

Permalink
add validation for "date" format
Browse files Browse the repository at this point in the history
  • Loading branch information
hjacobs committed Jul 31, 2015
1 parent 19acef2 commit 5337df4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
5 changes: 4 additions & 1 deletion connexion/decorators/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import strict_rfc3339

from connexion.problem import problem
from connexion.utils import validate_date

logger = logging.getLogger('connexion.decorators.validation')

Expand All @@ -32,7 +33,9 @@
'array': list,
'object': dict} # map of swagger types to python types

FORMAT_MAP = {('string', 'date-time'): strict_rfc3339.validate_rfc3339}

FORMAT_MAP = {('string', 'date-time'): strict_rfc3339.validate_rfc3339,
('string', 'date'): validate_date}


def validate_format(schema, data):
Expand Down
12 changes: 12 additions & 0 deletions connexion/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import importlib
import re
import strict_rfc3339

PATH_PARAMETER = re.compile(r'\{([^}]*)\}')

Expand Down Expand Up @@ -83,3 +84,14 @@ def produces_json(produces):
# todo handle parameters
maintype, subtype = mimetype.split('/') # type: str, str
return maintype == 'application' and subtype.endswith('+json')


def validate_date(s):
'''
>>> validate_date('foo')
False
>>> validate_date('2015-07-31')
True
'''
return strict_rfc3339.validate_rfc3339(s + 'T00:00:00Z')
7 changes: 7 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,10 @@ def test_get_function_from_name():
function = utils.get_function_from_name('math.ceil')
assert function == math.ceil
assert function(2.7) == 3


def test_validate_date():
assert not utils.validate_date('foo')
assert utils.validate_date('2015-07-31')
assert not utils.validate_date('2015-07-31T19:51:00Z')
assert utils.validate_date('9999-12-31')

0 comments on commit 5337df4

Please sign in to comment.