I'm working on an experimental Cerberus branch that would change the way validation errors are reported in Cerberus and, consequently, by Eve.
What we have right now is a list of errors. Each item in the list, while perfectly fine for human reading, isn't really ideal for algorithm parsing. Why would you want to parse the errors with an algorithm? A common case would be when your client is using business objects to represent API resources (think a client-side ORM), and would have a hard time binding validation errors to the objects themselves.
What I am experimenting with is converting the issues list to a dict in which fields would be dict keys, and errors the values. So for example, instead of:
>>> validator.errors
{'age': 'min value is 10', 'name': 'must be of string type'}
This system would also allow for future expansion, like providing a unique code for every error type, etc.
Overall I'm fairly confident that this would be an important step forward, however I am unsure on how to handle fields of list types. Suppose you have a a list field with a schema like this:
'a_list_of_values': {
'type': 'list',
'items': [{'type': 'string'}, {'type': 'integer'}, ]
},
And your document is like this:
'a_list_of_values': ['a string', 'not an integer']
Given the new dictionary format, how would you report the error on the second field?
>>> validator.errors
{'a_list_of_values[1]': 'must be of integer type'}
Or:
>>> validator.errors
{'a_list_of_values': (1,'must be of integer type')}
Let's see what would happen in case of multiple validation errors within the same list:
>>> validator.errors
{'a_list_of_values[0]': 'must be of string type'}
{'a_list_of_values[1]': 'must be of integer type'}
Alternatively:
>>> validator.errors
{'a_list_of_values': [(1,'must be of integer type'), (0, 'must be of string type')]}
I tend to prefer the second option, in which we have a tuple with the list index and the error. This would make parsing much easier (no need to parse the keys, and you have only one key per field), at the cost of reduced user readability.
Again I'm convinced that, while it would sort of break backward compatibility, in the long run the new dict format would help client SDK developers immensely. Nothing is set on stone though, and I am particularly curious on what is your take on the lists format. Any feedback greatly appreciated,
Nicola.