Add a custom error message argument for missing schema nodes

missing_msg lets the developer specify a custom message in place of the
default "Required" if the node is missing.
This commit is contained in:
Veeti Paananen
2013-08-15 06:51:05 +03:00
parent 6dc5b46d98
commit 18f18c0741
3 changed files with 16 additions and 2 deletions

View File

@@ -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)
------------------

View File

@@ -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

View File

@@ -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)