From c67a6cdef13e33abbebcce19f886e0cfbe28a3f2 Mon Sep 17 00:00:00 2001 From: "mauro.cortellazzi" Date: Wed, 24 Jul 2024 17:10:12 +0200 Subject: [PATCH] added field type validation in column definition --- api/app/models/model_dto.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/api/app/models/model_dto.py b/api/app/models/model_dto.py index 05a1ea4c..648f848b 100644 --- a/api/app/models/model_dto.py +++ b/api/app/models/model_dto.py @@ -43,6 +43,28 @@ class ColumnDefinition(BaseModel, validate_assignment=True): def to_dict(self): return self.model_dump() + @model_validator(mode='after') + def validate_field_type(self) -> Self: + match (self.type, self.field_type): + case (SupportedTypes.datetime, FieldType.datetime): + return self + case (SupportedTypes.string, FieldType.categorical): + return self + case (SupportedTypes.bool, FieldType.categorical): + return self + case (SupportedTypes.int, FieldType.categorical): + return self + case (SupportedTypes.float, FieldType.categorical): + return self + case (SupportedTypes.int, FieldType.numerical): + return self + case (SupportedTypes.float, FieldType.numerical): + return self + case _: + raise ValueError( + f'column {self.name} with type {self.type} can not have filed type {self.field_type}' + ) + class OutputType(BaseModel, validate_assignment=True): prediction: ColumnDefinition