Skip to content

Commit

Permalink
Merge pull request #216 from dapper91/dev
Browse files Browse the repository at this point in the history
- multiple pydantic model validators bug fixed.
  • Loading branch information
dapper91 authored Oct 3, 2024
2 parents 4a4c8e8 + b95ef02 commit b309d04
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 4 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Changelog
=========

2.13.1 (2024-10-03)
-------------------

- multiple pydantic model validators bug fixed.


2.13.0 (2024-09-29)
-------------------

Expand Down
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
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pydantic-xml"
version = "2.13.0"
version = "2.13.1"
description = "pydantic xml extension"
authors = ["Dmitry Pershin <[email protected]>"]
license = "Unlicense"
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'

0 comments on commit b309d04

Please sign in to comment.