From ba2a532eafbc309a2ed59604ae3ec563b6fe249d Mon Sep 17 00:00:00 2001 From: Sujan Adhikari Date: Wed, 16 Oct 2024 17:17:07 +0545 Subject: [PATCH] fix: use field validator and convert str values to int --- src/backend/app/central/central_schemas.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/backend/app/central/central_schemas.py b/src/backend/app/central/central_schemas.py index 0731d5a897..c808608233 100644 --- a/src/backend/app/central/central_schemas.py +++ b/src/backend/app/central/central_schemas.py @@ -22,7 +22,7 @@ from typing import Optional, TypedDict from geojson_pydantic import Feature, FeatureCollection -from pydantic import BaseModel, Field, ValidationInfo, computed_field, validator +from pydantic import BaseModel, Field, ValidationInfo, computed_field from pydantic.functional_validators import field_validator from app.models.enums import TaskStatus @@ -140,11 +140,16 @@ class EntityOsmID(BaseModel): id: str osm_id: Optional[int] = None - @validator("osm_id", pre=True, always=True) + @field_validator("osm_id", mode="before") + @classmethod def convert_osm_id(cls, value): """Set osm_id to None if empty or invalid.""" if value in ("", " "): # Treat empty strings as None return None + try: + return int(value) # Convert to integer if possible + except ValueError: + return value class EntityTaskID(BaseModel): @@ -153,11 +158,16 @@ class EntityTaskID(BaseModel): id: str task_id: Optional[int] = None - @validator("task_id", pre=True, always=True) + @field_validator("task_id", mode="before") + @classmethod def convert_task_id(cls, value): """Set task_id to None if empty or invalid.""" if value in ("", " "): # Treat empty strings as None return None + try: + return int(value) # Convert to integer if possible + except ValueError: + return value class EntityMappingStatus(EntityOsmID, EntityTaskID):