-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Closed
Labels
bug V1Bug related to Pydantic V1.XBug related to Pydantic V1.X
Description
from @dmfigol on gitter:
I am failing to understand why validator is applied several times on the complex field:
import json
from typing import List, Dict, Any
from pydantic import BaseModel, ValidationError, validator
class DemoModel(BaseModel):
data: Dict[str, Any] = {}
@validator('data', pre=True, whole=True)
def json_decode(cls, v):
print(f'value is {v!r}, type: {type(v)}')
if isinstance(v, str):
try:
return json.loads(v)
except ValueError:
pass
return v
print(DemoModel(data='{"foo": {"bar": 123}}'))
# > value is '{"foo": {"bar": 123}}', type: <class 'str'>
# > value is 'foo', type: <class 'str'>
# > DemoModel data={'foo': {'bar': 123}}I would appreciate if you could explain :) because of this I spent a lot of time debugging my code and after I found the problem, I still don't understand why it is working this way :(
Metadata
Metadata
Assignees
Labels
bug V1Bug related to Pydantic V1.XBug related to Pydantic V1.X