Skip to content

Commit

Permalink
Merge pull request #234 from divanovGH/master
Browse files Browse the repository at this point in the history
Date format support
  • Loading branch information
tusharmakkar08 authored Oct 25, 2016
2 parents 0853189 + 8833a97 commit c6080c7
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 5 deletions.
4 changes: 4 additions & 0 deletions voluptuous/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@ class DatetimeInvalid(Invalid):
"""The value is not a formatted datetime string."""


class DateInvalid(Invalid):
"""The value is not a formatted date string."""


class InInvalid(Invalid):
pass

Expand Down
15 changes: 14 additions & 1 deletion voluptuous/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
Schema, Required, Optional, Extra, Invalid, In, Remove, Literal,
Url, MultipleInvalid, LiteralInvalid, NotIn, Match, Email,
Replace, Range, Coerce, All, Any, Length, FqdnUrl, ALLOW_EXTRA, PREVENT_EXTRA,
validate, ExactSequence, Equal, Unordered, Number
validate, ExactSequence, Equal, Unordered, Number, Date, Datetime
)
from voluptuous.humanize import humanize_error
from voluptuous.util import to_utf8_py2, u
Expand Down Expand Up @@ -745,3 +745,16 @@ def test_named_tuples_validate_as_tuples():
Schema((int, int))(t)
Schema(NT(int, int))(nt)
Schema(NT(int, int))(t)


def test_datetime():
schema = Schema({"datetime": Datetime()})
schema({"datetime": "2016-10-24T14:01:57.102152Z"})
assert_raises(MultipleInvalid, schema, {"datetime": "2016-10-24T14:01:57"})


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"})
31 changes: 27 additions & 4 deletions voluptuous/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
from schema_builder import Schema, raises, message
from error import (MultipleInvalid, CoerceInvalid, TrueInvalid, FalseInvalid, BooleanInvalid, Invalid, AnyInvalid,
AllInvalid, MatchInvalid, UrlInvalid, EmailInvalid, FileInvalid, DirInvalid, RangeInvalid,
PathInvalid, ExactSequenceInvalid, LengthInvalid, DatetimeInvalid, InInvalid, TypeInvalid,
NotInInvalid)
PathInvalid, ExactSequenceInvalid, LengthInvalid, DatetimeInvalid, DateInvalid, InInvalid,
TypeInvalid, NotInInvalid)
except ImportError:
from .schema_builder import Schema, raises, message
from .error import (MultipleInvalid, CoerceInvalid, TrueInvalid, FalseInvalid, BooleanInvalid, Invalid, AnyInvalid,
AllInvalid, MatchInvalid, UrlInvalid, EmailInvalid, FileInvalid, DirInvalid, RangeInvalid,
PathInvalid, ExactSequenceInvalid, LengthInvalid, DatetimeInvalid, InInvalid, TypeInvalid,
NotInInvalid)
PathInvalid, ExactSequenceInvalid, LengthInvalid, DatetimeInvalid, DateInvalid, InInvalid,
TypeInvalid, NotInInvalid)


if sys.version_info >= (3,):
Expand Down Expand Up @@ -587,6 +587,29 @@ def __repr__(self):
return 'Datetime(format=%s)' % self.format


class Date(Datetime):
"""Validate that the value matches the date format."""

DEFAULT_FORMAT = '%Y-%m-%d'
FORMAT_DESCRIPTION = 'yyyy-mm-dd'

def __call__(self, v):
try:
datetime.datetime.strptime(v, self.format)
if len(v) != len(self.FORMAT_DESCRIPTION):
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'
' expected format %s' % self.format)
return v

def __repr__(self):
return 'Date(format=%s)' % self.format


class In(object):
"""Validate that a value is in a collection."""

Expand Down

0 comments on commit c6080c7

Please sign in to comment.