-
Notifications
You must be signed in to change notification settings - Fork 27
Description
Hello! I am working in Python 3.9.18 and am on the latest version of the library. I discovered recently that something about the from_xml call prevents pydantic model validators from executing in the correct order.
Take this minimal example (also true of "before" validators):
from pydantic import Field, model_validator, ModelWrapValidatorHandler
from pydantic_xml import BaseXmlModel, element
from typing import Any
from typing_extensions import Self
class Bar(BaseXmlModel):
baz: int = Field()
class Foo(BaseXmlModel):
bar: list[Bar] = element()
@model_validator(mode="wrap")
@classmethod
def test_validation_order(
cls,
data: Any,
handler: ModelWrapValidatorHandler[Self],
) -> Self:
print("hello world")
return handler(data)When I use a dict input and use pydantic's constructor:
input = {
"bar": [
{"baz": 1},
{"baz": 2},
{"baz": "hi"},
{"baz": 3},
]
}
Foo.model_validate(input)
#> hello world
#> ValidationError: ...versus an xml input and the pydantic-xml constructor:
input_xml = """
<Foo>
<bar>1</bar>
<bar>1</bar>
<bar>hi</bar>
<bar>1</bar>
</Foo>
"""
Foo.from_xml(input_xml)
#> ValidationError: ... # print statement is skippedI would expect the print statement to execute before validation begins, in the same way it does with the base library. Without that, it isn't possible to capture nested errors or perform any model-modifying actions in the same way one would with pydantic.
If I am missing a configuration or something, please let me know. This library has saved me an uncountable amount of time for my project.