Skip to content
This repository has been archived by the owner on Jul 14, 2020. It is now read-only.

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

Open
jannah opened this issue Jan 25, 2015 · 2 comments

Comments

@jannah
Copy link

jannah commented Jan 25, 2015

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
Copy link
Author

jannah commented Jan 25, 2015

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

@TaylorHere
Copy link

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants