Merge branch 'extra-value-exc' of surabujin/colander into pr/241

Renamed UnknownItemsError to UnsupportedFields.
This commit is contained in:
Michael Merickel
2016-01-16 15:29:04 -06:00
5 changed files with 31 additions and 5 deletions

View File

@@ -4,6 +4,9 @@ unreleased
Features Features
-------- --------
- Add new exception ExtraItemsError. Used to pass to the caller a list of extra
field detected in cstruct.
- Add ``drop`` functionality to ``Sequence`` type. - Add ``drop`` functionality to ``Sequence`` type.
Bug Fixes Bug Fixes

View File

@@ -126,4 +126,5 @@ Contributors
- Nando Florestan, 2014/11/27 - Nando Florestan, 2014/11/27
- Amos Latteier, 2014/11/30 - Amos Latteier, 2014/11/30
- Jimmy Thrasibule, 2014/12/11 - Jimmy Thrasibule, 2014/12/11
- Dmitry Bogun, 2015/09/10
- Tinne Cahy, 2015/12/22 - Tinne Cahy, 2015/12/22

View File

@@ -205,6 +205,18 @@ class Invalid(Exception):
result of an execution of this exception's ``asdict`` method""" result of an execution of this exception's ``asdict`` method"""
return pprint.pformat(self.asdict()) return pprint.pformat(self.asdict())
class UnsupportedFields(Invalid):
"""
Exception used when schema object detect unknown fields in the
cstruct during deserialize.
"""
def __init__(self, node, items, msg=None):
super(UnsupportedFields, self).__init__(node, msg)
self.items = items
class All(object): class All(object):
""" Composite validator which succeeds if none of its """ Composite validator which succeeds if none of its
subvalidators raises an :class:`colander.Invalid` exception""" subvalidators raises an :class:`colander.Invalid` exception"""
@@ -667,11 +679,10 @@ class Mapping(SchemaType):
if self.unknown == 'raise': if self.unknown == 'raise':
if value: if value:
raise Invalid( raise UnsupportedFields(
node, node, value,
_('Unrecognized keys in mapping: "${val}"', msg=_('Unrecognized keys in mapping: "${val}"',
mapping={'val':value}) mapping={'val': value}))
)
elif self.unknown == 'preserve': elif self.unknown == 'preserve':
result.update(value) result.update(value)

View File

@@ -718,10 +718,12 @@ class TestMapping(unittest.TestCase):
self.assertEqual(result, {'a':1}) self.assertEqual(result, {'a':1})
def test_deserialize_unknown_raise(self): def test_deserialize_unknown_raise(self):
import colander
node = DummySchemaNode(None) node = DummySchemaNode(None)
node.children = [DummySchemaNode(None, name='a')] node.children = [DummySchemaNode(None, name='a')]
typ = self._makeOne(unknown='raise') typ = self._makeOne(unknown='raise')
e = invalid_exc(typ.deserialize, node, {'a':1, 'b':2}) e = invalid_exc(typ.deserialize, node, {'a':1, 'b':2})
self.assertTrue(isinstance(e, colander.UnsupportedFields))
self.assertEqual(e.msg.interpolate(), self.assertEqual(e.msg.interpolate(),
"Unrecognized keys in mapping: \"{'b': 2}\"") "Unrecognized keys in mapping: \"{'b': 2}\"")

View File

@@ -48,6 +48,15 @@ Exceptions
from a widget as the value which should be redisplayed when an from a widget as the value which should be redisplayed when an
error is shown. error is shown.
.. autoclass:: UnsupportedFields
.. attribute:: fields
The ``dict`` with all detected extra fields and their values.
Nodes that contain extra fields can be located by the position of
this exception in the exception tree hierarchy.
.. autoclass:: UnboundDeferredError .. autoclass:: UnboundDeferredError