diff --git a/CHANGES.txt b/CHANGES.txt index 9debc6b..ce772d7 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -53,6 +53,9 @@ Features - The ``typ`` of a ``SchemaNode`` can optionally be pased in as a keyword argument. See https://github.com/Pylons/colander/issues/90 +- Add a ``missing_msg`` argument to ``SchemaNode`` that specifies the error + message to be used when the node is required and missing + 1.0a5 (2013-05-31) ------------------ diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index afe9392..a1cf78f 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -113,3 +113,4 @@ Contributors - Clayton Parker, 2013/08/15 - Brian Sutherland, 2013/08/16 - Peter Lamut, 2013/08/16 +- Veeti Paananen, 2013/08/20 diff --git a/colander/__init__.py b/colander/__init__.py index 00b3f16..15e70a7 100644 --- a/colander/__init__.py +++ b/colander/__init__.py @@ -1743,6 +1743,9 @@ class _SchemaNode(object): :attr:`colander.drop`, the node is dropped from the schema if it isn't set during serialization/deserialization. + - ``missing_msg``: Optional error message to be used if the value is + required and missing. + - ``preparer``: Optional preparer for this node. It should be an object that implements the :class:`colander.interfaces.Preparer` interface. @@ -1787,6 +1790,7 @@ class _SchemaNode(object): validator = None default = null missing = required + missing_msg = _('Required') name = '' raw_title = _marker title = '' @@ -1928,9 +1932,9 @@ class _SchemaNode(object): if appstruct is null: appstruct = self.missing if appstruct is required: - raise Invalid(self, _('Required')) + raise Invalid(self, self.missing_msg) if isinstance(appstruct, deferred): # unbound schema with deferreds - raise Invalid(self, _('Required')) + raise Invalid(self, self.missing_msg) # We never deserialize or validate the missing value return appstruct diff --git a/colander/tests/test_colander.py b/colander/tests/test_colander.py index b60de75..51d8266 100644 --- a/colander/tests/test_colander.py +++ b/colander/tests/test_colander.py @@ -2438,6 +2438,13 @@ class TestSchemaNode(unittest.TestCase): node.missing = 'abc' self.assertEqual(node.deserialize(null), 'abc') + def test_deserialize_value_is_null_with_missing_msg(self): + from colander import null + typ = DummyType() + node = self._makeOne(typ, missing_msg='Missing') + e = invalid_exc(node.deserialize, null) + self.assertEqual(e.msg, 'Missing') + def test_deserialize_noargs_uses_default(self): typ = DummyType() node = self._makeOne(typ)