Validation rule that compares two object properties.
Implements: IValidationRule
Description
The PropertiesComparisonRule class allows you to create a validation rule used to compare two object’s properties.
Constructors
Creates a new validation rule and sets its arguments. See ObjectComparator.compare
public
PropertiesComparisonRule(String property1, String operation, String property2)
- property1: String - name of the first property to compare.
- operation: String - comparison operation: "==" ("=", “EQ”), “!= " ("<>”, “NE”); “<"/">” (“LT”/“GT”), “<="/">=” (“LE”/“GE”); “LIKE”.
- property2: String - name of the second property to compare.
Instance methods
validate
Validates a given value against this rule.
public
void validate(String path, [Schema](../schema schema, Object value, List<ValidationResult> results)
- path: String - dot notation path to the value.
- schema: Schema - schema this rule is called from
- value: Object - value to be validated.
- results: List<ValidationResult> - list with validation results to add new results.
Examples
{@code
Schema schema = new ObjectSchema().withRule(new PropertiesComparisonRule("field1", "NE", "field2"));
schema.validate(Map.of("field1", 1, "field2", 2)); // Result: no errors
schema.validate(Map.of("field1", 1, "field2", 1)); // Result: field1 shall not be equal to field2
schema.validate(Map.of()); // Result: no errors
} // Result: no errors