Package com.oorian.validation.validators
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>
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 Summary
ConstructorsConstructorDescriptionCreates a RequiredValidator with default settings.RequiredValidator(String message) Creates a RequiredValidator with a custom message. -
Method Summary
Modifier and TypeMethodDescriptionAllows blank (whitespace-only) String values to pass validation.Returns the default error message for this validator.booleanReturns whether blank strings are allowed.validate(T value, ValidationContext context) Validates the given value.withMessage(String message) Creates a new validator with a custom error message.
-
Constructor Details
-
RequiredValidator
public RequiredValidator()Creates a RequiredValidator with default settings. -
RequiredValidator
Creates a RequiredValidator with a custom message.- Parameters:
message- The error message
-
-
Method Details
-
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
-
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
-