Skip to content

Commit

Permalink
Swap to validation after initialisation (#65)
Browse files Browse the repository at this point in the history
* Swap to validation after initialisation

* Fix date bug
  • Loading branch information
robertdstein committed Feb 16, 2024
1 parent a46faf3 commit bd2ac9a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "wintertoo"
version = "1.3.1"
version = "1.3.2"
description = ""
authors = [
{name = "Robert Stein", email = "rdstein@caltech.edu"},
Expand Down
26 changes: 24 additions & 2 deletions wintertoo/models/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,42 @@ class ProgramImageQuery(BaseModel):
)
start_date: int = Field(
title="Start date for images",
le=get_date(Time.now() + 1 * u.day),
default=get_date(Time.now() - 30.0 * u.day),
examples=[get_date(Time.now() - 30.0 * u.day), "20230601"],
)
end_date: int = Field(
title="End date for images",
le=get_date(Time.now() + 1 * u.day),
default=get_date(Time.now()),
examples=[get_date(Time.now() - 30.0 * u.day), get_date(Time.now())],
)
kind: WinterImageTypes = Field(
default=DEFAULT_IMAGE_TYPE, example="raw/science/diff"
)

@model_validator(mode="after")
def validate_date_order(self):
"""
Ensure that the start date is before the end date
:return: validated field value
"""
if self.start_date > self.end_date:
raise WinterValidationError("Start date is after end date")

today = get_date(Time.now())

if self.start_date > today:
raise WinterValidationError(
f"Start date is in the future, (today is {today})"
)

if self.end_date > today:
raise WinterValidationError(
f"End date is in the future, (today is {today})"
)

return self


class TargetImageQuery(ProgramImageQuery):
"""
Expand Down

0 comments on commit bd2ac9a

Please sign in to comment.