From 048b76c143d9888d0e7fae45f0f685f53e58c73b Mon Sep 17 00:00:00 2001 From: Jimmy Thrasibule Date: Thu, 11 Dec 2014 10:58:21 +0100 Subject: [PATCH] Add a UUID string validator The `uuid` validator uses a regex validator to match on all UUID strings allowed by the `uuid.UUID` class. --- colander/__init__.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/colander/__init__.py b/colander/__init__.py index be973bb..235503a 100644 --- a/colander/__init__.py +++ b/colander/__init__.py @@ -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):