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

Please add documentation and examples for ValidationError.from_exception_data() and InitErrorDetails #1452

Open
Tracked by #10468
davepeck opened this issue Sep 13, 2024 · 1 comment
Labels
documentation Improvements or additions to documentation

Comments

@davepeck
Copy link

davepeck commented Sep 13, 2024

I'd love to see ValidationError.from_exception_data() documented in more detail, with examples.

I realize that constructing a ValidationError directly is very rarely the "right" thing to do... but I've recently run into a case where it turns out to be exactly the right thing to do. Unfortunately, the documentation is lacking.

Here's a simple example for those looking to walk down this dark path:

from pydantic_core import ValidationError, InitErrorDetails

def build_validation_error():
   details = InitErrorDetails(loc=("field",), type="missing", input=None)
   return ValidationError.from_exception_data(title="title", line_errors=[details])

Testing this:

>>> validation_error = build_validation_error()
>>> print(validation_error.json())
[{"type":"missing","loc":["field"],"msg":"Field required","input":null,"url":"https://errors.pydantic.dev/2.9/v/missing"}]

A certain amount of experimentation seems required because different error types seem to have different expectations for the contents of InitErrorDetails. For instance, if you have a nested: NestedModel field, and construct an InitErrorDetails with type="model_type", you'll also want to set ctx={"class_name": NestedModel.__name__}.

@davepeck
Copy link
Author

Linking also to #963

@sydney-runkle sydney-runkle added refactor documentation Improvements or additions to documentation and removed refactor labels Sep 16, 2024
@davepeck davepeck changed the title Please add detailed documentation and examples for ValidationError.from_exception_data() and InitErrorDetails Please add documentation and examples for ValidationError.from_exception_data() and InitErrorDetails Sep 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

2 participants