Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/dve/metadata_parser/domain_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,8 @@ class ConFormattedDate(dt.date):

DATE_FORMAT: ClassVar[Optional[str]] = None
"""The specific format of the date as a Python 'strptime' string."""
strict: ClassVar[Optional[bool]] = False
"""Add additional check to ensure that date supplied meets the date format exactly."""
ge: ClassVar[Optional[dt.date]] = None
"""The earliest date allowed."""
le: ClassVar[Optional[dt.date]] = None
Expand All @@ -280,6 +282,8 @@ def validate(cls, value: Optional[Union[dt.date, str]]) -> Optional[dt.date]:
elif cls.DATE_FORMAT is not None:
try:
date = dt.datetime.strptime(value, cls.DATE_FORMAT).date()
if cls.strict and (date.strftime(cls.DATE_FORMAT) != value):
raise ValueError
except ValueError as err:
raise ValueError(
f"Unable to parse provided datetime in format {cls.DATE_FORMAT}"
Expand Down Expand Up @@ -317,6 +321,7 @@ def __get_validators__(cls) -> Iterator[classmethod]:
@validate_arguments
def conformatteddate(
date_format: Optional[str] = None,
strict: Optional[bool] = False,
ge: Optional[dt.date] = None, # pylint: disable=invalid-name
le: Optional[dt.date] = None, # pylint: disable=invalid-name
gt: Optional[dt.date] = None, # pylint: disable=invalid-name
Expand All @@ -331,6 +336,7 @@ def conformatteddate(

dict_ = ConFormattedDate.__dict__.copy()
dict_["DATE_FORMAT"] = date_format
dict_["strict"] = strict
dict_["ge"] = ge
dict_["le"] = le
dict_["gt"] = gt
Expand Down
Loading