Skip to content
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
6 changes: 5 additions & 1 deletion pydantic_xml/serializers/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,11 @@ def preprocess_schema(cls, schema: pcs.CoreSchema, ctx: Context) -> Tuple[pcs.Co
elif schema_type == 'nullable':
ctx = ctx.replace(optional=True)

inner_schema = schema['schema']
if schema_type == 'function-plain':
inner_schema = schema['serialization']
else:
inner_schema = schema['schema']

return cls.preprocess_schema(inner_schema, ctx)

elif type_family is SchemaTypeFamily.JSON_OR_PYTHON:
Expand Down
18 changes: 18 additions & 0 deletions tests/test_preprocessors.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import datetime as dt
from typing import Any, Dict, List

import pydantic as pd
Expand Down Expand Up @@ -105,3 +106,20 @@ def validate_model(self) -> 'TestModel':
with pytest.raises(ValueError) as err:
TestModel.from_xml(xml)
assert err.value.errors()[0]['ctx']['orig'] == 'Value error, text'


def test_plain_validator():
class TestModel(BaseXmlModel, tag='model1'):
element1: dt.date = element()

@pd.field_validator('element1', mode='plain')
def validate_element1(cls, value: Any) -> Any:
return dt.datetime.strptime(value, '%Y%m%d').date()

xml = '<model1><element1>20260517</element1></model1>'
model = TestModel.from_xml(xml)
assert model.element1 == dt.date(2026, 5, 17)

with pytest.raises(ValueError) as err:
xml = '<model1><element1>aabb</element1></model1>'
TestModel.from_xml(xml)
Loading