Permalink
Browse files

Merge branch 'missing-drop' of github.com:jayd3e/colander into jayd3e…

…-missing-drop
  • Loading branch information...
2 parents 981cb38 + a564f43 commit 5b9c9ff56ace03f957d7eb81ab4527db2e30f32a @mcdonc mcdonc committed Mar 20, 2013
Showing with 30 additions and 2 deletions.
  1. +16 −2 colander/__init__.py
  2. +10 −0 colander/tests/test_colander.py
  3. +4 −0 docs/api.rst
View
18 colander/__init__.py
@@ -40,6 +40,15 @@ def __reduce__(self):
null = _null()
+class _drop(object):
+ """
+ Represents a value that should be dropped if it is missing during
+ deserialization.
+ """
+ pass
+
+drop = _drop()
+
def interpolate(msgs):
for s in msgs:
if hasattr(s, 'interpolate'):
@@ -532,11 +541,14 @@ def _impl(self, node, value, callback):
name = subnode.name
subval = value.pop(name, null)
try:
- result[name] = callback(subnode, subval)
+ sub_result = callback(subnode, subval)
except Invalid as e:
if error is None:
error = Invalid(node)
error.add(e, num)
+ else:
+ if sub_result is not drop:
+ result[name] = sub_result
if self.unknown == 'raise':
if value:
@@ -1641,7 +1653,9 @@ class _SchemaNode(object):
not provided, the missing value of this node will be the special marker
value :attr:`colander.required`, indicating that it is considered
'required'. When ``missing`` is :attr:`colander.required`, the
- ``required`` computed attribute will be ``True``.
+ ``required`` computed attribute will be ``True``. When ``missing`` is
+ :attr:`colander.drop`, the node is dropped from the schema if it isn't
+ set during serialization/deserialization.
- ``preparer``: Optional preparer for this node. It should be
an object that implements the
View
10 colander/tests/test_colander.py
@@ -2897,6 +2897,16 @@ class MySchema(colander.Schema):
self.assertEqual(node.children[2].title, '')
self.assertEqual(node.children[3].title, 'thing2')
+ def test_deserialize_drop(self):
+ import colander
+ class MySchema(colander.Schema):
+ a = colander.SchemaNode(colander.String())
+ b = colander.SchemaNode(colander.String(), missing=colander.drop)
+ node = MySchema()
+ expected = {'a': 'test'}
+ result = node.deserialize(expected)
+ self.assertEqual(result, expected)
+
class TestSequenceSchema(unittest.TestCase):
def test_succeed(self):
import colander
View
4 docs/api.rst
@@ -139,3 +139,7 @@ Schema-Related
.. attribute:: required
Represents a required value in colander-related operations.
+
+ .. attribute:: drop
+ Represents a value that will be dropped from the schema if it is missing. Passed
+ ass a value to the `missing` keyword argument.

0 comments on commit 5b9c9ff

Please sign in to comment.