jazzband / jsonmodels

jsonmodels is library to make it easier for you to deal with structures that are converted to, or read from JSON.
http://jsonmodels.readthedocs.org/en/latest/
BSD 3-Clause "New" or "Revised" License
334 stars 51 forks source link

Model validation should return the list of errors instead of the encountered error #123

Open sergray opened 6 years ago

sergray commented 6 years ago

Current implementation of validation raises the jsonmodels.errors.ValidationError as soon as the first error is encountered.

>>> from jsonmodels import models, fields, validators
>>> class Person(models.Base):
...    name = fields.StringField(required=True, validators=validators.Length(1))
...    age = fields.IntField(required=True, validators=[validators.Min(14)])
... 
>>> p = Person()
>>> p.validate()
Traceback (most recent call last):
...
jsonmodels.errors.ValidationError: ("Error for field 'age'.", ValidationError('Field is required!',))    

That means there's no way to get an overview of all validation errors for particular model, which is essential for complex data models.

It would be great to have complete model validation errors, with complete list of the errors per each field. In above example, that could be

jsonmodels.errors.ValidationError: {'name': ['Field is required'], 'age': ['Field is required']}

That should work for embedded fields as well.

It is an open question, whether all validator for particular field should be executed or if inclusion of the first validation error for particular field is enough.