Merge pull request #123 from veeti/missing_msg
Add a custom error message argument for missing schema nodes
This commit is contained in:
@@ -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)
|
||||
------------------
|
||||
|
||||
|
@@ -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
|
||||
|
@@ -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
|
||||
|
||||
|
@@ -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)
|
||||
|
Reference in New Issue
Block a user