dapper91 / pydantic-xml

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

Pydantic pattern constraint not supported #142

Closed jordancrane-truework closed 5 months ago

jordancrane-truework commented 7 months ago

Given the following example code:

from pydantic_xml import BaseXmlModel, element

class TestXmlModel(BaseXmlModel, tag="Test"):
    field: str = element(tag="Field", pattern=r"/d+")

TestXmlModel(field="1234")

The following error occurs:

pydantic_xml.errors.ModelError: type chain is not supported

It works fine with other constraint types that I have tried (max_length, min_length).

dapper91 commented 7 months ago

@jordancrane-truework Hi. I think you made a mistake. Regexp digit is \d not /d. This code should work:

from pydantic_xml import BaseXmlModel, element

class TestXmlModel(BaseXmlModel, tag="Test"):
    field: str = element(tag="Field", pattern=r"\d+")

TestXmlModel(field="1234")
jordancrane-truework commented 7 months ago

@dapper91 Oops, you're correct, dumb mistake on my part - thanks for the correction. Unfortunately even with the correct regex I still get the same error.

dapper91 commented 7 months ago

@jordancrane-truework Hmm. That code works perfect on my side. Please, could you provide python, pydantic-xml, pydantic and pydantic-core versions you are using.