Package com.oorian.validation
Class ValidationResult
java.lang.Object
com.oorian.validation.ValidationResult
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 Summary
FieldsModifier and TypeFieldDescriptionstatic final ValidationResultA singleton instance representing a valid result with no messages. -
Constructor Summary
ConstructorsConstructorDescriptionValidationResult(boolean valid, List<ValidationMessage> messages) Creates a validation result. -
Method Summary
Modifier and TypeMethodDescriptionReturns all error messages.Returns the first error message, or null if there are no errors.Returns the first warning message, or null if there are no warnings.Returns the list of validation messages.Returns all warning messages.booleanReturns whether this result has any messages.static ValidationResultReturns a valid result with an informational message.static ValidationResultReturns an invalid result with the specified error message.static ValidationResultReturns an invalid result with the specified error message for a specific field.booleanReturns whether the validation failed.booleanisValid()Returns whether the validation passed.merge(ValidationResult other) Merges this result with another result.toString()static ValidationResultvalid()Returns a valid result with no messages.static ValidationResultReturns a valid result with a warning message.
-
Field Details
-
VALID
A singleton instance representing a valid result with no messages.
-
-
Constructor Details
-
ValidationResult
Creates a validation result.- Parameters:
valid- Whether the validation passedmessages- The list of validation messages
-
-
Method Details
-
valid
Returns a valid result with no messages.- Returns:
- A valid ValidationResult
-
invalid
Returns an invalid result with the specified error message.- Parameters:
message- The error message- Returns:
- An invalid ValidationResult with one error message
-
invalid
Returns an invalid result with the specified error message for a specific field.- Parameters:
message- The error messagefieldName- The field name- Returns:
- An invalid ValidationResult with one field-specific error message
-
warning
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
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
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
Returns the first error message, or null if there are no errors.- Returns:
- The first error message text, or null
-
getErrors
Returns all error messages.- Returns:
- A list of error message texts
-
getFirstWarning
Returns the first warning message, or null if there are no warnings.- Returns:
- The first warning message text, or null
-
getWarnings
Returns all warning messages.- Returns:
- A list of warning message texts
-
merge
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
-