Author: ianb
Date: 2007-10-17 11:21:44 -0600 (Wed, 17 Oct 2007)
New Revision: 3084
Added:
FormEncode/trunk/tests/test_validators.py
Modified:
FormEncode/trunk/docs/news.txt
FormEncode/trunk/formencode/validators.py
Log:
Improvements to validators.String, suggested by Felix Schwartz.
Modified: FormEncode/trunk/docs/news.txt
===================================================================
--- FormEncode/trunk/docs/news.txt 2007-10-17 17:17:52 UTC (rev 3083)
+++ FormEncode/trunk/docs/news.txt 2007-10-17 17:21:44 UTC (rev 3084)
@@ -28,6 +28,13 @@
* Include ``formencode.validators.InternationalPhoneNumber`` from
W-Mark Kubacki.
+* Treat empty lists/tuples/dicts as "empty".
+
+* ``validators.String`` now always returns strings. It also converts
+ lists to comma-separated strings (no ``[...]``), and can encode
+ unicode if an ``encoding`` parameter is given. Empty values are
+ handled better.
+
0.7.1
-----
Modified: FormEncode/trunk/formencode/validators.py
===================================================================
--- FormEncode/trunk/formencode/validators.py 2007-10-17 17:17:52 UTC (rev 3083)
+++ FormEncode/trunk/formencode/validators.py 2007-10-17 17:21:44 UTC (rev 3084)
@@ -967,6 +967,10 @@
Also takes a `max` and `min` argument, and the string length must
fall in that range.
+ Also you may give an `encoding` argument, which will encode any
+ unicode that is found. Lists and tuples are joined with
+ `list_joiner` (default ``', '``) in ``from_python``.
+
::
>>> String(min=2).to_python('a')
@@ -997,6 +1001,8 @@
min = None
max = None
not_empty = None
+ encoding = None
+ list_joiner = ', '
messages = {
'tooLong': _("Enter a value less than %(max)i characters long"),
@@ -1006,8 +1012,37 @@
def __initargs__(self, new_attrs):
if self.not_empty is None and self.min:
self.not_empty = True
+
+ def _to_python(self, value, state):
+ if not value:
+ value = ''
+ if not isinstance(value, basestring):
+ try:
+ value = str(value)
+ except UnicodeEncodeError:
+ value = unicode(value)
+ if self.encoding is not None and isinstance(value, unicode):
+ value = value.encode(self.encoding)
+ return value
+
+ def _from_python(self, value, state):
+ if not value and value != 0:
+ value = ''
+ if not isinstance(value, basestring):
+ if isinstance(value, (list, tuple)):
+ value = self.list_joiner.join([
+ self._from_python(v, state) for v in value])
+ try:
+ value = str(value)
+ except UnicodeEncodeError:
+ value = unicode(value)
+ if self.encoding is not None and isinstance(value, unicode):
+ value = value.encode(self.encoding)
+ if self.strip:
+ value = value.strip()
+ return value
- def validate_python(self, value, state):
+ def validate_other(self, value, state):
if (self.max is not None and value is not None
and len(value) > self.max):
raise Invalid(self.message('tooLong', state,
@@ -1019,13 +1054,6 @@
min=self.min),
value, state)
- def _from_python(self, value, state):
- if value:
- return str(value)
- if value == 0:
- return str(value)
- return ""
-
def empty_value(self, value):
return ''
Added: FormEncode/trunk/tests/test_validators.py
===================================================================
--- FormEncode/trunk/tests/test_validators.py (rev 0)
+++ FormEncode/trunk/tests/test_validators.py 2007-10-17 17:21:44 UTC (rev 3084)
@@ -0,0 +1,40 @@
+from formencode.validators import String, Invalid
+
+def validate(validator, value):
+ try:
+ validator.to_python(value)
+ return None
+ except Invalid, e:
+ return e.unpack_errors()
+
+def validate_from(validator, value):
+ try:
+ validator.from_python(value)
+ return None
+ except Invalid, e:
+ return e.unpack_errors()
+
+messages = String().message
+
+def test_sv_min():
+ sv = String(min=2, accept_python=False)
+ assert sv.to_python("foo") == "foo"
+ assert validate(sv, "x") == messages('tooShort', None, min=2)
+ assert validate(sv, None) == messages('empty', None)
+ # should be completely invalid?
+ assert validate(sv, []) == messages('empty', None, min=2)
+ assert sv.from_python(['x', 'y']) == 'x, y'
+
+def test_sv_not_empty():
+ sv = String(not_empty=True)
+ assert validate(sv, "") == messages('empty', None)
+ assert validate(sv, None) == messages('empty', None)
+ # should be completely invalid?
+ assert validate(sv, []) == messages('empty', None)
+ assert validate(sv, {}) == messages('empty', None)
+
+def test_sv_string_conversion():
+ sv = String(not_empty=False)
+ assert sv.from_python(2) == "2"
+ assert sv.from_python([]) == ""
+
Property changes on: FormEncode/trunk/tests/test_validators.py
___________________________________________________________________
Name: svn:eol-style
+ native
|