Skip to content

Commit 11930b8

Browse files
fix: make time format more strict to stop invalid time date flowing (#92)
* fix: make time format more strict to stop invalid time date flowing * style: address sonarcube l523 comment
1 parent 3587a02 commit 11930b8

2 files changed

Lines changed: 5 additions & 4 deletions

File tree

src/dve/metadata_parser/domain_types.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,8 @@ def validate(cls, value: Union[dt.time, dt.datetime, str]) -> dt.time | None:
519519
raise ValueError("Provided time has timezone, but this is forbidden for this field")
520520
if cls.TIMEZONE_TREATMENT == "require" and not new_time.tzinfo:
521521
raise ValueError("Provided time missing timezone, but this is required for this field")
522-
522+
if isinstance(value, str) and cls.TIME_FORMAT and value != str(new_time):
523+
raise ValueError("Provided time is not matching expected time format supplied.")
523524
return new_time
524525

525526
@classmethod

tests/test_model_generation/test_domain_types.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -335,9 +335,6 @@ def test_reportingperiod_raises(field, value):
335335
["23:00:00Z", None, "require", dt.time(23, 0, 0, tzinfo=UTC)],
336336
["12:00:00Zam", None, "permit", dt.time(0, 0, 0, tzinfo=UTC)],
337337
["12:00:00pm", None, "forbid", dt.time(12, 0, 0)],
338-
["1970-01-01", "%Y-%m-%d", "forbid", dt.time(0, 0)],
339-
# not great that it effectively returns incorrect time object here. However, this would be
340-
# down to user error in setting up the dischema.
341338
[dt.datetime(2025, 12, 1, 13, 0, 5), "%H:%M:%S", "forbid", dt.time(13, 0, 5)],
342339
[dt.datetime(2025, 12, 1, 13, 0, 5, tzinfo=UTC), "%H:%M:%S", "require", dt.time(13, 0, 5, tzinfo=UTC)],
343340
[dt.time(13, 0, 0), "%H:%M:%S", "forbid", dt.time(13, 0, 0)],
@@ -364,6 +361,9 @@ def test_formattedtime(
364361
["23:00:00", "%I:%M:%S", "permit",],
365362
["23:00:00", "%H:%M:%S", "require",],
366363
["23:00:00Z", "%I:%M:%S", "forbid",],
364+
["2:10:13", "%H:%M:%S", "forbid",],
365+
["20:0:13", "%H:%M:%S", "forbid",],
366+
["20:10:1", "%H:%M:%S", "forbid",],
367367
[dt.datetime(2025, 12, 1, 13, 0, 5, tzinfo=UTC), "%H:%M:%S", "forbid",],
368368
[dt.time(13, 0, 5, tzinfo=UTC), "%H:%M:%S", "forbid",],
369369
["12:00", "%H:%M:%S", "forbid",],

0 commit comments

Comments
 (0)