Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

way to validate while ignoring missing values #221

Closed
samuelcolvin opened this issue Jul 3, 2018 · 1 comment
Closed

way to validate while ignoring missing values #221

samuelcolvin opened this issue Jul 3, 2018 · 1 comment

Comments

@samuelcolvin
Copy link
Owner

@samuelcolvin samuelcolvin commented Jul 3, 2018

So if I have

def Foo(BaseModel):
  foo: int
  bar: str

Foo(foo=1) would raise a validation error as bar is missing, but Foo.ignore_missing(foo=1) would pass.

Perhaps this would be better achieved by modifying _process_values so it doesn't always raise validation errors but instead returns them and values.

samuelcolvin added a commit that referenced this issue Jul 4, 2018
samuelcolvin added a commit that referenced this issue Jul 5, 2018
* add validate_model, fix #221

* errors None from validate_model if ok
@mhadam
Copy link

@mhadam mhadam commented Nov 25, 2020

@samuelcolvin was something like Foo.ignore_missing(foo=1) ever implemented? I'd love a way to take off the guard-rails and type and use a model that allows me to annotate model fields as all required but allows for missing values at runtime.

In my use-case, I'm defining a model for a response that could contain many different keys, and those keys are dependent on what's specified in the request. So if I know the response is returned without error, I know which keys are present in the response. Technically all of the fields should be Optional, but in practice I only care about whether I'm handling the underlying datatype right, not whether the value is present. mypy is a bother, telling me to check for None every time I access a field.

Maybe a way to specify validation errors to ignore would work (e.g. value_error.missing)?

edit: In the meantime I'm just using Model.construct

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

2 participants