Class ValidationResult

java.lang.Object
com.oorian.validation.ValidationResult

public class ValidationResult extends Object
Represents the result of a validation operation.

ValidationResult encapsulates whether a validation passed or failed, along with any messages produced during validation. Results can contain multiple messages of different severity levels.

Usage:


 // Return a valid result
 return ValidationResult.valid();

 // Return an invalid result with error message
 return ValidationResult.invalid("Email is required");

 // Return a valid result with a warning
 return ValidationResult.warning("This email domain is unusual");

 // Check result
 ValidationResult result = validator.validate(value, context);
 if (result.isValid()) {
     // proceed
 } else {
     String error = result.getFirstError();
     // display error
 }
 
Since:
2025
Version:
1.0
Author:
Marvin P. Warble Jr.
See Also:
  • Field Details

    • VALID

      public static final ValidationResult VALID
      A singleton instance representing a valid result with no messages.
  • Constructor Details

    • ValidationResult

      public ValidationResult(boolean valid, List<ValidationMessage> messages)
      Creates a validation result.
      Parameters:
      valid - Whether the validation passed
      messages - The list of validation messages
  • Method Details

    • valid

      public static ValidationResult valid()
      Returns a valid result with no messages.
      Returns:
      A valid ValidationResult
    • invalid

      public static ValidationResult invalid(String message)
      Returns an invalid result with the specified error message.
      Parameters:
      message - The error message
      Returns:
      An invalid ValidationResult with one error message
    • invalid

      public static ValidationResult invalid(String message, String fieldName)
      Returns an invalid result with the specified error message for a specific field.
      Parameters:
      message - The error message
      fieldName - The field name
      Returns:
      An invalid ValidationResult with one field-specific error message
    • warning

      public static ValidationResult warning(String message)
      Returns a valid result with a warning message.

      Warnings do not cause validation to fail but provide feedback to the user.

      Parameters:
      message - The warning message
      Returns:
      A valid ValidationResult with one warning message
    • info

      public static ValidationResult info(String message)
      Returns a valid result with an informational message.
      Parameters:
      message - The informational message
      Returns:
      A valid ValidationResult with one info message
    • isValid

      public boolean isValid()
      Returns whether the validation passed.
      Returns:
      true if validation passed, false if it failed
    • isInvalid

      public boolean isInvalid()
      Returns whether the validation failed.
      Returns:
      true if validation failed, false if it passed
    • getMessages

      public List<ValidationMessage> getMessages()
      Returns the list of validation messages.
      Returns:
      An unmodifiable list of validation messages
    • hasMessages

      public boolean hasMessages()
      Returns whether this result has any messages.
      Returns:
      true if there are messages
    • getFirstError

      public String getFirstError()
      Returns the first error message, or null if there are no errors.
      Returns:
      The first error message text, or null
    • getErrors

      public List<String> getErrors()
      Returns all error messages.
      Returns:
      A list of error message texts
    • getFirstWarning

      public String getFirstWarning()
      Returns the first warning message, or null if there are no warnings.
      Returns:
      The first warning message text, or null
    • getWarnings

      public List<String> getWarnings()
      Returns all warning messages.
      Returns:
      A list of warning message texts
    • merge

      public ValidationResult merge(ValidationResult other)
      Merges this result with another result.

      The merged result is valid only if both results are valid. Messages from both results are combined.

      Parameters:
      other - The other result to merge with
      Returns:
      A new ValidationResult combining both results
    • toString

      public String toString()
      Overrides:
      toString in class Object