I’m using Jakarta/Hibernate to validate some fields in Java.
The problem is the data model is generated from Swagger file thus all the Jakarta annotations like @Size or @Pattern are autogenerated.
I cannot change the Swagger to generate some custom annotations but I have to create one single validation message based on all the constraints/annotations assigned to particular field.
My idea is to implement custom MessageInterpolator. I can get default interpolator using HibernateValidatorConfiguration class and set up constraint mapping:
`constraintMapping.constraintDefinition(->annotation here<-)
.validateBy(MyValidator.class);
configurationHibernate.addMapping(constraintMapping)
.messageInterpolator(myInterpolator);
`
As you can see it can handle only one annotation but I want to write only one validator and only one interpolator for all the annotations at once to have min, max, pattern values and create a message based on them.
Is there any way to achieve such implementation to get all the available annotations from the particular field and create a message based on the available annotations?
It’s worth to mention that one field can have @Size and @Pattern but another one could posses only @Size. For both class fields I want to use the same validator/message interpolator and create a message dynamically.
I know this is not the way Hibernate was designed so any other suggestions welcome. 🙂
I was trying to access the field and all available annotations by Jakarta validation Context class but with no success.
frog_in_the_fog is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.