Skip to content

Commit 8c51265

Browse files
author
Rhea Lakhotia
committed
Add domain validation tests for DomainStr
1 parent 4a33bab commit 8c51265

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

pydantic_extra_types/domain.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
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}$)'
17+
_domain_re_pattern = (r'(?=^.{1,253}$)'r'(^((?!-)[a-zA-Z0-9-]{1,63}(?<!-)\.)+'r'([a-zA-Z]{2,63}|xn--[a-zA-Z0-9]{2,59})$)')
1818

1919
@classmethod
2020
def validate(cls, __input_value: Any, _: Any) -> str:

tests/test_domain.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,21 @@ def test_very_long_domains(domain: str):
7676
def test_invalid_domain_types(domain: Any):
7777
with pytest.raises(ValidationError, match='Value must be a string'):
7878
MyModel(domain=domain)
79+
80+
def test_domainstr_with_punycode():
81+
class Model(BaseModel):
82+
domain: DomainStr
83+
84+
valid = "xn--7-7sbirhro.xn--80ahmohdapg.xn--80asehdb"
85+
model = Model(domain=valid)
86+
assert model.domain == valid
87+
88+
def test_domainstr_invalid():
89+
class Model(BaseModel):
90+
domain: DomainStr
91+
92+
try:
93+
Model(domain="invalid_domain")
94+
assert False, "Expected ValidationError"
95+
except ValidationError:
96+
pass

0 commit comments

Comments
 (0)