Added deferred validators support to colander.All validator #70

Open
wants to merge 2 commits into from
View
1 .gitignore
@@ -21,3 +21,4 @@ colander/coverage.xml
distribute*.gz
venv/
env*/
+.idea
View
12 colander/__init__.py
@@ -193,6 +193,16 @@ def __call__(self, node, value):
exc.children.extend(e.children)
raise exc
+ def _bind(self, node, kw):
+ bound = []
+ for validator in self.validators:
+ if isinstance(validator, deferred):
+ bound.append(validator(node, kw))
+ else:
+ bound.append(validator)
+ self.validators = bound
+
+
class Function(object):
""" Validator which accepts a function and an optional message;
the function is called with the ``value`` during validation.
@@ -1834,6 +1844,8 @@ def _bind(self, kw):
if isinstance(v, deferred):
v = v(self, kw)
setattr(self, k, v)
+ if isinstance(self.validator, All):
+ self.validator._bind(self, kw)
if getattr(self, 'after_bind', None):
self.after_bind(self, kw)
View
20 colander/tests/test_colander.py
@@ -2144,6 +2144,21 @@ def test_deserialize_with_validator(self):
e = invalid_exc(node.deserialize, 1)
self.assertEqual(e.msg, 'Wrong')
+ def test_deserialize_deferred_validator(self):
+ typ = DummyType()
+ node = self._makeOne(typ, validator=DummyDeferredValidator)
+ node = node.bind(message='good')
+ e = invalid_exc(node.deserialize, 1)
+ self.assertEqual(e.msg, 'good')
+
+ def test_deserialize_all_with_deferred_validator(self):
+ from colander import All
+ typ = DummyType()
+ node = self._makeOne(typ, validator=All(DummyDeferredValidator))
+ node = node.bind(message='good')
+ e = invalid_exc(node.deserialize, 1)
+ self.assertEqual(e.msg, ['good'])
+
def test_deserialize_value_is_null_no_missing(self):
from colander import null
from colander import Invalid
@@ -3265,6 +3280,11 @@ def __call__(self, node, value):
self.children and e.children.extend(self.children)
raise e
+from colander import deferred
+@deferred
+def DummyDeferredValidator(node, kw):
+ return DummyValidator(kw.get('message','Deferred'))
+
class Uncooperative(object):
def __str__(self):
raise ValueError('I wont cooperate')