Skip to content

model_validate_json uses dict for property instead of Pydantic model type #1296

Answered by mcantrell
mcantrell asked this question in Questions
Discussion options

You must be logged in to vote

While trying to fix the problem with a custom field_validator, I noticed that my validator wasn't being called. I found #52 (comment) while trying to research related field_validator questions. It seems that adding the validate_assignment config fixes the issue.

class SqlModelWidget(SQLModel, table=True):
    __tablename__ = "widget"

    id: str = Field(default=None, primary_key=True)
    gadget: Optional[Gadget] = Field(default=None, sa_type=JSON)

    class Config:
        validate_assignment = True

Updated test passes:

import json
from typing import Optional, Any

from pydantic import BaseModel, field_validator
from sqlalchemy import JSON
from sqlmodel import SQLModel, Field

from finra.

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by mcantrell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
1 participant