-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Closed
Milestone
Description
This is discussed at length in #1170 but I'm creating a new issue to make things clearer.
Pydantic needs a way of accessing "context" when validating data, serialising data, creating schema.
Might be used via MyModel.parse_obj(raw_data, context=my_context).
Use cases:
- dynamic choices - E.g. when choosing from a select based on a entities you have access to in a db, obviously both the validation and schema for the field should be dynamic - this is very common frustration for me
- dynamic parameters on fields, e.g. max and min
- cases like this
- and so many more...
Questions/concerns:
- adding context with
parse_obj, but I don't see way way to add context withMyModel(**data)maybe that's fine - how do we include context to field definitions? e.g.
foobar: conint(gt=context['...'])won't work - how will context work in sub-models?
- will this require breaking changes that need it to go into v2? I hope not.
Another option is to encourage one of the options from #1170 and/or make one of them easier.
Feedback on how this might work and when you would use it very welcome.
fadedDexofan, jeremyschulman, michaelurban, kozlek, nourselim0 and 28 more