question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature Request: Include Model class in ValidationError's line errors

See original GitHub issue

This is a feature request to include the model class for the specific error in a ValidationError’s line errors; the rework for pydantic v2 seems like an ideal opportunity for it. A use-case for this feature is enabling pydantic consumers to surface better errors to their, non-developer, end-users.

Consider the following simplified example:

from typing import Union
from pydantic import BaseModel, ValidationError

class Sub1(BaseModel):
    f1: str
    f2: str
    f3: str
    f4: str

class Sub2(BaseModel):
    f1: str
    a1: str
    a2: str
    a3: str

class Model(BaseModel):
    field: Union[Sub1,Sub2]

try:
    Model.parse_obj({
        "field": {
            "f1": "foo",
            "f2": "bar"
        }
    })
except ValidationError as e:
    for err in e.errors():
        print(err)

we get the following errors:

{'loc': ('field', 'f3'), 'msg': 'field required', 'type': 'value_error.missing'}
{'loc': ('field', 'f4'), 'msg': 'field required', 'type': 'value_error.missing'}
{'loc': ('field', 'a1'), 'msg': 'field required', 'type': 'value_error.missing'}
{'loc': ('field', 'a2'), 'msg': 'field required', 'type': 'value_error.missing'}
{'loc': ('field', 'a3'), 'msg': 'field required', 'type': 'value_error.missing'}

which if simply pretty-printed looks like:

field -> f3
  field required (type=value_error.missing)
field -> f4
  field required (type=value_error.missing)
field -> a1
  field required (type=value_error.missing)
field -> a2
  field required (type=value_error.missing)
field -> a3
  field required (type=value_error.missing)

The problem here is that an end-user trying to figure out what was wrong with their input has to have the experience and presence of mind to look through the model definitions to understand that they’re seeing errors from trying to match against two different model classes and only the first 2 errors are actually relevant to them.

Including the model class in the line errors would allow an application that is using pydantic to do some processing on the list of line errors to provide more meaningful error messages to end users. For example, if the line error dictionaries were actually something like:

{'loc': ('field', 'f3'), 'msg': 'field required', 'type': 'value_error.missing', model: <class 'Sub1'> }
{'loc': ('field', 'f4'), 'msg': 'field required', 'type': 'value_error.missing', model: <class 'Sub1'> }
{'loc': ('field', 'a1'), 'msg': 'field required', 'type': 'value_error.missing', model: <class 'Sub2'> }
{'loc': ('field', 'a2'), 'msg': 'field required', 'type': 'value_error.missing', model: <class 'Sub2'> }
{'loc': ('field', 'a3'), 'msg': 'field required', 'type': 'value_error.missing', model: <class 'Sub2'> }

Then the application could, say, collect the errors from the list that match within line_error['loc'][:-1], group them by model class, figure out that the end-user was most likely trying to write a Sub1 model in this case because they matched the most fields from it (2 matched fields vs 1 match for Sub2), and surface a more directed error like:

field (model: Sub1)
   f3: field required
   f4: field required

Thank you for your consideration.

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
samuelcolvincommented, Nov 24, 2022

This is done 😃.

In the example in the readme, you’ll see input_value=11 - the input value is included in the error as input, There are many examples in the docs, e.g. here it’s also included in the the type in for the new validation error:

https://github.com/pydantic/pydantic-core/blob/a084f30d915f557107e297215e2ca68c6e55a099/pydantic_core/_pydantic_core.pyi#L80-L85

Let me know if anything is still unclear.

0reactions
samuelcolvincommented, Nov 25, 2022

Pydantic’s logic for generating pydantic-core schema is much more sophisticated thank generate_self_schema.py, but it’s somewhat similar. Most of the logic is in pydantic/_internal/_generate_schema.py.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Model Validation in ASP.NET Web API - Microsoft Learn
This article shows how to annotate your models, use the annotations for data validation, and handle validation errors in your web API.
Read more >
How can I customize the error response in Web API with .NET ...
NET Core automatically handles model validation errors by returning a 400 Bad Request with ModelState as the response body.
Read more >
Random model validation errors occur during POST request ...
I get the following error, when executing my POST request: { "errors": ... Post(Model model) { return Ok(); } } public class Model...
Read more >
Customizing a Model Validation Response which results in an ...
This blog post shows how to customize a Model Validation response which results as an HTTP 400 error code. The ConfigureApiBehaviourOptions ...
Read more >
Handling errors in ASP.NET Core Web API - DevTrends
Handling errors in an ASP.NET Core Web API. This post looks at the best ways to handle exceptions, validation and other invalid requests...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found