Class ValidationContext

java.lang.Object
com.oorian.validation.ValidationContext

public class ValidationContext extends Object
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 Details

    • ValidationContext

      public ValidationContext()
      Creates an empty validation context.
    • ValidationContext

      public ValidationContext(Map<String,Object> fieldValues)
      Creates a validation context with the specified field values.
      Parameters:
      fieldValues - A map of field names to their current values
    • ValidationContext

      public ValidationContext(Map<String,Object> fieldValues, String currentFieldName)
      Creates a validation context with field values and current field name.
      Parameters:
      fieldValues - A map of field names to their current values
      currentFieldName - The name of the field currently being validated
  • Method Details

    • getFieldValue

      public Object getFieldValue(String fieldName)
      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

      public <T> T getFieldValue(String fieldName, Class<T> type)
      Gets the value of a field as a specific type.
      Type Parameters:
      T - The expected type
      Parameters:
      fieldName - The name of the field
      type - The expected type class
      Returns:
      The field value cast to the type, or null if not found or wrong type
    • getFieldValueAsString

      public String getFieldValueAsString(String fieldName)
      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

      public String getCurrentFieldName()
      Returns the name of the field currently being validated.
      Returns:
      The current field name, or null if not set
    • hasField

      public boolean hasField(String fieldName)
      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

      public Iterable<String> getFieldNames()
      Returns all field names in this context.
      Returns:
      An iterable of field names
    • withField

      public ValidationContext withField(String fieldName, Object value)
      Creates a new context with an additional field value.
      Parameters:
      fieldName - The name of the field to add
      value - The value of the field
      Returns:
      A new ValidationContext with the additional field
    • forField

      public ValidationContext forField(String fieldName)
      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