Skip to content

Commit e08b548

Browse files
ranjan-sthaRup-Narayan-Rajbanshi
authored andcommitted
Fix(EMDAT): add validation to the start and end dates
1 parent fd9428d commit e08b548

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

pystac_monty/validators/em_dat.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import logging
2+
from datetime import date
23
from typing import Any, Optional, Union
34

45
import pandas as pd
5-
from pydantic import BaseModel, ConfigDict, ValidationInfo, field_validator
6+
from pydantic import BaseModel, ConfigDict, ValidationInfo, field_validator, model_validator
67

78
logger = logging.getLogger(__name__)
89
logging.basicConfig(level=logging.INFO)
@@ -67,3 +68,37 @@ def replace_nan_with_none(cls, value: Any, info: ValidationInfo):
6768
if pd.isna(value):
6869
return None
6970
return value
71+
72+
@model_validator(mode="after")
73+
def validate_date_order(self):
74+
"""Validate the start_date and end_date in order"""
75+
try:
76+
if all([self.start_year, self.start_month, self.start_day]):
77+
start_date = date(
78+
self.start_year,
79+
self.start_month,
80+
self.start_day,
81+
)
82+
else:
83+
start_date = None
84+
except (TypeError, ValueError):
85+
start_date = None
86+
87+
# Build end date if complete
88+
try:
89+
if all([self.end_year, self.end_month, self.end_day]):
90+
end_date = date(
91+
self.end_year,
92+
self.end_month,
93+
self.end_day,
94+
)
95+
else:
96+
end_date = None
97+
except (TypeError, ValueError):
98+
end_date = None
99+
100+
# Compare only if both dates exist
101+
if start_date and end_date and start_date > end_date:
102+
raise ValueError("Start date cannot be later than end date.")
103+
104+
return self

0 commit comments

Comments
 (0)