Skip to content

Commit

Permalink
Improved validator + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
D.Ivanov committed Oct 24, 2016
1 parent 0fe64cd commit ff06372
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
1 change: 1 addition & 0 deletions voluptuous/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -756,4 +756,5 @@ def test_datetime():
def test_date():
schema = Schema({"date": Date()})
schema({"date": "2016-10-24"})
assert_raises(MultipleInvalid, schema, {"date": "2016-10-2"})
assert_raises(MultipleInvalid, schema, {"date": "2016-10-24Z"})
6 changes: 4 additions & 2 deletions voluptuous/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -595,9 +595,11 @@ class Date(Datetime):

def __call__(self, v):
try:
datetime.datetime.strptime(v, self.format).date()
datetime.datetime.strptime(v, self.format)
if len(v) != len(self.FORMAT_DESCRIPTION):
raise ValueError
raise DateInvalid(
self.msg or 'value has invalid length'
' expected length %d (%s)' % (len(self.FORMAT_DESCRIPTION), self.FORMAT_DESCRIPTION))
except (TypeError, ValueError):
raise DateInvalid(
self.msg or 'value does not match'
Expand Down

0 comments on commit ff06372

Please sign in to comment.