Allow model_validate_json
to return valid JSON string upon raising error
#9371
Labels
model_validate_json
to return valid JSON string upon raising error
#9371
Initial Checks
Description
Currently, I am using
model_validate_json
to validate the JSON string output.By default, I am hoping to get the JSON output without the fields that Pydantic has raised error on.
For example, if my class is as such:
but the JSON output is:
{ "name" : "Tim", "age": 20.0, "hobby" : "coding"}
It will raised the validation error, indicating "hobby" is type str but it is suppose to be type list[str]. Given that the validate function knows which field is errorneous, can it remove that field away from the JSON and return me the valid JSON instead?
so the code typically look something like this:
within this code, I want to get the valid json that I can load to still display valid fields instead of failing the entire JSON output from the model
Docs: https://docs.pydantic.dev/latest/api/base_model/#pydantic.BaseModel.model_validate_json
Affected Components
.model_dump()
and.model_dump_json()
model_construct()
, pickling, private attributes, ORM modeThe text was updated successfully, but these errors were encountered: