Skip to content

Commit 88d972e

Browse files
authored
Merge pull request #27 from codeuniversity/data-start-date
Set diffrent validation values for start and end date of the temperat…
2 parents d30058b + 0d38c4f commit 88d972e

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

backend/src/validation/utils.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,17 @@ def validate_timestamp_start_date_before_end_date(startDate, endDate):
2626
raise HTTPException(
2727
status_code=400, detail="endDate must be after startDate")
2828
return endDate
29+
30+
31+
def validate_temperature_timestamp_in_range(start, end):
32+
min_timestamp = -946771200 # 01/01/1940
33+
max_timestamp = int(time.time()) # now
34+
print(end)
35+
print(max_timestamp)
36+
if (start < min_timestamp or end > max_timestamp):
37+
raise HTTPException(
38+
status_code=400, detail=f"Timestamp must be between {min_timestamp} and {max_timestamp}")
39+
elif end <= start:
40+
raise HTTPException(
41+
status_code=400, detail="endDate must be after startDate")
42+
return end

backend/src/weather/schemas.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from src.constants import AggregationMethod, LocationName, TemporalResolution, Unit
66
from src.validation.utils import (
7-
validate_timestamp_in_range,
7+
validate_temperature_timestamp_in_range,
88
validate_timestamp_start_date_before_end_date,
99
)
1010

@@ -33,14 +33,9 @@ class WeatherDataRequest(BaseModel):
3333
)
3434
aggregation: AggregationMethod = Field(..., description="Aggregation method")
3535

36-
# Custom validator to check if timestamps are valid and in the correct order
37-
@field_validator("startDate")
38-
def validate_timestamp_in_range(cls, v):
39-
return validate_timestamp_in_range(v)
40-
4136
@field_validator("endDate")
42-
def end_date_must_be_after_start_date(cls, v, info: ValidationInfo):
43-
return validate_timestamp_start_date_before_end_date(
37+
def validate_temperature_timestamp_in_range(cls, v, info: ValidationInfo):
38+
return validate_temperature_timestamp_in_range(
4439
info.data.get("startDate"), v
4540
)
4641

0 commit comments

Comments
 (0)