Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add drop support to Sequence #225

Merged
merged 3 commits into from
Jan 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ Features
- Add a ``missing_msg`` argument to ``SchemaNode`` that specifies the error
message to be used when the node is required and missing

- Add ``drop`` functionality to ``Sequence`` type.

Bug Fixes
---------

Expand Down
9 changes: 8 additions & 1 deletion colander/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -987,13 +987,20 @@ def _impl(self, node, value, callback, accept_scalar):
error = None
result = []

subnode = node.children[0]
for num, subval in enumerate(value):
if subval is drop or (subval is null and subnode.default is drop):
continue
try:
result.append(callback(node.children[0], 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 drop:
continue
result.append(sub_result)

if error is not None:
raise error
Expand Down
26 changes: 26 additions & 0 deletions colander/tests/test_colander.py
Original file line number Diff line number Diff line change
Expand Up @@ -1320,6 +1320,14 @@ def test_serialize_null(self):
result = typ.serialize(node, colander.null)
self.assertEqual(result, colander.null)

def test_serialize_drop(self):
from colander import drop
node = DummySchemaNode(None)
node.children = [DummySchemaNode(None, name='a')]
typ = self._makeOne()
result = typ.serialize(node, (drop,))
self.assertEqual(result, [])

def test_serialize_not_iterable(self):
node = DummySchemaNode(None)
typ = self._makeOne()
Expand Down Expand Up @@ -3319,6 +3327,24 @@ class MySchema(colander.SequenceSchema):
e.msg,
'Sequence schemas must have exactly one child node')

def test_deserialize_drop(self):
import colander
class MySchema(colander.SequenceSchema):
a = colander.SchemaNode(colander.String(), missing=colander.drop)
node = MySchema()
result = node.deserialize([None])
self.assertEqual(result, [])
result = node.deserialize([colander.null])
self.assertEqual(result, [])

def test_serialize_drop_default(self):
import colander
class MySchema(colander.SequenceSchema):
a = colander.SchemaNode(colander.String(), default=colander.drop)
node = MySchema()
result = node.serialize([colander.null])
self.assertEqual(result, [])

class TestTupleSchema(unittest.TestCase):
def test_it(self):
import colander
Expand Down