diff --git a/docs/index.md b/docs/index.md index 5b0dbd44..ae76ef2a 100644 --- a/docs/index.md +++ b/docs/index.md @@ -1,7 +1,5 @@ # Reference -::: validators.utils - ::: validators.between ::: validators.btc_address @@ -16,4 +14,18 @@ ::: validators.iban + + ::: validators.length + +::: validators.mac_address + +::: validators.slug + + + +::: validators.uuid + +--- + +::: validators.utils diff --git a/tests/test_mac_address.py b/tests/test_mac_address.py index 756fa3ec..81025f67 100644 --- a/tests/test_mac_address.py +++ b/tests/test_mac_address.py @@ -1,21 +1,36 @@ +"""MAC Address.""" # -*- coding: utf-8 -*- + +# external import pytest +# local from validators import mac_address, ValidationFailure -@pytest.mark.parametrize(('address',), [ - ('01:23:45:67:ab:CD',), -]) -def test_returns_true_on_valid_mac_address(address): +@pytest.mark.parametrize( + ("address",), + [ + ("01:23:45:67:ab:CD",), + ("01-23-45-67-ab-CD",), + ("01:2F:45:37:ab:CD",), + ("A1-2F-4E-68-ab-CD",), + ], +) +def test_returns_true_on_valid_mac_address(address: str): + """Test returns true on valid mac address.""" assert mac_address(address) -@pytest.mark.parametrize(('address',), [ - ('00:00:00:00:00',), - ('01:23:45:67:89:',), - ('01:23:45:67:89:gh',), - ('123:23:45:67:89:00',), -]) -def test_returns_failed_validation_on_invalid_mac_address(address): +@pytest.mark.parametrize( + ("address",), + [ + ("00-00:-00-00-00",), + ("01:23:45:67:89:",), + ("01:23-45:67-89:gh",), + ("123:23:45:67:89:00",), + ], +) +def test_returns_failed_validation_on_invalid_mac_address(address: str): + """Test returns failed validation on invalid mac address.""" assert isinstance(mac_address(address), ValidationFailure) diff --git a/tests/test_slug.py b/tests/test_slug.py index a42fe5f7..7f699a42 100644 --- a/tests/test_slug.py +++ b/tests/test_slug.py @@ -1,23 +1,36 @@ +"""Test Slug.""" # -*- coding: utf-8 -*- + +# external import pytest +# local from validators import slug, ValidationFailure -@pytest.mark.parametrize('value', [ - '123-12312-asdasda', - '123____123', - 'dsadasd-dsadas', -]) -def test_returns_true_on_valid_slug(value): +@pytest.mark.parametrize( + "value", + [ + "123-asd-7sda", + "123-k-123", + "dac-12sa-459", + "dac-12sa7-ad31as", + ], +) +def test_returns_true_on_valid_slug(value: str): + """Test returns true on valid slug.""" assert slug(value) -@pytest.mark.parametrize('value', [ - 'some.slug', - '1231321%', - ' 21312', - '123asda&', -]) -def test_returns_failed_validation_on_invalid_slug(value): +@pytest.mark.parametrize( + "value", + [ + "some.slug&", + "1231321%", + " 21312", + "-47q-p--123", + ], +) +def test_returns_failed_validation_on_invalid_slug(value: str): + """Test returns failed validation on invalid slug.""" assert isinstance(slug(value), ValidationFailure) diff --git a/tests/test_uuid.py b/tests/test_uuid.py index bb638a65..a726d83c 100644 --- a/tests/test_uuid.py +++ b/tests/test_uuid.py @@ -1,40 +1,40 @@ +"""Test UUIDs.""" # -*- coding: utf-8 -*- -from uuid import UUID +# standard +from uuid import uuid4, UUID +from typing import Union + +# external import pytest +# local from validators import uuid, ValidationFailure -@pytest.mark.parametrize(('value',), [ - ('2bc1c94f-0deb-43e9-92a1-4775189ec9f8',), -]) -def test_returns_true_on_valid_mac_address(value): - assert uuid(value) - - -@pytest.mark.parametrize(('value',), [ - (UUID('2bc1c94f-0deb-43e9-92a1-4775189ec9f8'),), -]) -def test_returns_true_on_valid_uuid_object(value): +@pytest.mark.parametrize( + ("value",), + [ + (uuid4(),), + ("2bc1c94f-0deb-43e9-92a1-4775189ec9f8",), + (uuid4(),), + ("888256d7c49341f19fa33f29d3f820d7",), + ], +) +def test_returns_true_on_valid_uuid(value: Union[str, UUID]): + """Test returns true on valid uuid.""" assert uuid(value) -@pytest.mark.parametrize(('value',), [ - ('2bc1c94f-deb-43e9-92a1-4775189ec9f8',), - ('2bc1c94f-0deb-43e9-92a1-4775189ec9f',), - ('gbc1c94f-0deb-43e9-92a1-4775189ec9f8',), - ('2bc1c94f 0deb-43e9-92a1-4775189ec9f8',), -]) -def test_returns_failed_validation_on_invalid_mac_address(value): - assert isinstance(uuid(value), ValidationFailure) - - -@pytest.mark.parametrize(('value',), [ - (1,), - (1.0,), - (True,), - (None,), -]) -def test_returns_failed_validation_on_invalid_types(value): +@pytest.mark.parametrize( + ("value",), + [ + ("2bc1c94f-deb-43e9-92a1-4775189ec9f8",), + ("2bc1c94f-0deb-43e9-92a1-4775189ec9f",), + ("gbc1c94f-0deb-43e9-92a1-4775189ec9f8",), + ("2bc1c94f 0deb-43e9-92a1-4775189ec9f8",), + ], +) +def test_returns_failed_validation_on_invalid_uuid(value: Union[str, UUID]): + """Test returns failed validation on invalid uuid.""" assert isinstance(uuid(value), ValidationFailure) diff --git a/validators/__init__.py b/validators/__init__.py index 7c94d283..f837445f 100644 --- a/validators/__init__.py +++ b/validators/__init__.py @@ -13,7 +13,6 @@ from .length import length from .mac_address import mac_address from .slug import slug -from .truthy import truthy from .url import url from .utils import ValidationFailure, validator from .uuid import uuid @@ -44,7 +43,6 @@ "sha256", "sha512", "slug", - "truthy", "unionpay", "url", "uuid", diff --git a/validators/mac_address.py b/validators/mac_address.py index bdb19947..dc4110da 100644 --- a/validators/mac_address.py +++ b/validators/mac_address.py @@ -1,33 +1,37 @@ +"""MAC Address.""" +# -*- coding: utf-8 -*- + +# standard import re +# local from .utils import validator -pattern = re.compile(r'^(?:[0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$') - @validator -def mac_address(value): - """ - Return whether or not given value is a valid MAC address. - - If the value is valid MAC address this function returns ``True``, - otherwise :class:`~validators.utils.ValidationFailure`. +def mac_address(value: str): + """Return whether or not given value is a valid MAC address. - This validator is based on `WTForms MacAddress validator`_. + This validator is based on [WTForms MacAddress validator][1]. - .. _WTForms MacAddress validator: - https://github.com/wtforms/wtforms/blob/master/wtforms/validators.py - - Examples:: + [1]: https://github.com/wtforms/wtforms/blob/master/src/wtforms/validators.py#L482 + Examples: >>> mac_address('01:23:45:67:ab:CD') - True - + # Output: True >>> mac_address('00:00:00:00:00') - ValidationFailure(func=mac_address, args={'value': '00:00:00:00:00'}) + # Output: ValidationFailure(func=mac_address, args={'value': '00:00:00:00:00'}) + + Args: + value: + A string to validate. - .. versionadded:: 0.2 + Returns: + (Literal[True]): + If `value` is a valid MAC address. + (ValidationFailure): + If `value` is an invalid MAC address. - :param value: Mac address string to validate + > *New in version 0.2.0*. """ - return pattern.match(value) + return re.match(r"^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$", value) diff --git a/validators/slug.py b/validators/slug.py index 83bfd4b1..d6ca498a 100644 --- a/validators/slug.py +++ b/validators/slug.py @@ -1,28 +1,36 @@ +"""Slug.""" +# -*- coding: utf-8 -*- + +# standard import re +# local from .utils import validator -slug_regex = re.compile(r'^[-a-zA-Z0-9_]+$') - @validator -def slug(value): - """ - Validate whether or not given value is valid slug. +def slug(value: str): + """Validate whether or not given value is valid slug. - Valid slug can contain only alphanumeric characters, hyphens and - underscores. - - Examples:: + Valid slug can contain only lowercase alphanumeric characters and hyphens. + It starts and ends with these lowercase alphanumeric characters. + Examples: + >>> slug('my-slug-2134') + # Output: True >>> slug('my.slug') - ValidationFailure(func=slug, args={'value': 'my.slug'}) + # Output: ValidationFailure(func=slug, args={'value': 'my.slug'}) - >>> slug('my-slug-2134') - True + Args: + value: + A string to validate. - .. versionadded:: 0.6 + Returns: + (Literal[True]): + If `value` is a valid slug. + (ValidationFailure): + If `value` is an invalid slug. - :param value: value to validate + > *New in version 0.6.0*. """ - return slug_regex.match(value) + return re.match(r"^[a-z0-9]+(?:-[a-z0-9]+)*$", value) diff --git a/validators/truthy.py b/validators/truthy.py deleted file mode 100644 index 517149aa..00000000 --- a/validators/truthy.py +++ /dev/null @@ -1,39 +0,0 @@ -from .utils import validator - - -@validator -def truthy(value): - """ - Validate that given value is not a falsey value. - - This validator is based on `WTForms DataRequired validator`_. - - .. _WTForms DataRequired validator: - https://github.com/wtforms/wtforms/blob/master/wtforms/validators.py - - Examples:: - - >>> truthy(1) - True - - >>> truthy('someone') - True - - >>> truthy(0) - ValidationFailure(func=truthy, args={'value': 0}) - - >>> truthy(' ') - ValidationFailure(func=truthy, args={'value': ' '}) - - >>> truthy(False) - ValidationFailure(func=truthy, args={'value': False}) - - >>> truthy(None) - ValidationFailure(func=truthy, args={'value': None}) - - .. versionadded:: 0.2 - """ - return ( - value and - (not isinstance(value, str) or value.strip()) - ) diff --git a/validators/uuid.py b/validators/uuid.py index 20080088..6cc8fade 100644 --- a/validators/uuid.py +++ b/validators/uuid.py @@ -1,41 +1,45 @@ -from __future__ import absolute_import +"""UUID.""" -import re +# standard +from typing import Union from uuid import UUID +import re +# local from .utils import validator -pattern = re.compile(r'^[0-9a-fA-F]{8}-([0-9a-fA-F]{4}-){3}[0-9a-fA-F]{12}$') - @validator -def uuid(value): - """ - Return whether or not given value is a valid UUID. +def uuid(value: Union[str, UUID]): + """Return whether or not given value is a valid UUID-v4 string. - If the value is valid UUID this function returns ``True``, otherwise - :class:`~validators.utils.ValidationFailure`. + This validator is based on [WTForms UUID validator][1]. - This validator is based on `WTForms UUID validator`_. - - .. _WTForms UUID validator: - https://github.com/wtforms/wtforms/blob/master/wtforms/validators.py - - Examples:: + [1]: https://github.com/wtforms/wtforms/blob/master/src/wtforms/validators.py#L539 + Examples: >>> uuid('2bc1c94f-0deb-43e9-92a1-4775189ec9f8') - True - + # Output: True >>> uuid('2bc1c94f 0deb-43e9-92a1-4775189ec9f8') - ValidationFailure(func=uuid, ...) + # Output: ValidationFailure(func=uuid, ...) + + Args: + value: + A string or UUID object to validate. - .. versionadded:: 0.2 + Returns: + (Literal[True]): + If `value` is a valid UUID. + (ValidationFailure): + If `value` is an invalid UUID. - :param value: UUID value to validate + > *New in version 0.2.0*. """ if isinstance(value, UUID): return True try: - return pattern.match(value) - except TypeError: + return UUID(value) or re.match( + r"^[0-9a-fA-F]{8}-([0-9a-fA-F]{4}-){3}[0-9a-fA-F]{12}$", value + ) + except ValueError: return False