Open
Description
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__}
.