More test coverage for validators.py

Fixed a bug in IS_EMAIL throwing exceptions when asked to validate anything other than a string which was problematic for ANY_OF
Fixed a bug in ANY_OF.formatter where it was trying to format with a validator that didn't validate
This commit is contained in:
Leonel Câmara
2016-03-27 14:23:24 +01:00
parent a0bbd7885a
commit 5cbf381a2c
2 changed files with 39 additions and 2 deletions
+7 -2
View File
@@ -1200,7 +1200,12 @@ class IS_EMAIL(Validator):
self.error_message = error_message
def __call__(self, value):
match = self.regex.match(value)
try:
match = self.regex.match(value)
except TypeError:
# Value may not be a string where we can look for matches.
# Example: we're calling ANY_OF formatter and IS_EMAIL is asked to validate a date.
match = None
if match:
domain = value.split('@')[1]
if (not self.banned or not self.banned.match(domain)) \
@@ -2608,7 +2613,7 @@ class ANY_OF(Validator):
# 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:
if hasattr(validator, 'formatter') and validator(value)[1] is None:
return validator.formatter(value)