Skip to content

Commit 35102a4

Browse files
authored
♻️ Fix regex pattern for Domain (#308)
1 parent 7e48f6a commit 35102a4

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

pydantic_extra_types/domain.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
class DomainStr(str):
1515
"""A string subclass with custom validation for domain string format."""
1616

17+
_domain_re_pattern = r'(?=^.{1,253}$)(^((?!-)[a-zA-Z0-9-]{1,63}(?<!-)\.)+[a-zA-Z]{2,63}$)'
18+
1719
@classmethod
1820
def validate(cls, __input_value: Any, _: Any) -> str:
1921
"""Validate a domain name from the provided value.
@@ -37,8 +39,7 @@ def _validate(cls, v: Any) -> DomainStr:
3739
if len(v) < 1 or len(v) > 253:
3840
raise PydanticCustomError('domain_length', 'Domain must be between 1 and 253 characters')
3941

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

4445
return cls(v)

tests/test_domain.py

Lines changed: 2 additions & 0 deletions
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)