Package com.oorian.validation.validators
Class UrlValidator<T>
java.lang.Object
com.oorian.validation.validators.UrlValidator<T>
- Type Parameters:
T- The type of value being validated (typically String)
- All Implemented Interfaces:
Validator<T>
Validates URL format.
UrlValidator checks that a value is a valid URL with optional protocol restrictions.
Null and empty values pass validation - use RequiredValidator to enforce non-null.
Usage:
// Basic URL validation (http and https)
UrlValidator<String> validator = new UrlValidator<>();
validator.validate("https://example.com", context); // Valid
validator.validate("ftp://files.example.com", context); // Invalid (ftp not allowed by default)
// Require HTTPS
UrlValidator<String> httpsOnly = new UrlValidator<String>()
.requireHttps()
.withMessage("Please enter a secure (HTTPS) URL");
// Allow specific protocols
UrlValidator<String> ftpAllowed = new UrlValidator<String>()
.allowProtocols("http", "https", "ftp")
.withMessage("Please enter a valid URL");
// Fluent in ValidatedInput
new ValidatedInput<>(input, errorSpan)
.required()
.url();
- Since:
- 2025
- Version:
- 1.0
- Author:
- Marvin P. Warble Jr.
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a UrlValidator with default settings (allows http and https).UrlValidator(String message) Creates a UrlValidator with a custom message. -
Method Summary
Modifier and TypeMethodDescriptionallowProtocols(String... protocols) Sets the allowed protocols.Returns the set of allowed protocols.Returns the default error message for this validator.booleanReturns whether HTTPS is required.Configures the validator to require HTTPS protocol only.validate(T value, ValidationContext context) Validates the given value.withMessage(String message) Creates a new validator with a custom error message.
-
Constructor Details
-
UrlValidator
public UrlValidator()Creates a UrlValidator with default settings (allows http and https). -
UrlValidator
Creates a UrlValidator with a custom message.- Parameters:
message- The error message
-
-
Method Details
-
requireHttps
Configures the validator to require HTTPS protocol only.- Returns:
- This validator for method chaining
-
allowProtocols
Sets the allowed protocols.- Parameters:
protocols- The protocols to allow (e.g., "http", "https", "ftp")- 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
-
isHttpsRequired
public boolean isHttpsRequired()Returns whether HTTPS is required.- Returns:
- true if HTTPS is required
-
getAllowedProtocols
Returns the set of allowed protocols.- Returns:
- The allowed protocols
-