Merge pull request #96 from jayd3e/dict-is-too-liberal
Stricter checking for what is allowed as a Mapping object.
This commit is contained in:
@@ -510,7 +510,10 @@ class Mapping(SchemaType):
|
|||||||
|
|
||||||
def _validate(self, node, value):
|
def _validate(self, node, value):
|
||||||
try:
|
try:
|
||||||
return dict(value)
|
if hasattr(value, 'items'):
|
||||||
|
return dict(value)
|
||||||
|
else:
|
||||||
|
raise TypeError('Does not implement dict-like functionality.')
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise Invalid(node,
|
raise Invalid(node,
|
||||||
_('"${val}" is not a mapping type: ${err}',
|
_('"${val}" is not a mapping type: ${err}',
|
||||||
|
@@ -529,10 +529,27 @@ class TestMapping(unittest.TestCase):
|
|||||||
def test_deserialize_not_a_mapping(self):
|
def test_deserialize_not_a_mapping(self):
|
||||||
node = DummySchemaNode(None)
|
node = DummySchemaNode(None)
|
||||||
typ = self._makeOne()
|
typ = self._makeOne()
|
||||||
|
|
||||||
|
# None
|
||||||
e = invalid_exc(typ.deserialize, node, None)
|
e = invalid_exc(typ.deserialize, node, None)
|
||||||
self.assertTrue(
|
self.assertTrue(
|
||||||
e.msg.interpolate().startswith('"None" is not a mapping type'))
|
e.msg.interpolate().startswith('"None" is not a mapping type'))
|
||||||
|
|
||||||
|
# list
|
||||||
|
e = invalid_exc(typ.deserialize, node, [])
|
||||||
|
self.assertTrue(
|
||||||
|
e.msg.interpolate().startswith('"[]" is not a mapping type'))
|
||||||
|
|
||||||
|
# str
|
||||||
|
e = invalid_exc(typ.deserialize, node, "")
|
||||||
|
self.assertTrue(
|
||||||
|
e.msg.interpolate().startswith('"" is not a mapping type'))
|
||||||
|
|
||||||
|
# tuple
|
||||||
|
e = invalid_exc(typ.deserialize, node, ())
|
||||||
|
self.assertTrue(
|
||||||
|
e.msg.interpolate().startswith('"()" is not a mapping type'))
|
||||||
|
|
||||||
def test_deserialize_null(self):
|
def test_deserialize_null(self):
|
||||||
import colander
|
import colander
|
||||||
node = DummySchemaNode(None)
|
node = DummySchemaNode(None)
|
||||||
|
Reference in New Issue
Block a user