Class EmailValidator<T>

java.lang.Object
com.oorian.validation.validators.EmailValidator<T>
Type Parameters:
T - The type of value being validated (typically String)
All Implemented Interfaces:
Validator<T>

public class EmailValidator<T> extends Object implements Validator<T>
Validates email address format.

EmailValidator checks that a value matches a standard email address format. Null and empty values pass validation - use RequiredValidator to enforce non-null.

The validation uses a reasonably strict pattern that accepts most valid email addresses while rejecting obviously invalid ones. For mission-critical email validation, consider following up with an email verification service.

Usage:


 // Basic email validation
 EmailValidator<String> validator = new EmailValidator<>();
 validator.validate("user@example.com", context);   // Valid
 validator.validate("invalid-email", context);       // Invalid
 validator.validate("", context);                    // Valid (empty)
 validator.validate(null, context);                  // Valid (null)

 // With custom message
 EmailValidator<String> custom = new EmailValidator<String>()
     .withMessage("Please enter a valid email address");

 // Fluent in ValidatedInput
 new ValidatedInput<>(input, errorSpan)
     .required()
     .email();
 
Since:
2025
Version:
1.0
Author:
Marvin P. Warble Jr.
See Also:
  • Constructor Details

    • EmailValidator

      public EmailValidator()
      Creates an EmailValidator with the default message.
    • EmailValidator

      public EmailValidator(String message)
      Creates an EmailValidator with a custom message.
      Parameters:
      message - The error message
  • Method Details

    • validate

      public ValidationResult validate(T value, ValidationContext context)
      Description copied from interface: Validator
      Validates the given value.

      Implementations should return ValidationResult.valid() when the value passes validation, or ValidationResult.invalid(String) with an appropriate message when it fails.

      Validators typically should not fail on null values - use RequiredValidator to enforce non-null requirements. This allows validators to be composed without unexpected failures.

      Specified by:
      validate in interface Validator<T>
      Parameters:
      value - The value to validate (may be null)
      context - The validation context for accessing other field values
      Returns:
      The validation result
    • getMessage

      public String getMessage()
      Description copied from interface: Validator
      Returns the default error message for this validator.
      Specified by:
      getMessage in interface Validator<T>
      Returns:
      The error message
    • withMessage

      public EmailValidator<T> withMessage(String message)
      Description copied from interface: Validator
      Creates a new validator with a custom error message.

      This method returns a new instance with the custom message, leaving the original validator unchanged.

      Specified by:
      withMessage in interface Validator<T>
      Parameters:
      message - The custom error message
      Returns:
      A new validator instance with the custom message