field_validator is not supported? #787
Answered
by
joachimhuet
ferdousbhai
asked this question in
Questions
-
First Check
Commit to Help
Example Codefrom sqlmodel import SQLModel, Field, field_validator
class Market(SQLModel, table=True):
id: int | None = Field(default=None, primary_key=True)
question: str
description: str
outcomes: list[str] = Field(
default_factory=list, description="list of possible outcomes"
)
outcomePrices: list[float] = Field(
default_factory=list, description="probabilities of each outcome"
)
volume: float
categories: list[str] = Field(
default_factory=list, description="list of categories"
)
@field_validator("categories", mode="before")
def extract_category_labels(cls, categories):
if categories:
return [category["label"] for category in categories]
return []
@field_validator("outcomes", "outcomePrices", mode="before")
def parse_lists(cls, v):
if isinstance(v, str):
return json.loads(v)
return v DescriptionI want to pre-process some data before initializing the model. Works fine with Pydantic but seems Pydantic's 'field_validator' is missing in sqlmodel. Operating SystemLinux Operating System DetailsNo response SQLModel Versionlatest Python Version3.11 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Answered by
joachimhuet
May 3, 2024
Replies: 1 comment 1 reply
-
Did you find a response separately? Facing the same issue |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Validation is not triggered when
table=True
by choice. See #52 for alternatives. I would suggest you to use a non table class with the validation and then inherit it from yourtable=True
.Like so: