diff options
| author | Bobby <[email protected]> | 2022-11-27 12:12:37 -0500 |
|---|---|---|
| committer | Bobby <[email protected]> | 2022-11-27 12:12:37 -0500 |
| commit | 2076d462ed8c38a1202fd5ca5b20f0e07accbbee (patch) | |
| tree | fd1143b1f69964193eed29e9ad84700060b34793 | |
| parent | f5cb8a4d14c48f458039d608f6bd38bea13a881c (diff) | |
| download | edify-2076d462ed8c38a1202fd5ca5b20f0e07accbbee.tar.xz edify-2076d462ed8c38a1202fd5ca5b20f0e07accbbee.zip | |
added uuid validator
| -rw-r--r-- | docs/built-in/index.rst | 22 | ||||
| -rw-r--r-- | src/edify/library/__init__.py | 1 | ||||
| -rw-r--r-- | src/edify/library/uuid.py | 13 | ||||
| -rw-r--r-- | tests/test_uuid.py | 12 |
4 files changed, 48 insertions, 0 deletions
diff --git a/docs/built-in/index.rst b/docs/built-in/index.rst index c99ed40..5025195 100644 --- a/docs/built-in/index.rst +++ b/docs/built-in/index.rst @@ -169,3 +169,25 @@ By default, the ``url`` function matches both URLs with and without a protocol. url('https://example.com', match=['no_proto']) # returns False If you supply an Invalid or empty value in the ``match`` list argument, the function will raise a ``ValueError`` exception. Similarly, if you supply another data type in the ``match`` list argument, the function will raise a ``TypeError`` exception. + +.. code-block:: python + + from edify.library import url + + url('example.com', match=['invalid']) # raises ValueError + url('example.com', match=['no_proto', 'invalid']) # raises ValueError + url('example.com', match=['no_proto', 1]) # raises TypeError + +uuid(uuid: str) +--------------- + +The ``uuid`` function verifies that a string is a valid UUID. The function takes a ``string`` argument which is supposed to be a valid UUID. The function returns ``True`` if the string is a valid UUID, and ``False`` otherwise. + +You can use the ``uuid`` function as follows: + +.. code-block:: python + + from edify.library import uuid + + uuid('123e4567-e89b-12d3-a456-426655440000') # returns True + uuid('123e4567-e') # returns False diff --git a/src/edify/library/__init__.py b/src/edify/library/__init__.py index 16c5651..f2827ea 100644 --- a/src/edify/library/__init__.py +++ b/src/edify/library/__init__.py @@ -9,3 +9,4 @@ from .mail import email from .mail import email_rfc_5322 from .phone import phone_number from .url import url +from .uuid import uuid diff --git a/src/edify/library/uuid.py b/src/edify/library/uuid.py new file mode 100644 index 0000000..a6f2f65 --- /dev/null +++ b/src/edify/library/uuid.py @@ -0,0 +1,13 @@ +import re + +pattern = "^[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}$" + +def uuid(uuid: str) -> bool: + """Checks if a string is a valid UUID. + + Args: + uuid (str): The string to check. + Returns: + bool: True if the string is a valid UUID, False otherwise. + """ + return re.match(pattern, uuid) is not None diff --git a/tests/test_uuid.py b/tests/test_uuid.py new file mode 100644 index 0000000..6c37b17 --- /dev/null +++ b/tests/test_uuid.py @@ -0,0 +1,12 @@ +from edify.library import uuid + +uuids = { + "123e4567-e89b-12d3-a456-426614174000": True, + "123e456-789b-12d3-426614174000": False, + "123e456-789b-12d3-a456-426614174000-12ad3r": False, + "123e456": False, +} + +def test_valid_uuids(): + for uuid_string, expectation in uuids.items(): + assert uuid(uuid_string) == expectation |
