Skip to content

Conversation

@dapper91
Copy link
Owner

@dapper91 dapper91 commented Dec 17, 2023

Validation errors enhanced:

  • validation errors provide the full path to the malformed field (including nested sub-models).
  • error text contain the xml document source line where the error occurred (lxml parser only).

Example:

from typing import Literal, Optional, Tuple

from pydantic_xml import BaseXmlModel, attr, element


class Product(BaseXmlModel):
    status: Literal['running', 'development'] = attr()
    launched: Optional[int] = attr(default=None)
    title: str


class Company(BaseXmlModel, tag='Company'):
    products: Tuple[Product, ...] = element(tag='product')


xml = '''
<Company>
    <product status="runing" launched="2013">Several launch vehicles</product>
    <product status="running" launched="a2019">Starlink</product>
    <product status="development">Starship</product>
</Company>
'''

Company.from_xml(xml)

text before:

pydantic_core._pydantic_core.ValidationError: 1 validation error for Product
status
  Input should be 'running' or 'development' [type=literal_error, input_value='runing', input_type=str]
    For further information visit https://errors.pydantic.dev/2.5/v/literal_error

text after:

pydantic_core._pydantic_core.ValidationError: 2 validation errors for Company
products.0.status
  [line 3]: Input should be 'running' or 'development' [type=literal_error, input_value='runing', input_type=str]
products.1.launched
  [line 4]: Input should be a valid integer, unable to parse string as an integer [type=int_parsing, input_value='a2019', input_type=str]

Fixes the issue #81.

@codecov-commenter
Copy link

Codecov Report

Attention: 13 lines in your changes are missing coverage. Please review.

Comparison is base (f38d18f) 91.98% compared to head (d785fb2) 91.71%.

Files Patch % Lines
pydantic_xml/serializers/factories/model.py 86.48% 5 Missing ⚠️
pydantic_xml/element/native/lxml.py 0.00% 3 Missing ⚠️
pydantic_xml/element/element.py 91.30% 2 Missing ⚠️
pydantic_xml/serializers/factories/primitive.py 83.33% 1 Missing ⚠️
pydantic_xml/serializers/factories/raw.py 80.00% 1 Missing ⚠️
pydantic_xml/serializers/factories/wrapper.py 88.88% 1 Missing ⚠️

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##              dev     #150      +/-   ##
==========================================
- Coverage   91.98%   91.71%   -0.28%     
==========================================
  Files          26       26              
  Lines        1410     1508      +98     
==========================================
+ Hits         1297     1383      +86     
- Misses        113      125      +12     
Flag Coverage Δ
unittests 91.71% <91.55%> (-0.28%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@dapper91 dapper91 changed the title Validation error improves Validation errors enhanced Dec 18, 2023
@dapper91 dapper91 merged commit 7647e8a into dev Dec 18, 2023
@dapper91 dapper91 mentioned this pull request Dec 18, 2023
Merged
dapper91 added a commit that referenced this pull request Dec 18, 2023
- validation errors provide the full path to the malformed field (including nested sub-models).
- error text contain the xml document source line where the error occurred (lxml parser only). See #150.
@dapper91 dapper91 deleted the errors-improve branch January 13, 2024 06:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants