Class LengthValidator<T>

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

public class LengthValidator<T> extends Object implements Validator<T>
Validates string length constraints.

LengthValidator checks that a string value's length falls within specified bounds. Null values pass validation - use RequiredValidator to enforce non-null.

Usage:


 // Minimum length only
 LengthValidator<String> minLength = new LengthValidator<String>().min(3);

 // Maximum length only
 LengthValidator<String> maxLength = new LengthValidator<String>().max(100);

 // Range
 LengthValidator<String> range = new LengthValidator<String>().range(3, 50);

 // With custom message
 LengthValidator<String> password = new LengthValidator<String>()
     .min(8)
     .withMessage("Password must be at least 8 characters");

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

  • Method Details

    • min

      public LengthValidator<T> min(int minLength)
      Sets the minimum length constraint.
      Parameters:
      minLength - The minimum allowed length (inclusive)
      Returns:
      This validator for method chaining
    • max

      public LengthValidator<T> max(int maxLength)
      Sets the maximum length constraint.
      Parameters:
      maxLength - The maximum allowed length (inclusive)
      Returns:
      This validator for method chaining
    • range

      public LengthValidator<T> range(int minLength, int maxLength)
      Sets both minimum and maximum length constraints.
      Parameters:
      minLength - The minimum allowed length (inclusive)
      maxLength - The maximum allowed length (inclusive)
      Returns:
      This validator for method chaining
    • 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 LengthValidator<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
    • getMinLength

      public Integer getMinLength()
      Returns the minimum length constraint, if set.
      Returns:
      The minimum length, or null if not set
    • getMaxLength

      public Integer getMaxLength()
      Returns the maximum length constraint, if set.
      Returns:
      The maximum length, or null if not set