I have a code that gets data from the user and validates rather it's valid or not.
The validation is for the data from the URL and the data from the JSON.
The problem is that in case of URL the path field contains arg0 and that it requires me to take it from the message:
@ValidId (message = "The field is invalid")
private Long field;
annotation of the field.
In case of JSON, I simply can get the field from the path.substring(path.lastIndexOf('.') + 1).
i.e.
protected String buildErrorMessage(ConstraintViolation<?> violation) {
String path = violation.getPropertyPath().toString();
String field = path.substring(path.lastIndexOf('.') + 1);
//field = `arg0` in case of url
//field = `field` in case of JSON
}
If I'm facing a ConstraintViolation -
how can I find out if the violation is from JSON or GET?
EDIT
This is where I call the buildErrorMessage from -
public class ValidationExceptionMapper implements ExceptionMapper<ValidationException> {
@Override
public Response toResponse(ValidationException exception) {
if (exception instanceof ConstraintViolationException) {
final ConstraintViolationException constraint = (ConstraintViolationException) exception;
for (final ConstraintViolation<?> violation : constraint.getConstraintViolations()) {
String message = buildErrorMessage(violation); //HERE
}
}