refactor: Add field_validator for files in MessageBase model

This commit is contained in:
Gabriel Luiz Freitas Almeida 2024-06-25 21:48:50 -03:00
commit 5cefc8755b

View file

@ -2,6 +2,7 @@ from datetime import datetime, timezone
from typing import TYPE_CHECKING, List, Optional
from uuid import UUID, uuid4
from pydantic import field_validator
from sqlmodel import JSON, Column, Field, Relationship, SQLModel
if TYPE_CHECKING:
@ -17,6 +18,13 @@ class MessageBase(SQLModel):
text: str
files: list[str] = Field(default_factory=list)
@field_validator("files", mode="before")
@classmethod
def validate_files(cls, value):
if not value:
value = []
return value
@classmethod
def from_message(cls, message: "Message", flow_id: str | None = None):
# first check if the record has all the required fields