Allow kiwi to register its custom messages with Hibernate validator #689
Replies: 4 comments 1 reply
-
Also see #690 |
Beta Was this translation helpful? Give feedback.
-
Additional resources to read: |
Beta Was this translation helpful? Give feedback.
-
The better way to do this is definitely adding custom constraint definitions via the Java service loader mechanism. The one thing that could be problematic is using the Maven Shade plugin to build and uber-JAR, since only one But as long as only the service has its own |
Beta Was this translation helpful? Give feedback.
-
This is addressed and fixed by #1112 , so this discussion is now closed. |
Beta Was this translation helpful? Give feedback.
-
kiwi has its own
ValidatorMessages.properties
which works fine in our services because none of those services have their ownValidatorMessages.properties
. But if a service (or any application) uses kiwi and includes its ownValidatorMessages.properties
, then that takes precedence over kiwi's and none of kiwi's validation annotations will work as expected; instead of having the expected violation message, you would simply get the name of the message template, e.g. you would see something like{org.kiwiproject.validation.Required.message}
instead ofis required
. I guess this could be considered a bug, but I've labeled this discussion as an enhancement instead.So, how can we make kiwi register its custom messages with Hibernate Validator (or whatever the validation provider is)?
I searched for how to do this and found How to use Java Hibernate validation with multiple Resource bundles? The accepted answer is the one we care about.
Per the answer, there is a lovely
AggregateResourceBundleLocator
, e.g.:But, we want to use the above to still get the default behavior (i.e. using the bundle that comes with Hibernate Validator) as well as the one in kiwi and any one provided by an application.
And here's how we can do it, though not sure if this is "production-ready":
Hibernate Validator takes care of also adding its own (i.e. the
ValidationMessages_xx.properties
properties files located inorg.hibernate.validator
). So in the above example code, three message bundles will be registered:ValidationMessages
at the classpath root,org.kiwiproject.validation.ValidationMessages
, andorg.hibernate.validator.ValidationMessages
. Each bundle should of course ensure that its property names are unique; in kiwi we prefix all ours withorg.kiwiproject.validation
.We will also need to move kiwi's
ValidationMessages.properties
intosrc/main/resources/org/kiwi/validation/
in order for this to work. (And need to build a kiwi JAR and verify that I am correct.)Another question is whether this will also pick up other locales, e.g.
ValidationMessages_es.properties
. I'm pretty sure the answer is "yes" since this is how Java's message bundles work.And, we also need to update
KiwiValidations#newValidator
to do the above, and might also want to allow callers to specify this additional configuration, e.g.Or just always include the default bundle and kiwi's bundle and allow users to exclude them if desired? e.g.
This would allow someone to completely override the default messages from Hibernate Validator and/or kiwi. One question would be what happens if you include a bundle that doesn't exist, e.g. if there is no
ValidationMessages.properties
at the root-level? Other than that, we can use theAggregateResourceBundleLocator
to make this all work much better.Beta Was this translation helpful? Give feedback.
All reactions