mongokit icon indicating copy to clipboard operation
mongokit copied to clipboard

Error raising exception in validation: not all arguments converted during string formatting

Open jannah opened this issue 10 years ago • 2 comments

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 "", line 16, in nyarticle.validate() File "C:\Users\Hassan\Dropbox\Berkeley\iSchool\Final Project\news\lib\site-packages\mongokit\document.py", line 253, in validate super(Document, self).validate() File "C:\Users\Hassan\Dropbox\Berkeley\iSchool\Final Project\news\lib\site-packages\mongokit\schema_document.py", line 388, in validate self._process_validators(self, self.structure) File "C:\Users\Hassan\Dropbox\Berkeley\iSchool\Final Project\news\lib\site-packages\mongokit\schema_document.py", line 655, in _process_validators unicode(e) % key)

TypeError: not all arguments converted during string formatting

jannah avatar Jan 25 '15 07:01 jannah

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))
  1. 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).
  2. In the exception handler, their was an error which caused the exception

jannah avatar Jan 25 '15 07:01 jannah

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

TaylorHere avatar Oct 14 '16 12:10 TaylorHere