@@ -165,7 +165,7 @@ 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"""
|
||||
paths = self.paths()
|
||||
@@ -177,6 +177,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
|
||||
|
||||
|
@@ -3398,6 +3398,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