diff --git a/CHANGES.rst b/CHANGES.rst index 0225840..3c71cb2 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -4,6 +4,9 @@ unreleased 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. Bug Fixes diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index c3fdf73..2d2b979 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -126,4 +126,5 @@ Contributors - Nando Florestan, 2014/11/27 - Amos Latteier, 2014/11/30 - Jimmy Thrasibule, 2014/12/11 +- Dmitry Bogun, 2015/09/10 - Tinne Cahy, 2015/12/22 diff --git a/colander/__init__.py b/colander/__init__.py index d4b00ba..dae7f44 100644 --- a/colander/__init__.py +++ b/colander/__init__.py @@ -205,6 +205,18 @@ class Invalid(Exception): result of an execution of this exception's ``asdict`` method""" 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): """ Composite validator which succeeds if none of its subvalidators raises an :class:`colander.Invalid` exception""" @@ -667,11 +679,10 @@ class Mapping(SchemaType): if self.unknown == 'raise': if value: - raise Invalid( - node, - _('Unrecognized keys in mapping: "${val}"', - mapping={'val':value}) - ) + raise UnsupportedFields( + node, value, + msg=_('Unrecognized keys in mapping: "${val}"', + mapping={'val': value})) elif self.unknown == 'preserve': result.update(value) diff --git a/colander/tests/test_colander.py b/colander/tests/test_colander.py index 1317747..88b90e5 100644 --- a/colander/tests/test_colander.py +++ b/colander/tests/test_colander.py @@ -718,10 +718,12 @@ class TestMapping(unittest.TestCase): self.assertEqual(result, {'a':1}) def test_deserialize_unknown_raise(self): + import colander node = DummySchemaNode(None) node.children = [DummySchemaNode(None, name='a')] typ = self._makeOne(unknown='raise') e = invalid_exc(typ.deserialize, node, {'a':1, 'b':2}) + self.assertTrue(isinstance(e, colander.UnsupportedFields)) self.assertEqual(e.msg.interpolate(), "Unrecognized keys in mapping: \"{'b': 2}\"") diff --git a/docs/api.rst b/docs/api.rst index 3ecc9cc..872311e 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -48,6 +48,15 @@ Exceptions from a widget as the value which should be redisplayed when an 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