You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Jul 14, 2020. It is now read-only.
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
The text was updated successfully, but these errors were encountered:
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
Sign up for freeto subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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
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
The text was updated successfully, but these errors were encountered: