FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Validate non-string UUID types by Letsch22 · Pull Request #174 · python-validators/validators · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
19 changes: 19 additions & 0 deletions tests/test_uuid.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# -*- coding: utf-8 -*-
from uuid import UUID

import pytest

from validators import uuid, ValidationFailure
Expand All @@ -11,6 +13,13 @@ 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):
assert uuid(value)


@pytest.mark.parametrize(('value',), [
('2bc1c94f-deb-43e9-92a1-4775189ec9f8',),
('2bc1c94f-0deb-43e9-92a1-4775189ec9f',),
Expand All @@ -19,3 +28,13 @@ def test_returns_true_on_valid_mac_address(value):
])
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):
assert isinstance(uuid(value), ValidationFailure)
12 changes: 10 additions & 2 deletions validators/uuid.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from __future__ import absolute_import

import re
from uuid import UUID

from .utils import validator

Expand Down Expand Up @@ -28,6 +31,11 @@ def uuid(value):
.. versionadded:: 0.2
:param value: UUID string to validate
:param value: UUID value to validate
"""
return pattern.match(value)
if isinstance(value, UUID):
return True
try:
return pattern.match(value)
except TypeError:
return False

Back | FazBrowse Home | New Git URL