Skip to content

Commit

Permalink
Date format support
Browse files Browse the repository at this point in the history
  • Loading branch information
D.Ivanov committed Oct 24, 2016
1 parent 0853189 commit e4db597
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 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
22 changes: 20 additions & 2 deletions voluptuous/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
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,24 @@ 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'

def __call__(self, v):
try:
datetime.date(datetime.datetime.strptime(v, self.format))
except (TypeError, ValueError):
raise DatetimeInvalid(
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 e4db597

Please sign in to comment.