Skip to content

Commit fad5965

Browse files
authored
Merge pull request #236 from joe733/workshop
maint: improves `mac_address`, `slug` and `uuid`
2 parents 9499686 + 2f2be68 commit fad5965

File tree

9 files changed

+167
-152
lines changed

9 files changed

+167
-152
lines changed

docs/index.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# Reference
22

3-
::: validators.utils
4-
53
::: validators.between
64

75
::: validators.btc_address
@@ -16,4 +14,18 @@
1614

1715
::: validators.iban
1816

17+
<!-- ::: validators.ip_address -->
18+
1919
::: validators.length
20+
21+
::: validators.mac_address
22+
23+
::: validators.slug
24+
25+
<!-- ::: validators.url -->
26+
27+
::: validators.uuid
28+
29+
---
30+
31+
::: validators.utils

tests/test_mac_address.py

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,36 @@
1+
"""MAC Address."""
12
# -*- coding: utf-8 -*-
3+
4+
# external
25
import pytest
36

7+
# local
48
from validators import mac_address, ValidationFailure
59

610

7-
@pytest.mark.parametrize(('address',), [
8-
('01:23:45:67:ab:CD',),
9-
])
10-
def test_returns_true_on_valid_mac_address(address):
11+
@pytest.mark.parametrize(
12+
("address",),
13+
[
14+
("01:23:45:67:ab:CD",),
15+
("01-23-45-67-ab-CD",),
16+
("01:2F:45:37:ab:CD",),
17+
("A1-2F-4E-68-ab-CD",),
18+
],
19+
)
20+
def test_returns_true_on_valid_mac_address(address: str):
21+
"""Test returns true on valid mac address."""
1122
assert mac_address(address)
1223

1324

14-
@pytest.mark.parametrize(('address',), [
15-
('00:00:00:00:00',),
16-
('01:23:45:67:89:',),
17-
('01:23:45:67:89:gh',),
18-
('123:23:45:67:89:00',),
19-
])
20-
def test_returns_failed_validation_on_invalid_mac_address(address):
25+
@pytest.mark.parametrize(
26+
("address",),
27+
[
28+
("00-00:-00-00-00",),
29+
("01:23:45:67:89:",),
30+
("01:23-45:67-89:gh",),
31+
("123:23:45:67:89:00",),
32+
],
33+
)
34+
def test_returns_failed_validation_on_invalid_mac_address(address: str):
35+
"""Test returns failed validation on invalid mac address."""
2136
assert isinstance(mac_address(address), ValidationFailure)

tests/test_slug.py

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,36 @@
1+
"""Test Slug."""
12
# -*- coding: utf-8 -*-
3+
4+
# external
25
import pytest
36

7+
# local
48
from validators import slug, ValidationFailure
59

610

7-
@pytest.mark.parametrize('value', [
8-
'123-12312-asdasda',
9-
'123____123',
10-
'dsadasd-dsadas',
11-
])
12-
def test_returns_true_on_valid_slug(value):
11+
@pytest.mark.parametrize(
12+
"value",
13+
[
14+
"123-asd-7sda",
15+
"123-k-123",
16+
"dac-12sa-459",
17+
"dac-12sa7-ad31as",
18+
],
19+
)
20+
def test_returns_true_on_valid_slug(value: str):
21+
"""Test returns true on valid slug."""
1322
assert slug(value)
1423

1524

16-
@pytest.mark.parametrize('value', [
17-
'some.slug',
18-
'1231321%',
19-
' 21312',
20-
'123asda&',
21-
])
22-
def test_returns_failed_validation_on_invalid_slug(value):
25+
@pytest.mark.parametrize(
26+
"value",
27+
[
28+
"some.slug&",
29+
"1231321%",
30+
" 21312",
31+
"-47q-p--123",
32+
],
33+
)
34+
def test_returns_failed_validation_on_invalid_slug(value: str):
35+
"""Test returns failed validation on invalid slug."""
2336
assert isinstance(slug(value), ValidationFailure)

tests/test_uuid.py

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,40 @@
1+
"""Test UUIDs."""
12
# -*- coding: utf-8 -*-
2-
from uuid import UUID
33

4+
# standard
5+
from uuid import uuid4, UUID
6+
from typing import Union
7+
8+
# external
49
import pytest
510

11+
# local
612
from validators import uuid, ValidationFailure
713

814

9-
@pytest.mark.parametrize(('value',), [
10-
('2bc1c94f-0deb-43e9-92a1-4775189ec9f8',),
11-
])
12-
def test_returns_true_on_valid_mac_address(value):
13-
assert uuid(value)
14-
15-
16-
@pytest.mark.parametrize(('value',), [
17-
(UUID('2bc1c94f-0deb-43e9-92a1-4775189ec9f8'),),
18-
])
19-
def test_returns_true_on_valid_uuid_object(value):
15+
@pytest.mark.parametrize(
16+
("value",),
17+
[
18+
(uuid4(),),
19+
("2bc1c94f-0deb-43e9-92a1-4775189ec9f8",),
20+
(uuid4(),),
21+
("888256d7c49341f19fa33f29d3f820d7",),
22+
],
23+
)
24+
def test_returns_true_on_valid_uuid(value: Union[str, UUID]):
25+
"""Test returns true on valid uuid."""
2026
assert uuid(value)
2127

2228

23-
@pytest.mark.parametrize(('value',), [
24-
('2bc1c94f-deb-43e9-92a1-4775189ec9f8',),
25-
('2bc1c94f-0deb-43e9-92a1-4775189ec9f',),
26-
('gbc1c94f-0deb-43e9-92a1-4775189ec9f8',),
27-
('2bc1c94f 0deb-43e9-92a1-4775189ec9f8',),
28-
])
29-
def test_returns_failed_validation_on_invalid_mac_address(value):
30-
assert isinstance(uuid(value), ValidationFailure)
31-
32-
33-
@pytest.mark.parametrize(('value',), [
34-
(1,),
35-
(1.0,),
36-
(True,),
37-
(None,),
38-
])
39-
def test_returns_failed_validation_on_invalid_types(value):
29+
@pytest.mark.parametrize(
30+
("value",),
31+
[
32+
("2bc1c94f-deb-43e9-92a1-4775189ec9f8",),
33+
("2bc1c94f-0deb-43e9-92a1-4775189ec9f",),
34+
("gbc1c94f-0deb-43e9-92a1-4775189ec9f8",),
35+
("2bc1c94f 0deb-43e9-92a1-4775189ec9f8",),
36+
],
37+
)
38+
def test_returns_failed_validation_on_invalid_uuid(value: Union[str, UUID]):
39+
"""Test returns failed validation on invalid uuid."""
4040
assert isinstance(uuid(value), ValidationFailure)

validators/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
from .length import length
1414
from .mac_address import mac_address
1515
from .slug import slug
16-
from .truthy import truthy
1716
from .url import url
1817
from .utils import ValidationFailure, validator
1918
from .uuid import uuid
@@ -44,7 +43,6 @@
4443
"sha256",
4544
"sha512",
4645
"slug",
47-
"truthy",
4846
"unionpay",
4947
"url",
5048
"uuid",

validators/mac_address.py

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,37 @@
1+
"""MAC Address."""
2+
# -*- coding: utf-8 -*-
3+
4+
# standard
15
import re
26

7+
# local
38
from .utils import validator
49

5-
pattern = re.compile(r'^(?:[0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$')
6-
710

811
@validator
9-
def mac_address(value):
10-
"""
11-
Return whether or not given value is a valid MAC address.
12-
13-
If the value is valid MAC address this function returns ``True``,
14-
otherwise :class:`~validators.utils.ValidationFailure`.
12+
def mac_address(value: str):
13+
"""Return whether or not given value is a valid MAC address.
1514
16-
This validator is based on `WTForms MacAddress validator`_.
15+
This validator is based on [WTForms MacAddress validator][1].
1716
18-
.. _WTForms MacAddress validator:
19-
https://github.com/wtforms/wtforms/blob/master/wtforms/validators.py
20-
21-
Examples::
17+
[1]: https://github.com/wtforms/wtforms/blob/master/src/wtforms/validators.py#L482
2218
19+
Examples:
2320
>>> mac_address('01:23:45:67:ab:CD')
24-
True
25-
21+
# Output: True
2622
>>> mac_address('00:00:00:00:00')
27-
ValidationFailure(func=mac_address, args={'value': '00:00:00:00:00'})
23+
# Output: ValidationFailure(func=mac_address, args={'value': '00:00:00:00:00'})
24+
25+
Args:
26+
value:
27+
A string to validate.
2828
29-
.. versionadded:: 0.2
29+
Returns:
30+
(Literal[True]):
31+
If `value` is a valid MAC address.
32+
(ValidationFailure):
33+
If `value` is an invalid MAC address.
3034
31-
:param value: Mac address string to validate
35+
> *New in version 0.2.0*.
3236
"""
33-
return pattern.match(value)
37+
return re.match(r"^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$", value)

validators/slug.py

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,36 @@
1+
"""Slug."""
2+
# -*- coding: utf-8 -*-
3+
4+
# standard
15
import re
26

7+
# local
38
from .utils import validator
49

5-
slug_regex = re.compile(r'^[-a-zA-Z0-9_]+$')
6-
710

811
@validator
9-
def slug(value):
10-
"""
11-
Validate whether or not given value is valid slug.
12+
def slug(value: str):
13+
"""Validate whether or not given value is valid slug.
1214
13-
Valid slug can contain only alphanumeric characters, hyphens and
14-
underscores.
15-
16-
Examples::
15+
Valid slug can contain only lowercase alphanumeric characters and hyphens.
16+
It starts and ends with these lowercase alphanumeric characters.
1717
18+
Examples:
19+
>>> slug('my-slug-2134')
20+
# Output: True
1821
>>> slug('my.slug')
19-
ValidationFailure(func=slug, args={'value': 'my.slug'})
22+
# Output: ValidationFailure(func=slug, args={'value': 'my.slug'})
2023
21-
>>> slug('my-slug-2134')
22-
True
24+
Args:
25+
value:
26+
A string to validate.
2327
24-
.. versionadded:: 0.6
28+
Returns:
29+
(Literal[True]):
30+
If `value` is a valid slug.
31+
(ValidationFailure):
32+
If `value` is an invalid slug.
2533
26-
:param value: value to validate
34+
> *New in version 0.6.0*.
2735
"""
28-
return slug_regex.match(value)
36+
return re.match(r"^[a-z0-9]+(?:-[a-z0-9]+)*$", value)

validators/truthy.py

Lines changed: 0 additions & 39 deletions
This file was deleted.

0 commit comments

Comments
 (0)