Commits

Adam Lowry committed 0bed10e

Add flatten_errors

  • Participants
  • Parent commits b5419c2

Comments (0)

Files changed (2)

flatland/schema/base.py

                          if e.flattenable)
         return pairs
 
+    def flatten_errors(self, sep=u'_'):
+        """Return errors as a flat sequence of key, [errors] pairs."""
+        if self.errors:
+            pairs = [(self.flattened_name(sep), self.errors)]
+        else:
+            pairs = []
+        if self.children_flattenable:
+            pairs.extend((e.flattened_name(sep), e.errors)
+                         for e in self.all_children
+                         if e.flattenable and e.errors)
+        return pairs
+
+
     def set(self, value):
         """Assign the native and Unicode value.
 

tests/validation/test_containers.py

 from flatland import (
     Array,
     Dict,
+    Form,
     Integer,
     List,
     String,
     HasAtMost,
     HasBetween,
     NotDuplicated,
+    ValueIn,
     )
 
 from tests._util import assert_raises
     el = schema(['a', 'b', 'c'])
     assert not el.validate()
     assert el.errors == [u'outer must contain at least 1 and at most 2 inners']
+
+
+def test_flatten_errors_list():
+    schema = validated_list(HasBetween(minimum=1, maximum=2))
+
+    el = schema()
+    el.validate()
+    assert not el.validate()
+    assert el.flatten_errors() == \
+        [(u'outer', [u'outer must contain at least 1 and at most 2 inners'])]
+
+    schema = validated_string(ValueIn(['x', 'y']))
+    el = schema(['x', 'z'])
+    el.validate()
+    assert el.flatten_errors() == \
+        [(u'test_1_foo', [u'z is not a valid value for foo.'])]
+
+    schema = validated_string(ValueIn(['x', 'y']))
+    el = schema(['x', 'z'])
+    el.validate()
+    assert el.flatten_errors() == \
+        [(u'test_1_foo', [u'z is not a valid value for foo.'])]
+
+
+def test_flatten_errors_dict():
+    schema = Dict.named('test').of(
+        String.using(name=u'foo').using(validators=[ValueIn(['x', 'y'])]))
+
+    el = schema()
+    el.validate()
+    assert not el.validate()
+    assert el.flatten_errors() == \
+        [(u'test_foo', [u'None is not a valid value for foo.'])]
+
+    el = schema({'foo': 'z'})
+    el.validate()
+    assert el.flatten_errors() == \
+        [(u'test_foo', [u'z is not a valid value for foo.'])]
+
+    el = schema({'foo': 'y'})
+    el.validate()
+    assert el.flatten_errors() == []
+
+
+def test_flatten_errors_form():
+    class ErrForm(Form):
+        test = Dict.of(
+            String.named(u'foo').using(validators=[ValueIn(['x', 'y'])]))
+
+    el = ErrForm({})
+    el.validate()
+    assert not el.validate()
+    assert el.flatten_errors() == \
+        [(u'test_foo', [u'None is not a valid value for foo.'])]
+
+    el = ErrForm({'test': {'foo': 'z'}})
+    el.validate()
+    assert el.flatten_errors() == \
+        [(u'test_foo', [u'z is not a valid value for foo.'])]
+
+    el = ErrForm({'test': {'foo': 'y'}})
+    el.validate()
+    assert el.flatten_errors() == []