Added an ANY_OF validator that allows a value to be valid according to any of a list of validators.

This commit is contained in:
mictee
2013-03-26 17:24:19 +01:00
parent b9189771d0
commit f98548827a
+33
View File
@@ -32,6 +32,7 @@ except ImportError:
JSONErrors += (JSONDecodeError,)
__all__ = [
'ANY_OF',
'CLEANUP',
'CRYPT',
'IS_ALPHANUMERIC',
@@ -2591,6 +2592,38 @@ class IS_SLUG(Validator):
return (urlify(value, self.maxlen, self.keep_underscores), None)
class ANY_OF(Validator):
"""
test if any of the validators in a list return successfully
>>> ANY_OF([IS_EMAIL(),IS_ALPHANUMERIC()])('a@b.co')
('a@b.co', None)
>>> ANY_OF([IS_EMAIL(),IS_ALPHANUMERIC()])('abco')
('abco', None)
>>> ANY_OF([IS_EMAIL(),IS_ALPHANUMERIC()])('@ab.co')
('@ab.co', 'enter only letters, numbers, and underscore')
>>> ANY_OF([IS_ALPHANUMERIC(),IS_EMAIL()])('@ab.co')
('@ab.co', 'enter a valid email address')
"""
def __init__(self, subs):
self.subs = subs
def __call__(self, value):
for validator in self.subs:
value, error = validator(value)
if error == None:
break
return value, error
def formatter(self, value):
# Use the formatter of the first subvalidator
# that validates the value and has a formatter
for validator in self.subs:
if hasattr(validator, 'formatter') and validator(value)[1] != None:
return validator.formatter(value)
class IS_EMPTY_OR(Validator):
"""
dummy class for testing IS_EMPTY_OR