Hello Bean Validation Dev Team,
I hope this message finds you well.
I would like to propose a new feature for Jakarta Bean Validation, specifically an annotation for validating Enum
values within domain objects. Currently, there are mechanisms (such as @Pattern
) that can be used to validate enum-like fields, but they often require additional logic to ensure that the values are properly constrained. Introducing a dedicated @ValidEnum
annotation could simplify this and provide a more intuitive way of handling enum validation.
https://github.com/jakartaee/validation/issues/229
Problem Statement:
Validating that a string corresponds to a valid Enum
constant is a common requirement when processing user inputs in web forms, API requests, and other scenarios. While developers can handle this with custom validators or use patterns, it often results in repetitive code. A native annotation-based solution would significantly reduce boilerplate and improve the readability of validation logic.
Proposed Solution:
The @ValidEnum
annotation could be used to validate whether a given String
(or any CharSequence
) matches a valid constant of a specific Enum
type. Additionally, it could include options like case-insensitive matching, which would further improve flexibility. Here's a high-level look at the proposal:
Benefits:
- Simplified Validation: Reduces the need for custom validators or error-prone pattern matching.
- Flexibility: Allows developers to easily enable case-insensitive validation or adapt the validation logic to different
Enum
classes without much code duplication. - Readability: Improves the readability of domain objects by eliminating repetitive
Enum
checks.
Request for Feedback:
I believe this feature could enhance Jakarta Bean Validation by addressing a very common use case. I’d love to hear your thoughts on whether this could be a valuable addition to the framework. If there is interest, I would be happy to provide a pull request along with accompanying tests and documentation.
Thank you for your time and consideration.
Best regards,
Yusuf Alamu Musa