Patch: new validator that validates one of many validators
Sometimes one needs to support different formats in a field and for each format there is already a validator. This validator would allow to just say that one of many validators is sufficient for correct data:
class ANY:
"""
This validator tries all validators. If any one of them succeeds,
the validator returns. If none of them succeeds, the given message
is thrown as a validation error. The message is rather unspecific,
so it's best to give a specific one on instantiation. always_test
is propagated from the enclosed validators by setting it if any one
of them contains a always_test attribute.
"""
def __init__(self, validator_list=[], error_message="This field should conform to one of several formats"):
self.validator_list = validator_list
self.error_message = error_message
for v in validator_list:
if hasattr(v, 'always_test'):
self.always_test = True
def __call__(self, field_data, all_data):
for v in self.validator_list:
try:
v(field_data, all_data)
return
except validators.ValidationError, e:
pass
raise validators.ValidationError(self.error_message)
Change History
(2)
Resolution: |
→ fixed
|
Status: |
assigned → closed
|
(In [402]) Fixed #268 -- Added AnyValidator and fixed small bug in [399]. Thanks, Hugo