added a ValidatorError.schema properties which contains a dict representation of the whole validation error schema

This commit is contained in:
Nicolas Perriault
2011-10-24 17:15:34 +02:00
parent 3d7b30da77
commit 62480fe940
2 changed files with 70 additions and 0 deletions

View File

@@ -20,10 +20,13 @@ class InvalidDocumentError(Exception):
class ValidationError(Exception):
"""Validation exception.
"""
errors = {}
field_name = None
def __init__(self, message, **kwargs):
self.errors = kwargs.get('errors', {})
self.field_name = kwargs.get('field_name')
super(ValidationError, self).__init__(message)
@@ -40,6 +43,25 @@ class ValidationError(Exception):
else:
return message
@property
def schema(self):
def get_schema(source):
errors_dict = {}
if not source:
return errors_dict
if isinstance(source, dict):
for field_name, error in source.iteritems():
errors_dict[field_name] = get_schema(error)
elif isinstance(source, ValidationError) and source.errors:
return get_schema(source.errors)
else:
return unicode(source)
return errors_dict
if not self.errors:
return {}
return get_schema(self.errors)
_document_registry = {}