Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #40 from rbu/master

Enable colander.Function to use ${val} in error messagehttps://github.com/Pylons/colander/issues/40/close
  • Loading branch information...
commit 783bcb7a84a047d35896d278a7908f2b8ede451a 2 parents d9f5934 + db8dbf3
@kiorky kiorky authored
Showing with 26 additions and 2 deletions.
  1. +2 −2 colander/__init__.py
  2. +24 −0 colander/tests/test_colander.py
View
4 colander/__init__.py
@@ -225,9 +225,9 @@ def __init__(self, function, message=_('Invalid value')):
def __call__(self, node, value):
result = self.function(value)
if not result:
- raise Invalid(node, self.message)
+ raise Invalid(node, translationstring.TranslationString(self.message, mapping={'val':value}))
if isinstance(result, string_types):
- raise Invalid(node, result)
+ raise Invalid(node, translationstring.TranslationString(result, mapping={'val':value}))
class Regex(object):
""" Regular expression validator.
View
24 colander/tests/test_colander.py
@@ -243,6 +243,30 @@ def test_fail_function_returns_string(self):
e = invalid_exc(validator, None, None)
self.assertEqual(e.msg, 'fail')
+ def test_error_message_adds_mapping_to_configured_message(self):
+ validator = self._makeOne(lambda x: False, message='fail ${val}')
+ e = invalid_exc(validator, None, None)
+ self.assertEqual(e.msg.interpolate(), 'fail None')
+
+ def test_error_message_adds_mapping_to_return_message(self):
+ validator = self._makeOne(lambda x: 'fail ${val}')
+ e = invalid_exc(validator, None, None)
+ self.assertEqual(e.msg.interpolate(), 'fail None')
+
+ def test_error_message_does_not_overwrite_configured_domain(self):
+ import translationstring
+ _ = translationstring.TranslationStringFactory('fnord')
+ validator = self._makeOne(lambda x: False, message=_('fail ${val}'))
+ e = invalid_exc(validator, None, None)
+ self.assertEqual(e.msg.domain, 'fnord')
+
+ def test_error_message_does_not_overwrite_returned_domain(self):
+ import translationstring
+ _ = translationstring.TranslationStringFactory('fnord')
+ validator = self._makeOne(lambda x: _('fail ${val}'))
+ e = invalid_exc(validator, None, None)
+ self.assertEqual(e.msg.domain, 'fnord')
+
def test_propagation(self):
validator = self._makeOne(lambda x: 'a' in x, 'msg')
self.assertRaises(TypeError, validator, None, None)
Please sign in to comment.
Something went wrong with that request. Please try again.