Skip to content

Commit 02f52af

Browse files
authored
Merge pull request #722 from cordada/bugfix/pydantic-rut-json-schema-regex
extras: Pydantic `Rut` type regex is not compliant with JSON Schema
2 parents 7a433b2 + d885a3c commit 02f52af

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

src/cl_sii/extras/pydantic_types.py

+18-4
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44

55
from __future__ import annotations
66

7+
import re
78
import sys
8-
from typing import Any
9+
from typing import Any, ClassVar, Pattern
910

1011

1112
if sys.version_info[:2] >= (3, 9):
@@ -74,6 +75,21 @@ class _RutPydanticAnnotation:
7475
b'"78773510-K"'
7576
"""
7677

78+
RUT_CANONICAL_STRICT_REGEX: ClassVar[Pattern] = re.compile(
79+
re.sub(
80+
pattern=r'\?P<\w+>',
81+
repl='',
82+
string=cl_sii.rut.constants.RUT_CANONICAL_STRICT_REGEX.pattern,
83+
)
84+
)
85+
"""
86+
RUT (strict) regex for canonical format, without named groups.
87+
88+
.. warning::
89+
JSON Schema and OpenAPI use the regular expression syntax from
90+
JavaScript (ECMA 262), which does not support Python’s named groups.
91+
"""
92+
7793
@classmethod
7894
def __get_pydantic_core_schema__(
7995
cls, source_type: Any, handler: pydantic.GetCoreSchemaHandler
@@ -83,9 +99,7 @@ def validate_from_str(value: str) -> cl_sii.rut.Rut:
8399

84100
from_str_schema = pydantic_core.core_schema.chain_schema(
85101
[
86-
pydantic_core.core_schema.str_schema(
87-
pattern=cl_sii.rut.constants.RUT_CANONICAL_STRICT_REGEX.pattern
88-
),
102+
pydantic_core.core_schema.str_schema(pattern=cls.RUT_CANONICAL_STRICT_REGEX),
89103
pydantic_core.core_schema.no_info_plain_validator_function(validate_from_str),
90104
]
91105
)

0 commit comments

Comments
 (0)