Skip to content

Commit

Permalink
Add support for IDN validation, refs #14
Browse files Browse the repository at this point in the history
  • Loading branch information
kvesteri committed Jan 9, 2016
1 parent 46426b8 commit 3e51bae
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 11 deletions.
6 changes: 6 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ Changelog
---------


0.10.0 (2016-01-09)
^^^^^^^^^^^^^^^^^^^

- Added support for internationalized domain names in ``domain`` validator


0.9.0 (2015-10-10)
^^^^^^^^^^^^^^^^^^

Expand Down
20 changes: 12 additions & 8 deletions tests/test_domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,23 @@
from validators import domain, ValidationFailure


@pytest.mark.parametrize(('value',), [
('example.com',),
@pytest.mark.parametrize('value', [
'example.com',
'xn----gtbspbbmkef.xn--p1ai',
'underscore_subdomain.example.com',
'something.versicherung'
])
def test_returns_true_on_valid_domain(value):
assert domain(value)


@pytest.mark.parametrize(('value',), [
('example.com/',),
('example.com:4444',),
('example.-com',),
('example.',),
('example',),
@pytest.mark.parametrize('value', [
'example.com/',
'example.com:4444',
'example.-com',
'example.',
'-example.com',
'example',
])
def test_returns_failed_validation_on_invalid_domain(value):
assert isinstance(domain(value), ValidationFailure)
2 changes: 1 addition & 1 deletion validators/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
from .utils import ValidationFailure, validator # noqa
from .uuid import uuid # noqa

__version__ = '0.9'
__version__ = '0.10'
18 changes: 16 additions & 2 deletions validators/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
from .utils import validator

pattern = re.compile(
r'^(?!\-)(?:[a-zA-Z\d\-]{0,62}[a-zA-Z\d]\.)'
r'{1,126}(?!\d+)[a-zA-Z\d]{1,63}$'
r'^(([a-zA-Z]{1})|([a-zA-Z]{1}[a-zA-Z]{1})|'
r'([a-zA-Z]{1}[0-9]{1})|([0-9]{1}[a-zA-Z]{1})|'
r'([a-zA-Z0-9][-_.a-zA-Z0-9]{1,61}[a-zA-Z0-9]))\.'
r'([a-zA-Z]{2,13}|[a-zA-Z0-9-]{2,30}.[a-zA-Z]{2,3})$'
)


Expand All @@ -24,6 +26,18 @@ def domain(value):
>>> domain('example.com/')
ValidationFailure(func=domain, ...)
Supports IDN domains as well::
>>> domain('xn----gtbspbbmkef.xn--p1ai')
True
.. versionadded:: 0.9
.. versionchanged:: 0.10
Added support for internationalized domain name (IDN) validation.
:param value: domain string to validate
"""
return pattern.match(value)

0 comments on commit 3e51bae

Please sign in to comment.