Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide kiwi validation constraints via service loader mechanism #1112

Closed
sleberknight opened this issue Mar 10, 2024 · 0 comments · Fixed by #1113
Closed

Provide kiwi validation constraints via service loader mechanism #1112

sleberknight opened this issue Mar 10, 2024 · 0 comments · Fixed by #1113
Assignees
Labels
enhancement A request for change or improvement to an existing feature
Milestone

Comments

@sleberknight
Copy link
Member

For background see discussion #689 and Adding custom constraint definitions via the Java service loader.

This change has no impact to existing code other than to ensure kiwi provides its custom validation constraints in a way that they augment custom constraints in an application. Currently, kiwi's custom constraints uses ValidationMessages.properties as its validation messages. The problem is that if an application also uses its own ValidationMessages.properties (which is generally how you implement custom constraints in a service, such as a Dropwizard application) then the application's message bundle overrides the one in kiwi unless additonal steps are taken, for example using the Maven Shade Plugin and an AppendingTransformer to combine all ValidationMessages.properties. And this assumes there are no conflicts in property names in the bundle.

This refactoring uses the method discussed in this blog to use Java's service loader mechanism. It renames the ValidationMessages.properties to ContributorValidationMessages.properties, adds a META-INF/services/jakarta.validation.ConstraintValidator file that contains the list of kiwi ConstraintValidator implementations, and removes the validatedBy from all the validation annotations (which avoids them being found twice, once via the service loader mechanism, and once via validatedBy).

Note that if there are other libraries in an application which also provide their own custom constraints and use the same ContributorValidationMessages.properties, then an additional step is still required to combine these files, e.g., using the AppendingTransformer mentioned above.

@sleberknight sleberknight added the enhancement A request for change or improvement to an existing feature label Mar 10, 2024
@sleberknight sleberknight added this to the 3.4.0 milestone Mar 10, 2024
@sleberknight sleberknight self-assigned this Mar 10, 2024
sleberknight added a commit that referenced this issue Mar 10, 2024
* Rename ValidationMessages.properties to
  ContributorValidationMessages.properties
* Add jakarta.validation.ConstraintValidator in META-INF/services
  which defines the services that will be loaded via service loader
* Remove the value in the validatedBy attribute of Constraint
  annotations on all kiwi custom constraints to avoid duplicate
  definitions of them.

Closes #1112
sleberknight added a commit that referenced this issue Mar 10, 2024
* Rename ValidationMessages.properties to
  ContributorValidationMessages.properties
* Add jakarta.validation.ConstraintValidator in META-INF/services
  which defines the services that will be loaded via service loader
* Remove the value in the validatedBy attribute of Constraint
  annotations on all kiwi custom constraints to avoid duplicate
  definitions of them.

Closes #1112
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement A request for change or improvement to an existing feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant