Override property field names in error messages

It might be possible but I’ve been looking for months but found nothing;
I’ve got this json;
{
“inner_object”: {
“string_variable”: “”
}
}
Using Jackson to map to these object;

import com.fasterxml.jackson.annotation.JsonProperty;
public class OuterObject {
@JsonProperty(“inner_object”)
private InnerObject inner;
}

import com.fasterxml.jackson.annotation.JsonProperty;
import javax.validation.constraints.NotNull;
public class InnerObject {
@JsonProperty(“string_variable”)
@NotNull
private String stringVariable;
}

When it’s validating it picks up “stringVariable” and “inner” in the error messages if stringVariable is Null.

Without manually doing string replacements, how can I change error messages so they state “string_variable” and “inner_object” respectively.

Unfortunately, it’s not something you can do at the moment.

-you can use the message field of the @NotNull annotation to provide your own

-you can write a class implementing javax.validation.MessageInterpolator (https://docs.oracle.com/javaee/7/api/javax/validation/MessageInterpolator.html) and wire the class in Validation.byDefaultProvider().configure().messageInterpolator( YOUR_OWN_MSG_INTERPOLATOR )... and opt. use the payload field of the validation annotation to help your code resolve the message

I want the out of the box message just with a custom property name, as it’s not just @NotNull as I’m using most of the annotations so would like to not duplicate messages.

At the moment I’ve got a massive string replacement method that knows all internal field names and external json names and replaces them in the messages. It works in most casts but it no ideal.

I’ll look into a PR to give this functionality.

@p3consulting

Sure. But both of your solutions require a lot of boilerplate as you don’t have the necessary information where you’re trying to do it. It’s not really what’s the OP wants.

In ConstraintValidator.isValid(), you don’t have any information about the concerned bean class, which could have allowed to rely on reflection for instance (but that would have been slow at runtime).

@nhojpatrick

Better create a JIRA issue and discuss the design first. I’m not sure how such a feature would look like. What could make sense (thinking out loud here) might be to add a PropertyNameProvider SPI that would take a Member as a parameter and thus allow to check the annotations for such a name.

This way, we could resolve the names at bootstrap, instead of doing it at runtime.

I was going to take a look at how it currently works.
Then raise a JIRA regarding current issue, propose a potential design from how i see the code works and how it might work, then get input as to flaws in that design, am i fighting the framework instead of working with the framework.
Also considering some uses might want static field names, others might want it to pick up jackson’s dynamic ability to rewrite json fields or simply use the hard code jackson field names.

As my solution would be aimed at Jackson and Validation working better together but I’m fully aware the ideal solution would probably need to consider support for other framework hooks to be added.

I knew it rang a bell yesterday evening when I first read this post. We already have: https://hibernate.atlassian.net/browse/HV-823 which was open a very long time ago with exactly the same requirement as yours in mind.

There are some preliminary discussions in the issue.

It’s definitely something I would like to have in 6.1 if you’re willing to work on it. I’ll obviously provide guidance to drive it home.

Let’s move the discussion to the JIRA issue, I’ll add a few things I have in mind there.