Class RangeValidator<T>

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

public class RangeValidator<T> extends Object implements Validator<T>
Validates that a numeric value is within a specified range.

RangeValidator checks that a numeric value falls within the specified minimum and/or maximum bounds. The validator accepts Number values directly, or parses String values as numbers. Null values pass validation - use RequiredValidator to enforce non-null.

Usage:


 // Range with both bounds
 RangeValidator<Number> age = new RangeValidator<>(18, 120);

 // Minimum only
 RangeValidator<Number> positive = new RangeValidator<Number>().min(0);

 // Maximum only
 RangeValidator<Number> percentage = new RangeValidator<Number>().max(100);

 // With custom message
 RangeValidator<Number> score = new RangeValidator<Number>()
     .range(0, 100)
     .withMessage("Score must be between 0 and 100");

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

    • RangeValidator

      public RangeValidator()
      Creates a RangeValidator with no constraints.

      Use min(Number), max(Number), or range(Number, Number) to set constraints.

    • RangeValidator

      public RangeValidator(Number min, Number max)
      Creates a RangeValidator with the specified minimum and maximum.
      Parameters:
      min - The minimum value (inclusive)
      max - The maximum value (inclusive)
  • Method Details

    • min

      public RangeValidator<T> min(Number min)
      Sets the minimum value constraint.
      Parameters:
      min - The minimum allowed value (inclusive)
      Returns:
      This validator for method chaining
    • max

      public RangeValidator<T> max(Number max)
      Sets the maximum value constraint.
      Parameters:
      max - The maximum allowed value (inclusive)
      Returns:
      This validator for method chaining
    • range

      public RangeValidator<T> range(Number min, Number max)
      Sets both minimum and maximum value constraints.
      Parameters:
      min - The minimum allowed value (inclusive)
      max - The maximum allowed value (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 RangeValidator<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
    • getMin

      public Number getMin()
      Returns the minimum value constraint, if set.
      Returns:
      The minimum value, or null if not set
    • getMax

      public Number getMax()
      Returns the maximum value constraint, if set.
      Returns:
      The maximum value, or null if not set