- New argument to `colander.String
constructor:
allow_empty
`.
This is a boolean representing whether an empty string is a valid value during deserialization, defaulting to ``False``.
This commit is contained in:
@@ -11,6 +11,10 @@ Next release
|
|||||||
error message formatting, which may impact you if you were feeding
|
error message formatting, which may impact you if you were feeding
|
||||||
colander an error message template.
|
colander an error message template.
|
||||||
|
|
||||||
|
- New argument to ``colander.String`` constructor: ``allow_empty``.
|
||||||
|
This is a boolean representing whether an empty string is a valid
|
||||||
|
value during deserialization, defaulting to ``False``.
|
||||||
|
|
||||||
0.5.2 (2010-04-09)
|
0.5.2 (2010-04-09)
|
||||||
------------------
|
------------------
|
||||||
|
|
||||||
|
@@ -313,8 +313,8 @@ class Mapping(object):
|
|||||||
unknown
|
unknown
|
||||||
``unknown`` controls the behavior of this type when an
|
``unknown`` controls the behavior of this type when an
|
||||||
unknown key is encountered in the value passed to the
|
unknown key is encountered in the value passed to the
|
||||||
``deserialize`` method of this instance. The potential
|
``deserialize`` method of this instance. All the potential
|
||||||
values of ``unknown`` are:
|
values of ``unknown`` are strings. They are:
|
||||||
|
|
||||||
- ``ignore`` means that keys that are not present in the schema
|
- ``ignore`` means that keys that are not present in the schema
|
||||||
associated with this type will be ignored during
|
associated with this type will be ignored during
|
||||||
@@ -615,9 +615,15 @@ default_encoding = 'utf-8'
|
|||||||
class String(object):
|
class String(object):
|
||||||
""" A type representing a Unicode string.
|
""" A type representing a Unicode string.
|
||||||
|
|
||||||
This type constructor accepts a single argument ``encoding``,
|
This type constructor accepts a number of arguments:
|
||||||
representing the encoding which should be applied to object
|
|
||||||
serialization. It defaults to ``utf-8`` if not provided.
|
``encoding``
|
||||||
|
Represents the encoding which should be applied to object
|
||||||
|
serialization. It defaults to ``utf-8`` if not provided.
|
||||||
|
|
||||||
|
``allow_empty``
|
||||||
|
Boolean representing whether an empty string input to
|
||||||
|
deserialize will be accepted. Default: ``False``.
|
||||||
|
|
||||||
Input to ``serialize`` is serialized to a Python ``str`` object,
|
Input to ``serialize`` is serialized to a Python ``str`` object,
|
||||||
which is encoded in the encoding provided.
|
which is encoded in the encoding provided.
|
||||||
@@ -630,10 +636,11 @@ class String(object):
|
|||||||
The subnodes of the :class:`colander.SchemaNode` that wraps
|
The subnodes of the :class:`colander.SchemaNode` that wraps
|
||||||
this type are ignored.
|
this type are ignored.
|
||||||
"""
|
"""
|
||||||
def __init__(self, encoding=None):
|
def __init__(self, encoding=None, allow_empty=False):
|
||||||
if encoding is None:
|
if encoding is None:
|
||||||
encoding = default_encoding
|
encoding = default_encoding
|
||||||
self.encoding = encoding
|
self.encoding = encoding
|
||||||
|
self.allow_empty = allow_empty
|
||||||
|
|
||||||
def deserialize(self, node, value):
|
def deserialize(self, node, value):
|
||||||
try:
|
try:
|
||||||
@@ -643,7 +650,7 @@ class String(object):
|
|||||||
raise Invalid(node,
|
raise Invalid(node,
|
||||||
_('${val} is not a string: %{err}',
|
_('${val} is not a string: %{err}',
|
||||||
mapping={'val':value, 'err':e}))
|
mapping={'val':value, 'err':e}))
|
||||||
if not value:
|
if not value and not self.allow_empty:
|
||||||
if node.required:
|
if node.required:
|
||||||
raise Invalid(node, _('Required'))
|
raise Invalid(node, _('Required'))
|
||||||
value = node.default
|
value = node.default
|
||||||
|
@@ -632,9 +632,9 @@ class TestSequence(unittest.TestCase):
|
|||||||
self.assertEqual(len(e.children), 2)
|
self.assertEqual(len(e.children), 2)
|
||||||
|
|
||||||
class TestString(unittest.TestCase):
|
class TestString(unittest.TestCase):
|
||||||
def _makeOne(self, encoding='utf-8'):
|
def _makeOne(self, encoding='utf-8', allow_empty=False):
|
||||||
from colander import String
|
from colander import String
|
||||||
return String(encoding)
|
return String(encoding, allow_empty)
|
||||||
|
|
||||||
def test_alias(self):
|
def test_alias(self):
|
||||||
from colander import Str
|
from colander import Str
|
||||||
@@ -655,6 +655,12 @@ class TestString(unittest.TestCase):
|
|||||||
result = typ.deserialize(node, val)
|
result = typ.deserialize(node, val)
|
||||||
self.assertEqual(result, 'default')
|
self.assertEqual(result, 'default')
|
||||||
|
|
||||||
|
def test_deserialize_emptystring_allow_empty(self):
|
||||||
|
node = DummySchemaNode(None)
|
||||||
|
typ = self._makeOne(None, True)
|
||||||
|
result = typ.deserialize(node, '')
|
||||||
|
self.assertEqual(result, '')
|
||||||
|
|
||||||
def test_deserialize_uncooperative(self):
|
def test_deserialize_uncooperative(self):
|
||||||
val = Uncooperative()
|
val = Uncooperative()
|
||||||
node = DummySchemaNode(None)
|
node = DummySchemaNode(None)
|
||||||
|
Reference in New Issue
Block a user