# Override property field names in error messages

**URL:** <https://discourse.hibernate.org/t/override-property-field-names-in-error-messages/766>\
**Category:** Bean Validation (JSR 380)\
**Created:** [May 15, 2018, 7:28pm UTC](https://discourse.hibernate.org/t/override-property-field-names-in-error-messages/766 "2018-05-15T19:28:11Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![nhojpatrick](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/nhojpatrick/32/188_2.png) [@nhojpatrick](https://discourse.hibernate.org/u/nhojpatrick)\
**Post date:** [May 15, 2018, 7:28pm UTC](https://discourse.hibernate.org/t/override-property-field-names-in-error-messages/766/1 "2018-05-15T19:28:11Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![gsmet](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/gsmet/32/9_2.png) [@gsmet](https://discourse.hibernate.org/u/gsmet)\
**Post date:** [May 15, 2018, 10:17pm UTC](https://discourse.hibernate.org/t/override-property-field-names-in-error-messages/766/2 "2018-05-15T22:17:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![p3consulting](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/p3consulting/32/565_2.png) [@p3consulting](https://discourse.hibernate.org/u/p3consulting)\
**Post date:** [May 16, 2018, 6:00am UTC](https://discourse.hibernate.org/t/override-property-field-names-in-error-messages/766/3 "2018-05-16T06:00:29Z")

</div>

-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](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

---

<div class="post-metadata">

**Author:** ![nhojpatrick](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/nhojpatrick/32/188_2.png) [@nhojpatrick](https://discourse.hibernate.org/u/nhojpatrick)\
**Post date:** [May 16, 2018, 9:31am UTC](https://discourse.hibernate.org/t/override-property-field-names-in-error-messages/766/4 "2018-05-16T09:31:13Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![gsmet](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/gsmet/32/9_2.png) [@gsmet](https://discourse.hibernate.org/u/gsmet)\
**Post date:** [May 16, 2018, 10:10am UTC](https://discourse.hibernate.org/t/override-property-field-names-in-error-messages/766/5 "2018-05-16T10:10:38Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![nhojpatrick](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/nhojpatrick/32/188_2.png) [@nhojpatrick](https://discourse.hibernate.org/u/nhojpatrick)\
**Post date:** [May 16, 2018, 10:20am UTC](https://discourse.hibernate.org/t/override-property-field-names-in-error-messages/766/6 "2018-05-16T10:20:41Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![gsmet](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/gsmet/32/9_2.png) [@gsmet](https://discourse.hibernate.org/u/gsmet)\
**Post date:** [May 16, 2018, 10:49am UTC](https://discourse.hibernate.org/t/override-property-field-names-in-error-messages/766/7 "2018-05-16T10:49:03Z")

</div>

I knew it rang a bell yesterday evening when I first read this post. We already have: [https://hibernate.atlassian.net/browse/HV-823](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.
