Skip to content

Context when validating data #1549

@samuelcolvin

Description

@samuelcolvin

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 with MyModel(**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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions