Skip to content

Commit 0ea54dd

Browse files
authored
Validate non-string UUID types (#174)
* Validate invalid UUID types * Check for UUID instances * Fix import sorting
1 parent bea9e86 commit 0ea54dd

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

tests/test_uuid.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# -*- coding: utf-8 -*-
2+
from uuid import UUID
3+
24
import pytest
35

46
from validators import uuid, ValidationFailure
@@ -11,6 +13,13 @@ def test_returns_true_on_valid_mac_address(value):
1113
assert uuid(value)
1214

1315

16+
@pytest.mark.parametrize(('value',), [
17+
(UUID('2bc1c94f-0deb-43e9-92a1-4775189ec9f8'),),
18+
])
19+
def test_returns_true_on_valid_uuid_object(value):
20+
assert uuid(value)
21+
22+
1423
@pytest.mark.parametrize(('value',), [
1524
('2bc1c94f-deb-43e9-92a1-4775189ec9f8',),
1625
('2bc1c94f-0deb-43e9-92a1-4775189ec9f',),
@@ -19,3 +28,13 @@ def test_returns_true_on_valid_mac_address(value):
1928
])
2029
def test_returns_failed_validation_on_invalid_mac_address(value):
2130
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):
40+
assert isinstance(uuid(value), ValidationFailure)

validators/uuid.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
from __future__ import absolute_import
2+
13
import re
4+
from uuid import UUID
25

36
from .utils import validator
47

@@ -28,6 +31,11 @@ def uuid(value):
2831
2932
.. versionadded:: 0.2
3033
31-
:param value: UUID string to validate
34+
:param value: UUID value to validate
3235
"""
33-
return pattern.match(value)
36+
if isinstance(value, UUID):
37+
return True
38+
try:
39+
return pattern.match(value)
40+
except TypeError:
41+
return False

0 commit comments

Comments
 (0)