I'm using the ValidationRulesServlet to generate valdr JSON for my APIs. Currently the generated JSON looks like this:
{
"Person" : {
"firstName" : {
"size" : {
"min" : 2,
"message" : "{javax.validation.constraints.Size.message}",
"max" : 2147483647
},
"required" : {
"message" : "{javax.validation.constraints.NotNull.message}"
}
},
"lastName" : {
"size" : {
"min" : 2,
"message" : "{javax.validation.constraints.Size.message}",
"max" : 20
},
"required" : {
"message" : "{javax.validation.constraints.NotNull.message}"
}
}
}
}
I'm using Jersey for my REST services and I want the messages in the above JSON to be replaced with values from ValidationMessages.properties.
My ValidationMessages.properties is located in classpath (src/main/resources) and is used correctly by Jackson. This can be confirmed by calling a REST endpoint with an invalid value. Here is an example response:
[
{
"message": "Must be between 2 and 2147483647 characters",
"messageTemplate": "{javax.validation.constraints.Size.message}",
"path": "PersonServiceImpl.updatePerson.arg0.firstName",
"invalidValue": ""
}
]
The corresponding message in my ValidationMessages.properties is
javax.validation.constraints.Size.message = Must be between {min} and {max} characters
How can I get the valdr JSON to output messages from ValidationMessages.properties rather than e.g. {javax.validation.constraints.Size.message}?
You can't, at least not out of the box. The reason is pretty obvious if you check the Bean Validation spec or look at the
ConstraintJavadoc:So, in essence
"message" : "{javax.validation.constraints.Size.message}"in the valdr constraint JSON signifies the message key rather than the actual validationmessage. It'd IMO be more sensible to call the JSON propertymessageKeyto make this very clear but we wanted to stick with the Bean Validation lingo. In fact, all the properties in the JSON are extracted 1-by-1 from the Bean ValidationConstraint.So, you need a way to display "Must be between 2 and 2147483647 characters" in your AngularJS front-end if the
Person.firstName.sizeconstraint is violated. valdr achieves that by integrating well with angular-translate. All you need to do is to make yourValidationMessages.propertiesavailable to the front-end and to initialize angular-translate with the messages from that file.