Skip to content

Commit 12d4df9

Browse files
committed
Fix domain validation
- Updated regex to not allow numeric only TLDs (examples in tests) - Allow for idna encoded domains and test for them (examples in tests) Fixes #47 Fixes #123
1 parent 669129a commit 12d4df9

2 files changed

Lines changed: 13 additions & 4 deletions

File tree

tests/test_domain.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
'3.cn',
1414
'a.cn',
1515
'sub1.sub2.sample.co.uk',
16-
'somerandomexample.xn--fiqs8s'
16+
'somerandomexample.xn--fiqs8s',
17+
'kräuter.com',
18+
'über.com'
1719
])
1820
def test_returns_true_on_valid_domain(value):
1921
assert domain(value)
@@ -29,7 +31,11 @@ def test_returns_true_on_valid_domain(value):
2931
'_example.com',
3032
'example_.com',
3133
'example',
32-
'a......b.com'
34+
'a......b.com',
35+
'a.123',
36+
'123.123',
37+
'123.123.123',
38+
'123.123.123.123'
3339
])
3440
def test_returns_failed_validation_on_invalid_domain(value):
3541
assert isinstance(domain(value), ValidationFailure)

validators/domain.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
r'^(?:[a-z0-9]' # First character of the domain
77
r'(?:[a-z0-9-_]{0,61}[a-z0-9])?\.)' # Sub domain + hostname
88
r'+[a-z0-9][a-z0-9-_]{0,61}' # First 61 characters of the gTLD
9-
r'[a-z0-9]$' # Last character of the gTLD
9+
r'[a-z]$' # Last character of the gTLD
1010
)
1111

1212

@@ -40,4 +40,7 @@ def domain(value):
4040
4141
:param value: domain string to validate
4242
"""
43-
return pattern.match(value)
43+
try:
44+
return pattern.match(value.encode('idna').decode())
45+
except UnicodeError as e:
46+
return False

0 commit comments

Comments
 (0)