hmarr/mongoengine — Issue #328: Retrieve all validation errors for ComplexBaseField, recursively
Author: n1k0
Created: 2011-10-22 13:38:37+00:00
State: open

As far as I can see, validating a ComplexBaseField won't give all the validation errors but only the first one encountered. Also, errors on EmbeddedDocumentField will just give something like `'Invalid EmbeddedDocumentField item (FooBaz)'`which is hard to debug, to say the least.

What would be cool is have access to some property in the `ValidationError` instance, eg. an `errors` one which could give something like:

``` python
def print_errors(errors):
for field_name, error in err.errors.iteritems():
if isinstance(error, dict):
print_errors(error)
else:
print 'error for %s: %s' % (field_name, error, )

try:
obj.validate()
except ValidationError, err:
print_errors(err.errors)
```

The `errors` property would basically contain a ditionnary with the validation errors:

``` python
errors = {
'field1': {
'subfield1': 'this field is required',
'subfield2': 'wrong type provided',
},
'field2': 'Email is invalid',
}
```

I can eventually work on a patch in my fork, but I would love to hear your thoughts and feedback on this proposition first.