Skip to content
Merged
Show file tree
Hide file tree
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
14 changes: 14 additions & 0 deletions backend/src/validation/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,17 @@ def validate_timestamp_start_date_before_end_date(startDate, endDate):
raise HTTPException(
status_code=400, detail="endDate must be after startDate")
return endDate


def validate_temperature_timestamp_in_range(start, end):
min_timestamp = -946771200 # 01/01/1940
max_timestamp = int(time.time()) # now
print(end)
print(max_timestamp)
if (start < min_timestamp or end > max_timestamp):
raise HTTPException(
status_code=400, detail=f"Timestamp must be between {min_timestamp} and {max_timestamp}")
elif end <= start:
raise HTTPException(
status_code=400, detail="endDate must be after startDate")
return end
11 changes: 3 additions & 8 deletions backend/src/weather/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from src.constants import AggregationMethod, LocationName, TemporalResolution, Unit
from src.validation.utils import (
validate_timestamp_in_range,
validate_temperature_timestamp_in_range,
validate_timestamp_start_date_before_end_date,
)

Expand Down Expand Up @@ -33,14 +33,9 @@ class WeatherDataRequest(BaseModel):
)
aggregation: AggregationMethod = Field(..., description="Aggregation method")

# Custom validator to check if timestamps are valid and in the correct order
@field_validator("startDate")
def validate_timestamp_in_range(cls, v):
return validate_timestamp_in_range(v)

@field_validator("endDate")
def end_date_must_be_after_start_date(cls, v, info: ValidationInfo):
return validate_timestamp_start_date_before_end_date(
def validate_temperature_timestamp_in_range(cls, v, info: ValidationInfo):
return validate_temperature_timestamp_in_range(
info.data.get("startDate"), v
)

Expand Down