Skip to content

Commit 3f59d8c

Browse files
committed
fix: add ability to strictly enforce date format in conformatteddate
1 parent 616b558 commit 3f59d8c

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

src/dve/metadata_parser/domain_types.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,8 @@ class ConFormattedDate(dt.date):
260260

261261
DATE_FORMAT: ClassVar[Optional[str]] = None
262262
"""The specific format of the date as a Python 'strptime' string."""
263+
strict: ClassVar[Optional[bool]] = False
264+
"""Add additional check to ensure that date supplied meets the date format exactly."""
263265
ge: ClassVar[Optional[dt.date]] = None
264266
"""The earliest date allowed."""
265267
le: ClassVar[Optional[dt.date]] = None
@@ -280,12 +282,17 @@ def validate(cls, value: Optional[Union[dt.date, str]]) -> Optional[dt.date]:
280282
elif cls.DATE_FORMAT is not None:
281283
try:
282284
date = dt.datetime.strptime(value, cls.DATE_FORMAT).date()
285+
if cls.strict and not (date.strftime(cls.DATE_FORMAT) == value):
286+
raise ValueError
283287
except ValueError as err:
284288
raise ValueError(
285289
f"Unable to parse provided datetime in format {cls.DATE_FORMAT}"
286290
) from err # pylint: disable=line-too-long
287291
else:
288292
raise ValueError("No date format provided")
293+
294+
295+
289296

290297
return date
291298

@@ -317,6 +324,7 @@ def __get_validators__(cls) -> Iterator[classmethod]:
317324
@validate_arguments
318325
def conformatteddate(
319326
date_format: Optional[str] = None,
327+
strict: Optional[bool] = False,
320328
ge: Optional[dt.date] = None, # pylint: disable=invalid-name
321329
le: Optional[dt.date] = None, # pylint: disable=invalid-name
322330
gt: Optional[dt.date] = None, # pylint: disable=invalid-name
@@ -331,6 +339,7 @@ def conformatteddate(
331339

332340
dict_ = ConFormattedDate.__dict__.copy()
333341
dict_["DATE_FORMAT"] = date_format
342+
dict_["strict"] = strict
334343
dict_["ge"] = ge
335344
dict_["le"] = le
336345
dict_["gt"] = gt

0 commit comments

Comments
 (0)