Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[bean-validation-dev] Proposal for Enum Validation Annotation in Jakarta Bean Validation (@ValidEnum)

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:

  1. Simplified Validation: Reduces the need for custom validators or error-prone pattern matching.
  2. Flexibility: Allows developers to easily enable case-insensitive validation or adapt the validation logic to different Enum classes without much code duplication.
  3. 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


Back to the top