In our company we use java bean validation for our restful api's. With some constraints it would be helpful to have a bit more context then just a message.
Something like:
public boolean isValid(Object value, ConstraintValidatorContext context) {
...
Map<String, Object> additionalErrorData = new HashMap<>();
additionalErrorData.put("ids_in_error", Lists.newArrayList(5, 6, 7));
...
context.addBeanNode().addData(additionalErrorData).addConstraintViolation();
...
}
this would allow to have additional data in a constraint violation that can be used together with the message(template) to create a meaningful error response including some structured data, thus making it easy to read and interpret by a machine.
Foo foo = ...;
Set<ConstraintViolation<Foo>> violations = validator.validate(foo);
ConstraintViolation<Foo> violation = violations.iterator().next();
Object additionalErrorData = violation.getData();
For sure such a feature needs quiet some polishing to make it suitable for all the possible cases. But at least i wanted it to be placed here for further discussion.
|