diff --git a/backend/src/validation/utils.py b/backend/src/validation/utils.py index d08708e..7550f21 100644 --- a/backend/src/validation/utils.py +++ b/backend/src/validation/utils.py @@ -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 \ No newline at end of file diff --git a/backend/src/weather/schemas.py b/backend/src/weather/schemas.py index b35891b..0cd7f84 100644 --- a/backend/src/weather/schemas.py +++ b/backend/src/weather/schemas.py @@ -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, ) @@ -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 )