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>

public class UrlValidator<T> extends Object implements 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 Details

    • UrlValidator

      public UrlValidator()
      Creates a UrlValidator with default settings (allows http and https).
    • UrlValidator

      public UrlValidator(String message)
      Creates a UrlValidator with a custom message.
      Parameters:
      message - The error message
  • Method Details

    • requireHttps

      public UrlValidator<T> requireHttps()
      Configures the validator to require HTTPS protocol only.
      Returns:
      This validator for method chaining
    • allowProtocols

      public UrlValidator<T> allowProtocols(String... protocols)
      Sets the allowed protocols.
      Parameters:
      protocols - The protocols to allow (e.g., "http", "https", "ftp")
      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 UrlValidator<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
    • isHttpsRequired

      public boolean isHttpsRequired()
      Returns whether HTTPS is required.
      Returns:
      true if HTTPS is required
    • getAllowedProtocols

      public Set<String> getAllowedProtocols()
      Returns the set of allowed protocols.
      Returns:
      The allowed protocols