Package com.oorian.validation.validators
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>
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionReturns the maximum length constraint, if set.Returns the default error message for this validator.Returns the minimum length constraint, if set.max(int maxLength) Sets the maximum length constraint.min(int minLength) Sets the minimum length constraint.range(int minLength, int maxLength) Sets both minimum and maximum length constraints.validate(T value, ValidationContext context) Validates the given value.withMessage(String message) Creates a new validator with a custom error message.
-
Constructor Details
-
LengthValidator
public LengthValidator()Creates a LengthValidator with no constraints.Use
min(int),max(int), orrange(int, int)to set constraints.
-
-
Method Details
-
min
Sets the minimum length constraint.- Parameters:
minLength- The minimum allowed length (inclusive)- Returns:
- This validator for method chaining
-
max
Sets the maximum length constraint.- Parameters:
maxLength- The maximum allowed length (inclusive)- Returns:
- This validator for method chaining
-
range
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
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
-
getMinLength
Returns the minimum length constraint, if set.- Returns:
- The minimum length, or null if not set
-
getMaxLength
Returns the maximum length constraint, if set.- Returns:
- The maximum length, or null if not set
-