Skip to content

Commit e20d025

Browse files
committed
Merge branch 'url_regex_fix' of https://github.com/gokhanm/validators into gokhanm-url_regex_fix
2 parents b1badab + a48c39f commit e20d025

2 files changed

Lines changed: 11 additions & 14 deletions

File tree

‎tests/test_url.py‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,21 @@
44
from validators import url, ValidationFailure
55

66

7-
@pytest.mark.parametrize(('address', 'require_tld'), [
7+
@pytest.mark.parametrize(('address',), [
88
('http://foobar.dk', True),
99
('http://foobar.dk', True),
1010
('http://foobar.museum/foobar', True),
1111
('http://127.0.0.1/foobar', True),
1212
('http://127.0.0.1:9000/fake', True),
1313
('http://localhost/foobar', False),
1414
('http://foobar', False),
15+
('htp://foobar.com', False),
16+
('http://foobar..com', False),
17+
('http://fo..com', False),
18+
('http://fo.com', True),
1519
])
16-
def test_returns_true_on_valid_url(address, require_tld):
17-
assert url(address, require_tld=require_tld)
20+
def test_returns_true_on_valid_url(address):
21+
assert url(address)
1822

1923

2024
@pytest.mark.parametrize(('address',), [

‎validators/url.py‎

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

55
regex = (
6-
r'^[a-z]+://([^/:]+{tld}|([0-9]{{1,3}}\.)'
7-
r'{{3}}[0-9]{{1,3}})(:[0-9]+)?(\/.*)?$'
6+
r'^(http(s)?://)?(www\.)?((\w|-)+)(\.(\w|-)+)+(:\d*)?(/(\w|-|\&|\=|\#)*)*'
87
)
98

10-
pattern_with_tld = re.compile(regex.format(tld=r'\.[a-z]{2,10}'))
11-
pattern_without_tld = re.compile(regex.format(tld=''))
9+
pattern = re.compile(regex)
1210

1311

1412
@validator
15-
def url(value, require_tld=True):
13+
def url(value):
1614
"""
1715
Return whether or not given value is a valid URL.
1816
@@ -29,9 +27,6 @@ def url(value, require_tld=True):
2927
>>> url('http://foobar.dk')
3028
True
3129
32-
>>> url('http://localhost/foobar', require_tld=False)
33-
True
34-
3530
>>> url('http://foobar.d')
3631
ValidationFailure(func=url, ...)
3732
@@ -40,6 +35,4 @@ def url(value, require_tld=True):
4035
:param value: URL address string to validate
4136
"""
4237

43-
if require_tld:
44-
return pattern_with_tld.match(value)
45-
return pattern_without_tld.match(value)
38+
return pattern.match(value)

0 commit comments

Comments
 (0)