data_models: allow future dates (#25)

This commit is contained in:
Sina Atalay 2024-02-28 21:34:28 +01:00
parent f191f21987
commit 170c4f912e
2 changed files with 17 additions and 31 deletions

View File

@ -208,22 +208,15 @@ class EntryBase(RenderCVBaseModel):
end_date_is_provided = True
if date_is_provided:
try:
date_object = get_date_object(model.date) # type: ignore
except ValueError:
# Then it is a custom date string (e.g., "My Custom Date")
pass
else:
today_object = Date.today()
if date_object > today_object:
raise ValueError(
'"date" cannot be in the future!',
"date", # this is the location of the error
model.date, # this is value of the error
)
model.start_date = None
model.end_date = None
elif start_date_is_provided and not end_date_is_provided:
model.end_date = "present"
if re.fullmatch(date_pattern_for_json_schema, model.date):
# Then it is in YYYY-MM-DD, YYYY-MM, or YYYY format
try:
get_date_object(model.date)
except ValueError as e:
raise ValueError(str(e), "date", str(model.date))
elif not start_date_is_provided and end_date_is_provided:
raise ValueError(
@ -232,8 +225,11 @@ class EntryBase(RenderCVBaseModel):
"start_date", # this is the location of the error
"", # this supposed to be the value of the error
)
elif start_date_is_provided:
if not end_date_is_provided:
model.end_date = "present"
if model.start_date is not None and model.end_date is not None:
# Check if start_date and end_date are provided correctly:
try:
end_date = get_date_object(model.end_date)
except ValueError as e:
@ -250,12 +246,6 @@ class EntryBase(RenderCVBaseModel):
"start_date", # this is the location of the error
str(model.start_date), # this is value of the error
)
elif end_date > Date.today():
raise ValueError(
'"end_date" cannot be in the future!',
"end_date", # this is the location of the error
str(model.end_date), # this is value of the error
)
return model
@ -515,10 +505,9 @@ class PublicationEntry(RenderCVBaseModel):
@pydantic.field_validator("date")
@classmethod
def check_date(cls, date: int | RenderCVDate) -> int | RenderCVDate:
"""Check if the date is in the past."""
date_object = get_date_object(date)
if date_object > Date.today():
raise ValueError("The publication date cannot be in the future!")
"""Check if the date is a valid date."""
# The function below will raise an error if the date is not valid:
get_date_object(date)
return date

View File

@ -265,7 +265,7 @@ def test_publication_dates(publication_entry, date, expected_date_string):
assert publication_entry.date_string == expected_date_string
@pytest.mark.parametrize("date", ["aaa", None, "2025"])
@pytest.mark.parametrize("date", ["aaa", None, "2025-23-23"])
def test_invalid_publication_dates(publication_entry, date):
with pytest.raises(pydantic.ValidationError):
publication_entry["date"] = date
@ -279,16 +279,13 @@ def test_invalid_publication_dates(publication_entry, date):
("2020-01-01", "aaa", None),
(None, "2020-01-01", None),
("2023-01-01", "2021-01-01", None),
("2999-01-01", None, None),
("2020-01-01", "2999-01-01", None),
("2022", "2021", None),
("2021", "2060", None),
("2025", "2021", None),
(None, None, "2028"),
("2020-01-01", "invalid_end_date", None),
("invalid_start_date", "2021-01-01", None),
("2020-99-99", "2021-01-01", None),
("2020-10-12", "2020-99-99", None),
(None, None, "2020-20-20")
],
)
def test_invalid_dates(start_date, end_date, date):