Problem Statement
The current @jakarta.validation.constraints.Email annotation used in Spring Boot for email validation has limitations and doesn't cover many valid email formats according to RFC 5322. The previously deprecated @org.hibernate.validator.constraints.Email annotation could be reintroduced with improved validation that better aligns with modern email standards.
Current Limitations
- Jakarta's
@Emailimplementation misses valid email formats - Doesn't properly validate many legitimate email addresses
- Lacks comprehensive validation based on latest standards
- Doesn't provide detailed validation error messages
Proposed Solution
We propose reintroducing Spring's @Email annotation with an enhanced validation implementation that:
- Follows RFC 5322 standards completely
- Provides more comprehensive validation
- Handles special cases properly
- Offers better error messages
- Is thoroughly tested
Implementation Example
We have implemented a similar validation in our open source project that could serve as a reference:
Annotation:
https://github.com/afet-yonetim-sistemi/ays-be/blob/main/src/main/java/org/ays/common/util/validation/EmailAddress.java
Validator:
https://github.com/afet-yonetim-sistemi/ays-be/blob/main/src/main/java/org/ays/common/util/validation/EmailAddressValidator.java
Tests:
https://github.com/afet-yonetim-sistemi/ays-be/blob/9324e237b02a739b2c2cfd42b642a4ca92f0a399/src/test/java/org/ays/auth/controller/AysAuthControllerTest.java#L58
Key Features of the Proposed Implementation
- Comprehensive regex pattern that properly validates:
- Local part restrictions
- Domain part validation
- Special characters handling
- TLD validation
- Detailed validation messages
- Extensive test coverage
- Better performance than current implementation
Benefits
- More accurate email validation
- Better developer experience
- Reduced validation bypass risks
- Backward compatibility with existing code
- Improved error handling and messages
Example Usage
```java @EmailAddress private String emailAddress;
Comment From: wilkinsona
Thanks for the suggestion but, as far as I can recall, Spring Boot has never provided a custom @Email annotation for validation. In what version of Spring Boot did you find it?
Comment From: agitrubard
@Wilkinsona thank you for your feedback. Yes, you are right, I created an issue here with the hibernate misconception. I was talking about the @org.hibernate.validator.constraints.Email annotation.
Is there a special case in the absence of such validations in Spring? I think they can be used very widely anotations. We use it to validate the values when any REST API is written.
Comment From: wilkinsona
Providing and maintaining custom bean validation annotations is out of scope for Spring Boot. I would recommend raising your needs with the maintainers of Hibernate Validator or the Bean Validation spec. Thanks anyway for the suggestion.