I'd like to parse object, which initially is not corresponded to the model, means it's fields not match target schema. So I'd like to inference model values, based on other values of the raw object. In the following scenario I'd like to give foo
the value of first key of src
dict, just for example.
class Model(BaseModel):
foo: int
@validator('foo', pre=True, always=True, check_fields=False)
def convert_foo(cls, v, values):
v = values['src']
assert isinstance(v, dict)
return [*v.keys()][0]
m = Model.parse_obj({'src': {1: 2}})
and I got
pydantic.error_wrappers.ValidationError: 1 validation error for Model
foo
field required (type=value_error.missing)
How to deal with that? Is the only option to use root validator?