Skip to content

multiple pydantic model validators bug fixed #215

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

Merged
merged 1 commit into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pydantic_xml/serializers/factories/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def from_core_schema(cls, schema: pcs.ModelSchema, ctx: Serializer.Context) -> '
model_cls = schema['cls']
fields_schema = schema['schema']

if fields_schema['type'] == 'function-before':
while fields_schema['type'] == 'function-before':
fields_schema = fields_schema['schema']

assert issubclass(model_cls, pxml.BaseXmlModel), "model class must be a BaseXmlModel subclass"
Expand Down
52 changes: 50 additions & 2 deletions tests/test_preprocessors.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from typing import List
from typing import Any, Dict, List

import pydantic as pd
import pytest
from helpers import assert_xml_equal

from pydantic_xml import BaseXmlModel, element, xml_field_serializer, xml_field_validator
from pydantic_xml import BaseXmlModel, attr, element, xml_field_serializer, xml_field_validator
from pydantic_xml.element import XmlElementReader, XmlElementWriter


Expand Down Expand Up @@ -54,3 +56,49 @@ def serialize_element(self, element: XmlElementWriter, value: List[int], field_n

actual_xml = obj.to_xml()
assert_xml_equal(actual_xml, expected_xml)


def test_pydantic_model_validator():
class TestModel(BaseXmlModel, tag='model1'):
text: str
attr1: str = attr()
attr2: str = attr()

@pd.model_validator(mode='before')
def validate_before_attr1(cls, values: Dict[str, Any]) -> Dict[str, Any]:
if values.get('attr1') != "expected attr value":
raise ValueError('attr1')

return values

@pd.model_validator(mode='before')
def validate_before_attr2(cls, values: Dict[str, Any]) -> Dict[str, Any]:
if values.get('attr2') != "expected attr value":
raise ValueError('attr2')

return values

@pd.model_validator(mode='after')
def validate_model(self) -> 'TestModel':
if self.text != "expected text value":
raise ValueError('text')

return self

xml = '<model1 attr1="expected attr value" attr2="expected attr value">expected text value</model1>'
TestModel.from_xml(xml)

xml = '<model1 attr1="unexpected attr value" attr2="expected attr value">expected text value</model1>'
with pytest.raises(ValueError) as err:
TestModel.from_xml(xml)
assert err.value.errors()[0]['ctx']['orig'] == 'Value error, attr1'

xml = '<model1 attr1="expected attr value" attr2="unexpected attr value">expected text value</model1>'
with pytest.raises(ValueError) as err:
TestModel.from_xml(xml)
assert err.value.errors()[0]['ctx']['orig'] == 'Value error, attr2'

xml = '<model1 attr1="expected attr value" attr2="expected attr value">unexpected text value</model1>'
with pytest.raises(ValueError) as err:
TestModel.from_xml(xml)
assert err.value.errors()[0]['ctx']['orig'] == 'Value error, text'
Loading