Skip to content

Commit ff8eb8c

Browse files
committed
fix: 307 class variable for pattern and updated regexp added tests
1 parent 7e48f6a commit ff8eb8c

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

pydantic_extra_types/domain.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
class DomainStr(str):
1515
"""A string subclass with custom validation for domain string format."""
16+
_domain_re_pattern = r'(?=^.{1,253}$)(^((?!-)[a-zA-Z0-9-]{1,63}(?<!-)\.)+[a-zA-Z]{2,63}$)'
1617

1718
@classmethod
1819
def validate(cls, __input_value: Any, _: Any) -> str:
@@ -37,8 +38,7 @@ def _validate(cls, v: Any) -> DomainStr:
3738
if len(v) < 1 or len(v) > 253:
3839
raise PydanticCustomError('domain_length', 'Domain must be between 1 and 253 characters')
3940

40-
pattern = r'^([a-z0-9-]+(\.[a-z0-9-]+)+)$'
41-
if not re.match(pattern, v):
41+
if not re.match(cls._domain_re_pattern, v):
4242
raise PydanticCustomError('domain_format', 'Invalid domain format')
4343

4444
return cls(v)

tests/test_domain.py

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ class MyModel(BaseModel):
2727
'exam ple.com', # Space in domain
2828
'exa_mple.com', # Underscore in domain
2929
'example.com.', # Trailing dot
30+
'192.168.1.23', # Ip address
31+
'192.168.1.0/23', # CIDR
3032
]
3133

3234
very_long_domains = [

0 commit comments

Comments
 (0)