Skip to content

Commit 8067721

Browse files
kingbuzzmankvesteri
authored andcommitted
Fixes issue with domains that have a single character (python-validators#122)
1 parent 34d355e commit 8067721

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

tests/test_domain.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
'underscore_subdomain.example.com',
1111
'something.versicherung',
1212
'11.com',
13+
'3.cn',
14+
'a.cn',
15+
'sub1.sub2.sample.co.uk',
1316
'somerandomexample.xn--fiqs8s'
1417
])
1518
def test_returns_true_on_valid_domain(value):
@@ -22,6 +25,9 @@ def test_returns_true_on_valid_domain(value):
2225
'example.-com',
2326
'example.',
2427
'-example.com',
28+
'example-.com',
29+
'_example.com',
30+
'example_.com',
2531
'example',
2632
'a......b.com'
2733
])

validators/domain.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
from .utils import validator
44

55
pattern = re.compile(
6-
r'^(:?(([a-zA-Z]{1})|([a-zA-Z]{1}[a-zA-Z]{1})|' # domain pt.1
7-
r'([a-zA-Z]{1}[0-9]{1})|([0-9]{1}[a-zA-Z]{1})|' # domain pt.2
8-
r'([a-zA-Z0-9][-_a-zA-Z0-9]{0,61}[a-zA-Z0-9]))\.)+' # domain pt.3
9-
r'([a-zA-Z]{2,13}|(xn--[a-zA-Z0-9]{2,30}))$' # TLD
6+
r'^(?:[a-z0-9]' # First character of the domain
7+
r'(?:[a-z0-9-_]{0,61}[a-z0-9])?\.)' # Sub domain + hostname
8+
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
1010
)
1111

1212

0 commit comments

Comments
 (0)