Error raising exception in validation: not all arguments converted during string formatting
I am trying to create my own validators. I copied the min length validator in the example. However, everytime it fails, I get the below error instead
not all arguments converted during string formatting
class MaxLengthValidator(object):
def __init__(self, max_length):
self.max_length = int(max_length)
def __call__(self, value):
print 'Max Length Validator %s'% value
print self.max_length
if len(value) <= self.max_length:
print value
return True
else:
print 'Error'
msg = 'must be at most %d characters long.'%self.max_length
print msg
raise Exception(msg,)
return False
Traceback (most recent call last):
File "
TypeError: not all arguments converted during string formatting
I found the error and fixed it in my own code. in schema_document.py linke 642 onwards ORIGINAL:
def _process_validators(self, doc, struct, path=""):
doted_struct = DotCollapsedDict(self.structure)
doted_doc = DotCollapsedDict(doc)
for key, validators in self.validators.iteritems():
if key in doted_doc and doted_doc[key] is not None:
if not hasattr(validators, "__iter__"):
validators = [validators]
for validator in validators:
try:
if not validator(doted_doc[key]):
raise ValidationError("%s does not pass the validator " + validator.__name__)
except Exception, e:
self._raise_exception(ValidationError, key,
unicode(e) % key)
FIXED
def _process_validators(self, doc, struct, path=""):
doted_struct = DotCollapsedDict(self.structure)
doted_doc = DotCollapsedDict(doc)
for key, validators in self.validators.iteritems():
if key in doted_doc and doted_doc[key] is not None:
if not hasattr(validators, "__iter__"):
validators = [validators]
for validator in validators:
try:
if not validator(doted_doc[key]):
raise ValidationError("%s does not pass the validator "%key + validator.__name__)
except Exception, e:
self._raise_exception(ValidationError, key,
unicode(e))
- in the definition of the validator class, the code was looking for attirbute name, that's why it was raising an exception when running the validator (not that the vaildation failed).
- In the exception handler, their was an error which caused the exception
you can just write space holder like:
raise ValidationError('%s')
then the Validation message can show nicely
but the recommended approach for this is fix the MogonKit source as @jannah commented