Define additional data for a constraint

Description

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:

1 2 3 4 5 6 7 8 9 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.

1 2 3 4 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.

Environment

None

Status

Assignee

Unassigned

Reporter

Julien May

Labels

None

Worked in

None

Feedback Requested

None

Feedback Requested By

None

backPortable

None

Community Help Wanted

None

Suitable for new contributors

Yes, likely

Requires Release Note

None

Pull Request

None

backportDecision

None

backportReEvaluate

None

Components

Fix versions

Priority

Major