Add a UUID string validator

The `uuid` validator uses a regex validator to match on all UUID
strings allowed by the `uuid.UUID` class.
This commit is contained in:
Jimmy Thrasibule
2014-12-11 10:58:21 +01:00
parent 24c5f03bd0
commit 048b76c143

View File

@@ -300,9 +300,9 @@ class Regex(object):
validation succeeds; otherwise, :exc:`colander.Invalid` is
raised with the ``msg`` error message.
"""
def __init__(self, regex, msg=None):
def __init__(self, regex, msg=None, flags=0):
if isinstance(regex, string_types):
self.match_object = re.compile(regex)
self.match_object = re.compile(regex, flags)
else:
self.match_object = regex
if msg is None:
@@ -465,6 +465,11 @@ URL_REGEX = r"""(?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9
url = Regex(URL_REGEX, _('Must be a URL'))
UUID_REGEX = r"""^(?:urn:uuid:)?\{?[a-f0-9]{8}(?:-?[a-f0-9]{4}){3}-?[a-f0-9]{12}\}?$"""
uuid = Regex(UUID_REGEX, _('Invalid UUID string'), 2) # 2 = re.IGNORECASE
class SchemaType(object):
""" Base class for all schema types """
def flatten(self, node, appstruct, prefix='', listitem=False):