Commits

Adam Lowry  committed 7a92f2e

Store the pre-adaptaion raw value on elements

  • Participants
  • Parent commits 85d2fdb

Comments (0)

Files changed (5)

File flatland/schema/base.py

     :meth:`set` to update the element's value.
     """
 
+    raw = None
+    """The element's raw, unadapted value from input."""
+
     u = u''
     """A Unicode representation of the element's value.
 

File flatland/schema/containers.py

         """
 
         del self[:]
+        self.raw = iterable
         values, converted = [], True
         try:
             for v in iterable:
 
     def set(self, value):
         """TODO: doc set()"""
+        self.raw = value
         pairs = to_pairs(value)
         self._reset()
 
 
     def set(self, value, policy=None):
         """TODO: doc set()"""
+        self.raw = value
         pairs = to_pairs(value)
         self._reset()
 

File flatland/schema/scalars.py

         contain ``unicode(value)`` or ``u''`` for none.
 
         """
+        self.raw = value
         try:
             # adapt and normalize the value, if possible
             value = self.value = self.adapt(value)

File tests/schema/test_containers.py

     assert el.validate()
 
 
+def test_raw_value():
+    schema = Dict.of(Integer.named('x').using(optional=False))
+    el = schema()
+    assert el.raw is None
+
+    el = schema('foo')
+    assert el.raw == 'foo'
+
+    el = schema({'x': 'bar'})
+    assert el.raw == {'x': 'bar'}
+
+    el = schema([('x', 'bar')])
+    assert el.raw == [('x', 'bar')]
+
+    schema = List.of(Integer)
+    el = schema()
+    assert el.raw is None
+
+    el = schema('foo')
+    assert el.raw == 'foo'
+
+    el = schema([1, 2, 3])
+    assert el.raw == [1, 2, 3]
+
+    el = schema((1, 2, 3))
+    assert el.raw == (1, 2, 3)
+
+    el = schema({'x': 'bar'})
+    assert el.raw == {'x': 'bar'}
+
+
 class TestContainerValidation(object):
 
     def setup(self):

File tests/schema/test_scalars.py

         return element
 
     eq_(element_for(u'a').value, u'1')
+    eq_(element_for(u'a').raw, u'1')
     eq_(element_for(u'b').value, u'2')
+    eq_(element_for(u'b').raw, u'2')
     eq_(element_for(u'c').value, None)
+    eq_(element_for(u'c').raw, None)
 
 
 @requires_unicode_coercion