Make deserialize with an unbound validator raise an exception.
This should help avoid programming mistakes where you forget to bind a schema.
This commit is contained in:
@@ -57,6 +57,13 @@ def interpolate(msgs):
|
||||
else:
|
||||
yield s
|
||||
|
||||
class UnboundDeferredError(Exception):
|
||||
"""
|
||||
An exception raised by :meth:`SchemaNode.deserialize` when an attempt
|
||||
is made to deserialize a node which has an unbound :class:`deferred`
|
||||
validator.
|
||||
"""
|
||||
|
||||
class Invalid(Exception):
|
||||
"""
|
||||
An exception raised by data types and validators indicating that
|
||||
@@ -1961,7 +1968,11 @@ class _SchemaNode(object):
|
||||
return appstruct
|
||||
|
||||
if self.validator is not None:
|
||||
if not isinstance(self.validator, deferred): # unbound
|
||||
if isinstance(self.validator, deferred): # unbound
|
||||
raise UnboundDeferredError(
|
||||
"Schema node {node} has an unbound deferred validator"
|
||||
.format(node=self))
|
||||
else:
|
||||
self.validator(self, appstruct)
|
||||
return appstruct
|
||||
|
||||
|
||||
@@ -2462,6 +2462,19 @@ class TestSchemaNode(unittest.TestCase):
|
||||
e = invalid_exc(node.deserialize, 1)
|
||||
self.assertEqual(e.msg, 'Wrong')
|
||||
|
||||
def test_deserialize_with_unbound_validator(self):
|
||||
from colander import Invalid
|
||||
from colander import deferred
|
||||
from colander import UnboundDeferredError
|
||||
typ = DummyType()
|
||||
def validator(node, kw):
|
||||
def _validate(node, value):
|
||||
node.raise_invalid('Invalid')
|
||||
return _validate
|
||||
node = self._makeOne(typ, validator=deferred(validator))
|
||||
self.assertRaises(UnboundDeferredError, node.deserialize, None)
|
||||
self.assertRaises(Invalid, node.bind(foo='foo').deserialize, None)
|
||||
|
||||
def test_deserialize_value_is_null_no_missing(self):
|
||||
from colander import null
|
||||
from colander import Invalid
|
||||
|
||||
Reference in New Issue
Block a user