Class RangeValidator<T>
- Type Parameters:
T- The type of value being validated (Number or String)
- All Implemented Interfaces:
Validator<T>
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 Summary
ConstructorsConstructorDescriptionCreates a RangeValidator with no constraints.RangeValidator(Number min, Number max) Creates a RangeValidator with the specified minimum and maximum. -
Method Summary
Modifier and TypeMethodDescriptiongetMax()Returns the maximum value constraint, if set.Returns the default error message for this validator.getMin()Returns the minimum value constraint, if set.Sets the maximum value constraint.Sets the minimum value constraint.Sets both minimum and maximum value constraints.validate(T value, ValidationContext context) Validates the given value.withMessage(String message) Creates a new validator with a custom error message.
-
Constructor Details
-
RangeValidator
public RangeValidator()Creates a RangeValidator with no constraints.Use
min(Number),max(Number), orrange(Number, Number)to set constraints. -
RangeValidator
Creates a RangeValidator with the specified minimum and maximum.- Parameters:
min- The minimum value (inclusive)max- The maximum value (inclusive)
-
-
Method Details
-
min
Sets the minimum value constraint.- Parameters:
min- The minimum allowed value (inclusive)- Returns:
- This validator for method chaining
-
max
Sets the maximum value constraint.- Parameters:
max- The maximum allowed value (inclusive)- Returns:
- This validator for method chaining
-
range
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
Description copied from interface:ValidatorValidates the given value.Implementations should return
ValidationResult.valid()when the value passes validation, orValidationResult.invalid(String)with an appropriate message when it fails.Validators typically should not fail on null values - use
RequiredValidatorto enforce non-null requirements. This allows validators to be composed without unexpected failures. -
getMessage
Description copied from interface:ValidatorReturns the default error message for this validator.- Specified by:
getMessagein interfaceValidator<T>- Returns:
- The error message
-
withMessage
Description copied from interface:ValidatorCreates 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:
withMessagein interfaceValidator<T>- Parameters:
message- The custom error message- Returns:
- A new validator instance with the custom message
-
getMin
Returns the minimum value constraint, if set.- Returns:
- The minimum value, or null if not set
-
getMax
Returns the maximum value constraint, if set.- Returns:
- The maximum value, or null if not set
-