Skip to content

fields with a field validator sometimes attempt to read the whitespace between the elements #266

@vroonhof

Description

@vroonhof

I have been struggling with the following issue and finally found what is going wrong. I asked claude 4 to minimize the test case and do a write up

Summary

When using Field() validators on XML attributes in pydantic-xml, the parser incorrectly captures whitespace between XML elements as the field content instead of reading the actual attribute value. This causes validation errors when the whitespace cannot be converted to the expected type.

Expected Behavior

XML attributes with Field validators should read the attribute value from the XML, just like attributes without Field validators.

Actual Behavior

  • With whitespace in XML: Field validator receives whitespace (e.g., '\n ') instead of the attribute value
  • With compact XML: Field validator receives an empty dict and reports the field as missing
  • Without Field validator: Works correctly in both cases

Minimal Reproduction

Environment:

  • pydantic >= 2.0.0
  • pydantic-xml >= 2.0.0

Test case: See minimal_pydantic_xml_bug_demo.py

Sample XML:

<item value="42">
    <child>some content</child>
</item>

Broken Model (with Field validator):

class BrokenModel(BaseXmlModel, tag="item"):
    value: Annotated[int, Field(ge=1, le=100)] = attr(use="required")

Error: Input should be a valid integer, unable to parse string as an integer [input_value='\n ']

Working Model (without Field validator):

class WorkingModel(BaseXmlModel, tag="item"):
    value: int = attr(use="required")

Result: value=42

Root Cause

Field validators appear to interfere with the XML attribute parsing mechanism, causing the parser to capture element text content (whitespace) instead of attribute values.

Workaround

Remove Field validators from XML attribute definitions:

# Instead of:
value: Annotated[int, Field(ge=1, le=100)] = attr(use="required")

# Use:
value: int = attr(use="required")

Impact

This affects any pydantic-xml model that uses Field validators on XML attributes, particularly when parsing real-world XML files that contain whitespace formatting.

Test Output

When you run python minimal_pydantic_xml_bug_demo.py, you should see:

BrokenModel result:
   FAILED: Expected integer 5, got '\n'
   This shows whitespace is captured instead of attribute value

WorkingModel result:
   SUCCESS: value=5

This clearly demonstrates that the Field validator receives whitespace instead of the attribute value.

requirements_bug_demo.txt

minimal_pydantic_xml_bug_demo.txt

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions