Class RequiredValidator<T>

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

public class RequiredValidator<T> extends Object implements Validator<T>
Validates that a value is present and not empty.

RequiredValidator checks that a value is non-null and, for String values, not empty. By default, blank strings (whitespace only) are also considered invalid.

Usage:


 // Basic required validation
 RequiredValidator<String> validator = new RequiredValidator<>();
 validator.validate(null, context);     // Invalid
 validator.validate("", context);       // Invalid
 validator.validate("   ", context);    // Invalid (blank)
 validator.validate("hello", context);  // Valid

 // With custom message
 RequiredValidator<String> emailRequired = new RequiredValidator<String>()
     .withMessage("Email address is required");

 // Allow blank values (whitespace-only strings pass)
 RequiredValidator<String> allowBlank = new RequiredValidator<String>()
     .allowBlank();
 validator.validate("   ", context);    // Valid when allowBlank()
 
Since:
2025
Version:
1.0
Author:
Marvin P. Warble Jr.
See Also:
  • Constructor Details

    • RequiredValidator

      public RequiredValidator()
      Creates a RequiredValidator with default settings.
    • RequiredValidator

      public RequiredValidator(String message)
      Creates a RequiredValidator 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 RequiredValidator<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
    • allowBlank

      public RequiredValidator<T> allowBlank()
      Allows blank (whitespace-only) String values to pass validation.

      By default, strings containing only whitespace are considered invalid. Calling this method changes that behavior to allow such values.

      Returns:
      This validator for method chaining
    • isBlankAllowed

      public boolean isBlankAllowed()
      Returns whether blank strings are allowed.
      Returns:
      true if blank strings pass validation