Tagged Union field validation results in the wrong error messages. #6543
-
First Check
Commit to Help
Example CodeI've written a test which shows the problem:
https://github.com/ojii/fastapi/blob/tagged-union-validation/tests/test_tagged_union_field_validation_errors.pyDescriptionGiven ```python class B(BaseModel): class Item(BaseModel): I think it should be: {
"detail": [
{
"loc": ["body", "value", "value"],
"msg": "ensure this value is less than or equal to 1",
"type": "value_error.number.not_le",
"ctx": {"limit_value": 1},
},
]
}Operating SystemmacOS Operating System Details(os should not matter) FastAPI Version0.68.0 (25a4828) Python Version3.8.3 and 3.9.1 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
|
sorry, this is a bug in pydantic. will report it there. |
Beta Was this translation helpful? Give feedback.
-
|
This is not a bug, but just how it works. You can make error message cleaner using discriminated unions: https://docs.pydantic.dev/latest/concepts/unions/#discriminated-unions |
Beta Was this translation helpful? Give feedback.
This is not a bug, but just how it works.
Pydantic tries to validate data using all types in Union and collect all error messages.
You can make error message cleaner using discriminated unions: https://docs.pydantic.dev/latest/concepts/unions/#discriminated-unions