Package com.oorian.validation
Class ValidationContext
java.lang.Object
com.oorian.validation.ValidationContext
Provides context for validation, including access to other field values.
ValidationContext is primarily used for cross-field validation scenarios, such as confirming a password matches, comparing dates, or validating that one field's value is dependent on another's.
Usage:
// In a cross-field validator
public ValidationResult validate(String value, ValidationContext context) {
String otherValue = context.getFieldValue("password", String.class);
if (!value.equals(otherValue)) {
return ValidationResult.invalid("Passwords must match");
}
return ValidationResult.valid();
}
// Creating a context for form validation
Map<String, Object> values = new HashMap<>();
values.put("email", emailInput.getValue());
values.put("password", passwordInput.getValue());
ValidationContext context = new ValidationContext(values);
- Since:
- 2025
- Version:
- 1.0
- Author:
- Marvin P. Warble Jr.
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionCreates an empty validation context.ValidationContext(Map<String, Object> fieldValues) Creates a validation context with the specified field values.ValidationContext(Map<String, Object> fieldValues, String currentFieldName) Creates a validation context with field values and current field name. -
Method Summary
Modifier and TypeMethodDescriptionCreates a new context for validating a specific field.Returns the name of the field currently being validated.Returns all field names in this context.getFieldValue(String fieldName) Gets the value of a field by name.<T> TgetFieldValue(String fieldName, Class<T> type) Gets the value of a field as a specific type.getFieldValueAsString(String fieldName) Gets the value of a field as a String.booleanReturns whether this context contains a value for the specified field.Creates a new context with an additional field value.
-
Constructor Details
-
ValidationContext
public ValidationContext()Creates an empty validation context. -
ValidationContext
Creates a validation context with the specified field values.- Parameters:
fieldValues- A map of field names to their current values
-
ValidationContext
Creates a validation context with field values and current field name.- Parameters:
fieldValues- A map of field names to their current valuescurrentFieldName- The name of the field currently being validated
-
-
Method Details
-
getFieldValue
Gets the value of a field by name.- Parameters:
fieldName- The name of the field- Returns:
- The field value, or null if not found
-
getFieldValue
Gets the value of a field as a specific type.- Type Parameters:
T- The expected type- Parameters:
fieldName- The name of the fieldtype- The expected type class- Returns:
- The field value cast to the type, or null if not found or wrong type
-
getFieldValueAsString
Gets the value of a field as a String.If the value is not a String but is non-null, toString() is called.
- Parameters:
fieldName- The name of the field- Returns:
- The field value as a String, or null if not found
-
getCurrentFieldName
Returns the name of the field currently being validated.- Returns:
- The current field name, or null if not set
-
hasField
Returns whether this context contains a value for the specified field.- Parameters:
fieldName- The name of the field- Returns:
- true if the context contains a value for the field
-
getFieldNames
Returns all field names in this context.- Returns:
- An iterable of field names
-
withField
Creates a new context with an additional field value.- Parameters:
fieldName- The name of the field to addvalue- The value of the field- Returns:
- A new ValidationContext with the additional field
-
forField
Creates a new context for validating a specific field.- Parameters:
fieldName- The name of the field being validated- Returns:
- A new ValidationContext with the current field name set
-