jsonmodels icon indicating copy to clipboard operation
jsonmodels copied to clipboard

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

Open sergray opened this issue 6 years ago • 0 comments

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.

sergray avatar Jun 26 '18 12:06 sergray