create custom validation for Email

person shubham sharmafolder_openJAVA, Spring Bootlocal_offer, access_time November 17, 2024

To create a custom email validation in Spring Boot, you can follow these steps:

  1. Create a Custom Annotation for Email Validation.
  2. Create a Validator Class to Implement the Validation Logic.
  3. Use the Custom Annotation in Your Model Class.

Let’s walk through the process step by step.


Step 1: Create a Custom Annotation

First, you need to create a custom annotation that will be used to validate email addresses.

Explanation:

  • @Constraint: Specifies the validator class (EmailValidator), which will contain the logic for validating the email.
  • @Target: Specifies where this annotation can be applied (e.g., field, method parameter, etc.).
  • @Retention: Specifies that the annotation should be available at runtime.

Step 2: Create the Validator Class

Now, create a validator class that implements the ConstraintValidator interface to define the validation logic.

Explanation:

  • ConstraintValidator<ValidEmail, String>: This tells Spring that this validator will validate fields annotated with @ValidEmail and those fields will be of type String.
  • EMAIL_PATTERN: A regular expression pattern that matches valid email addresses.
  • isValid: The method where the actual validation logic happens. It returns true if the email is valid, false otherwise.

Step 3: Use the Custom Annotation in Your Model

Now that you have the custom annotation and validator, you can use it in your model class.

In this example, the @ValidEmail annotation is applied to the email field of UserDTO. This ensures that when the UserDTO object is validated, the email format will be checked.


Step 4: Enable Validation in the Controller

You need to make sure that the Spring Boot application will actually trigger the validation. For this, you can use the @Valid annotation in your controller to ensure the input data is validated.

Explanation:

  • @Valid: This annotation tells Spring to validate the incoming UserDTO object based on the annotations (like @ValidEmail) before the method executes.
  • If the email is invalid, Spring will return an error response.

Step 5: Handle Validation Errors (Optional)

If you want to customize the error response when validation fails, you can create a @ControllerAdvice to handle validation exceptions.

Explanation:

  • This advice class listens for validation exceptions (MethodArgumentNotValidException).
  • It then extracts the validation errors and returns a 400 Bad Request response with a detailed error message.

Final Example Request and Response

Request:

Response:


Summary:

  1. Create a custom annotation (@ValidEmail) that points to a validator.
  2. Create a validator class (EmailValidator) that implements the validation logic.
  3. Use the custom annotation in your model class (e.g., UserDTO).
  4. Enable validation in your controller by using @Valid.
  5. (Optional) Handle validation errors using @ControllerAdvice.

By following these steps, you can create custom validations like email format checks in a clean, reusable, and maintainable way.

warningComments are closed.