Merge branch 'asdict-i18n' of https://github.com/makinacorpus/colander
Conflicts: CHANGES.txt CONTRIBUTORS.txt
This commit is contained in:
@@ -23,6 +23,9 @@ Features
|
||||
- Add `Any` validator which succeeds if at least one of its subvalidators
|
||||
succeeded.
|
||||
|
||||
- Allow localization of error messages returned by ``colander.Invalid.asdict``
|
||||
by adding an optional ``translate`` callable argument.
|
||||
|
||||
1.0b1 (2013-09-01)
|
||||
------------------
|
||||
|
||||
|
@@ -115,3 +115,4 @@ Contributors
|
||||
- Peter Lamut, 2013/08/16
|
||||
- Veeti Paananen, 2013/08/20
|
||||
- Michael Howitz, 2013/12/05
|
||||
- Alex Marandon, 2013/12/21
|
||||
|
@@ -165,9 +165,14 @@ class Invalid(Exception):
|
||||
return str(self.pos)
|
||||
return str(self.node.name)
|
||||
|
||||
def asdict(self):
|
||||
def asdict(self, translate=None):
|
||||
""" Return a dictionary containing a basic
|
||||
(non-language-translated) error report for this exception"""
|
||||
(non-language-translated) error report for this exception.
|
||||
|
||||
If ``translate`` is supplied, it must be a callable taking a
|
||||
translation string as its sole argument and returning a localized,
|
||||
interpolated string.
|
||||
"""
|
||||
paths = self.paths()
|
||||
errors = {}
|
||||
for path in paths:
|
||||
@@ -177,6 +182,8 @@ class Invalid(Exception):
|
||||
exc.msg and msgs.extend(exc.messages())
|
||||
keyname = exc._keyname()
|
||||
keyname and keyparts.append(keyname)
|
||||
if translate:
|
||||
msgs = [translate(msg) for msg in msgs]
|
||||
errors['.'.join(keyparts)] = '; '.join(interpolate(msgs))
|
||||
return errors
|
||||
|
||||
|
@@ -3446,6 +3446,39 @@ class TestFunctional(object):
|
||||
errors = e.asdict()
|
||||
self.assertEqual(errors, expected)
|
||||
|
||||
def test_invalid_asdict_translation_callback(self):
|
||||
from translationstring import TranslationString
|
||||
|
||||
expected = {
|
||||
'schema.int': 'translated',
|
||||
'schema.ob': 'translated',
|
||||
'schema.seq.0.0': 'translated',
|
||||
'schema.seq.1.0': 'translated',
|
||||
'schema.seq.2.0': 'translated',
|
||||
'schema.seq.3.0': 'translated',
|
||||
'schema.seq2.0.key': 'translated',
|
||||
'schema.seq2.0.key2': 'translated',
|
||||
'schema.seq2.1.key': 'translated',
|
||||
'schema.seq2.1.key2': 'translated',
|
||||
'schema.tup.0': 'translated',
|
||||
}
|
||||
data = {
|
||||
'int': '20',
|
||||
'ob': 'no.way.this.exists',
|
||||
'seq': [('q', 's'), ('w', 's'), ('e', 's'), ('r', 's')],
|
||||
'seq2': [{'key': 't', 'key2': 'y'}, {'key':'u', 'key2':'i'}],
|
||||
'tup': ('s', 's'),
|
||||
}
|
||||
schema = self._makeSchema()
|
||||
e = invalid_exc(schema.deserialize, data)
|
||||
|
||||
def translation_function(string):
|
||||
return TranslationString('translated')
|
||||
|
||||
errors = e.asdict(translate=translation_function)
|
||||
self.assertEqual(errors, expected)
|
||||
|
||||
|
||||
class TestImperative(unittest.TestCase, TestFunctional):
|
||||
|
||||
def _makeSchema(self, name='schema'):
|
||||
|
Reference in New Issue
Block a user