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

  1. Jakarta's @Email implementation misses valid email formats
  2. Doesn't properly validate many legitimate email addresses
  3. Lacks comprehensive validation based on latest standards
  4. Doesn't provide detailed validation error messages

Proposed Solution

We propose reintroducing Spring's @Email annotation with an enhanced validation implementation that:

  1. Follows RFC 5322 standards completely
  2. Provides more comprehensive validation
  3. Handles special cases properly
  4. Offers better error messages
  5. 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

  1. Comprehensive regex pattern that properly validates:
  2. Local part restrictions
  3. Domain part validation
  4. Special characters handling
  5. TLD validation
  6. Detailed validation messages
  7. Extensive test coverage
  8. Better performance than current implementation

Benefits

  1. More accurate email validation
  2. Better developer experience
  3. Reduced validation bypass risks
  4. Backward compatibility with existing code
  5. 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.