dapper91 / pydantic-xml

python xml for humans
https://pydantic-xml.readthedocs.io
The Unlicense
141 stars 14 forks source link

Validation errors enhanced #150

Closed dapper91 closed 6 months ago

dapper91 commented 6 months ago

Validation errors enhanced:

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 https://github.com/dapper91/pydantic-xml/issues/81.

codecov-commenter commented 6 months ago

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 :warning:
pydantic_xml/element/native/lxml.py 0.00% 3 Missing :warning:
pydantic_xml/element/element.py 91.30% 2 Missing :warning:
pydantic_xml/serializers/factories/primitive.py 83.33% 1 Missing :warning:
pydantic_xml/serializers/factories/raw.py 80.00% 1 Missing :warning:
pydantic_xml/serializers/factories/wrapper.py 88.88% 1 Missing :warning:

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

Additional details and impacted files ```diff @@ 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](https://app.codecov.io/gh/dapper91/pydantic-xml/pull/150/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Dmitry+Pershin) | Coverage Δ | | |---|---|---| | [unittests](https://app.codecov.io/gh/dapper91/pydantic-xml/pull/150/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Dmitry+Pershin) | `91.71% <91.55%> (-0.28%)` | :arrow_down: | Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Dmitry+Pershin#carryforward-flags-in-the-pull-request-comment) to find out more.

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.