From 6a655ae0b4f670dcab1e6e724fb1e526d6122496 Mon Sep 17 00:00:00 2001 From: "Matheus C. Zaglia" Date: Fri, 16 Apr 2021 10:06:56 -0300 Subject: [PATCH] check if datetime is a str instance before parsing --- stac_pydantic/shared.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/stac_pydantic/shared.py b/stac_pydantic/shared.py index 07dddfa..1b75b60 100644 --- a/stac_pydantic/shared.py +++ b/stac_pydantic/shared.py @@ -123,19 +123,31 @@ def _parse_rfc3339(dt: str): @validator("start_datetime", allow_reuse=True) def validate_start_datetime(cls, v): - return cls._parse_rfc3339(v) + if isinstance(v, str): + return cls._parse_rfc3339(v) + + return v @validator("end_datetime", allow_reuse=True) def validate_start_datetime(cls, v): - return cls._parse_rfc3339(v) + if isinstance(v, str): + return cls._parse_rfc3339(v) + + return v @validator("created", allow_reuse=True) def validate_start_datetime(cls, v): - return cls._parse_rfc3339(v) + if isinstance(v, str): + return cls._parse_rfc3339(v) + + return v @validator("updated", allow_reuse=True) def validate_start_datetime(cls, v): - return cls._parse_rfc3339(v) + if isinstance(v, str): + return cls._parse_rfc3339(v) + + return v class Config: json_encoders = {datetime: lambda v: v.strftime(DATETIME_RFC339)}