Skip to content

Commit 7a77373

Browse files
vanzhiganovkvesteri
authored andcommitted
Add domain validator, refs #9
1 parent ca23a8e commit 7a77373

6 files changed

Lines changed: 80 additions & 15 deletions

File tree

CHANGES.rst

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
Changelog
22
---------
33

4-
0.8.0 (unreleased)
4+
5+
0.9.0 (2015-10-10)
6+
^^^^^^^^^^^^^^^^^^
7+
8+
- Added new validator: ``domain``
9+
10+
11+
0.8.0 (2015-06-24)
512
^^^^^^^^^^^^^^^^^^
613

714
- Added new validator: ``iban``

docs/index.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,14 @@ between
6868
.. autofunction:: between
6969

7070

71+
domain
72+
------
73+
74+
.. module:: validators.domain
75+
76+
.. autofunction:: domain
77+
78+
7179
email
7280
-----
7381

tests/test_domain.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# -*- coding: utf-8 -*-
2+
import pytest
3+
from validators import domain, ValidationFailure
4+
5+
6+
@pytest.mark.parametrize(('value',), [
7+
('example.com',),
8+
])
9+
def test_returns_true_on_valid_domain(value):
10+
assert domain(value)
11+
12+
13+
@pytest.mark.parametrize(('value',), [
14+
('example.com/',),
15+
('example.com:4444',),
16+
('example.-com',),
17+
('example.',),
18+
('example',),
19+
])
20+
def test_returns_failed_validation_on_invalid_domain(value):
21+
assert isinstance(domain(value), ValidationFailure)

validators/__init__.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
from .between import between
2-
from .email import email
3-
from .extremes import Min, Max
4-
from .iban import iban
5-
from .ip_address import ipv4, ipv6
6-
from .i18n import fi_business_id, fi_ssn
7-
from .length import length
8-
from .mac_address import mac_address
9-
from .slug import slug
10-
from .truthy import truthy
11-
from .url import url
12-
from .utils import ValidationFailure, validator
13-
from .uuid import uuid
1+
from .between import between # noqa
2+
from .domain import domain # noqa
3+
from .email import email # noqa
4+
from .extremes import Min, Max # noqa
5+
from .iban import iban # noqa
6+
from .ip_address import ipv4, ipv6 # noqa
7+
from .i18n import fi_business_id, fi_ssn # noqa
8+
from .length import length # noqa
9+
from .mac_address import mac_address # noqa
10+
from .slug import slug # noqa
11+
from .truthy import truthy # noqa
12+
from .url import url # noqa
13+
from .utils import ValidationFailure, validator # noqa
14+
from .uuid import uuid # noqa
1415

1516
__version__ = '0.8'

validators/domain.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import re
2+
from .utils import validator
3+
4+
pattern = re.compile(
5+
r'^(?!\-)(?:[a-zA-Z\d\-]{0,62}[a-zA-Z\d]\.)'
6+
r'{1,126}(?!\d+)[a-zA-Z\d]{1,63}$'
7+
)
8+
9+
10+
@validator
11+
def domain(value):
12+
"""
13+
Return whether or not given value is a valid domain.
14+
15+
If the value is valid domain name this function returns ``True``, otherwise
16+
:class:`~validators.utils.ValidationFailure`.
17+
18+
Examples::
19+
20+
>>> domain('example.com')
21+
True
22+
23+
>>> domain('example.com/')
24+
ValidationFailure(func=domain, ...)
25+
26+
:param value: domain string to validate
27+
"""
28+
return pattern.match(value)

validators/i18n/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
from .fi import fi_ssn, fi_business_id
1+
from .fi import fi_ssn, fi_business_id # noqa

0 commit comments

Comments
 (0)