colander
colander copied to clipboard
Translation domains of messages passed to validators
Take a look at __call__
methods of Range validator:
if self.min is not None:
if value < self.min:
min_err = _(self.min_err, mapping={'val':value, 'min':self.min})
raise Invalid(node, min_err)
Shoudn't there be:
min_err = translationstring.TranslationString(self.min_err,
domain=self.min_err.domain,
default=self.min_err.default,
mapping={'val':value, 'min':self.min})
So it appears that if you pass a TranslationString into a TranslationString, it resolves as you would think. The properties of the inner TranslationString override the outer. So check out this code:
import colander
import translationstring
_ = translationstring.TranslationStringFactory('test')
schema = colander.SchemaNode(colander.Mapping(),
colander.SchemaNode(colander.Int(),
name='number',
validator=colander.Range(min=0, max=5, max_err=_('blah'))))
if __name__ == '__main__':
try:
data = schema.deserialize({'number': 10})
except Exception as e:
assert isinstance(e, colander.Invalid)
# more specifically
validator = colander.Range(min=0, max=5, max_err=_('blah'))
assert validator.max_err.domain == 'test'
P.S. sorry for getting around to this so late.
I have a validator:
def age_validator(node, value):
if value <= 0:
raise colander.Invalid(node, _(u"Age must be greater then 0"))
Error message is translated to polish, and in a browser I can see translation.
I have just checked that following version of valiadtor does not work (in the browser I see message stored in code).
def age_validator(node, value):
validator = colander.Range(min=1, min_err=_(u"Age must be greater then 0"))
assert (validator.min_err.domain == 'KonsultacjeNPR')
validator(node,value)
For some reason it does not matter that translation domain is correct. The problem does not occur when you use the modified version of the range validator (as I described in first comment).
I am using translationstring-1.1 and colander-0.9.9 (with colander-1.0a2 I have the same problem).
Ok I have your error reproduced. Going to attempt to find a solution for it.
This issue resulted in a pull request to translationstring, changing the way TranslationStringFactory works. It can be found here: https://github.com/Pylons/translationstring/pull/12.
Marking as "sprintable" to see if someone can determine if this is actually fixed.
Seems to be fixed to me.