Permalink
Browse files

specify optional fields with "missing=drop"

  • Loading branch information...
1 parent ff6d8c2 commit 21f525959fcc7c47ba063c28c9e3355d89cb9ae1 @abrookins abrookins committed Feb 20, 2013
Showing with 23 additions and 1 deletion.
  1. +13 −1 colander/__init__.py
  2. +10 −0 colander/tests/test_colander.py
View
14 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'):
@@ -531,11 +540,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:
View
10 colander/tests/test_colander.py
@@ -2895,6 +2895,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

0 comments on commit 21f5259

Please sign in to comment.