hibernate-validator icon indicating copy to clipboard operation
hibernate-validator copied to clipboard

HV-2073 Add new OneOfValidator for CharSequence validation

Open aalamu opened this issue 11 months ago • 6 comments

Description:

Contributed a new OneOfValidator for the Hibernate Validator project.

This validator checks if a given CharSequence matches one of the allowed values specified in the OneOf annotation.

Features:

  • Validates if a CharSequence is one of the allowed values.
  • Supports case-sensitive and case-insensitive validation based on the ignoreCase flag.
  • Supports both Enum constants and manually provided allowed values.

The validator is implemented with the following:

  • OneOfValidator class implementing ConstraintValidator<OneOf, CharSequence>.
  • Adds values from allowedValues() and enumClass() in the OneOf annotation for validation.
  • Provides methods to handle case-sensitive and case-insensitive validation.

Tests have been added, and all tests pass.

[Please describe here what your change is about]


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license. For more information on licensing, please check here.


aalamu avatar Dec 30 '24 16:12 aalamu

Hey @aalamu 👋🏻 Thanks for starting a pull request! I'll try to take a closer look and think more about the suggested constraint this week, but here are a few things:

  • From what I've seen, you have an enum and a string array as the source of "possible" values. Have you considered extending this to other types? The first thing that comes to mind here is JUnit's "argument sources" for parameterised tests: https://junit.org/junit5/docs/current/user-guide/#writing-tests-parameterized-tests-sources. We have this post on adding new constraints: https://in.relation.to/2018/01/04/adding-new-constraint-to-engine/. It describes all the steps so that we don't miss anything. You can take a look to see what's needed in general, but I suggest we figure out the constraint itself and how it should/could behave first, then go back to these instructions to add all we need.

marko-bekhta avatar Jan 07 '25 14:01 marko-bekhta

Hi @marko-bekhta,

I would go through the details. Thank you.

aalamu avatar Jan 09 '25 18:01 aalamu

Thanks for your pull request!

This pull request appears to follow the contribution rules.

› This message was automatically generated.

Hi @marko-bekhta ,

I've made an to the PR. I refactored the OneOf validator to make it versatile with different types.

I also tried to investigate the Sonar analysis failure, I couldn't find enough details regarding it except unknown host or service.

aalamu avatar Jan 17 '25 16:01 aalamu