I have a small set of validation classes which I have created which have served me well, however now I need to update them to handle prioritized rules. If a high priority rule is met, then I don’t need to run any further validations, as we will just tell the user one single error message instead of adding the entire set of messages to the user.
Here is the set of classes I have:
//Rule.java
public interface Rule<T> {
List<ErrorMessage> validate(T value);
}
//ValidationStrategy.java
public interface ValidationStrategy<T> {
public List<Rule<? super T>> getRules();
}
//Validator.java
public class Validator<T> implements Rule<T> {
private List<Rule<? super T>> tests = new ArrayList<Rule<? super T>>();
public Validator(ValidationStrategy<T> type) {
this.tests = type.getRules();
}
public List<ErrorMessage> validate(T value) {
List <ErrorMessage> errors = new ArrayList<ErrorMessage>();
for (Rule<? super T> rule : tests) {
errors.addAll(rule.check(value));
}
return errors;
}
}
I am having some trouble modifying this code to deal with prioritized rules. Surely there is something out there that I can modify to use instead of bringing in a Rules Engine.
Ideally I’d then be able to create rules like this:
private static final Rule<SomeClass> ensureAllFieldsNotBlank = new Rule<SomeClass>(RulePriority.HIGHEST) {
public List<ErrorMessage> check(SomeClass someClass) {
List<ErrorMessage> errors = new ArrayList<ErrorMessage>();
if (StringUtils.isBlank(someClass.getValue1())
&& StringUtils.isBlank(someClass.getValue2())
&& StringUtils.isBlank(someClass.getValue3())) {
errors.add("Provide a response for \"" + someClass.getName() + "\"");
}
return errors;
}
};
Edit to updated classes:
//ValidationStrategy.java
public interface ValidationStrategy<T> {
public List<Rule<? super T>> getRules(RulePriority rulePriority);
}
//RulePriority.java
public enum RulePriority { HIGHEST, DEFAULT, LOWEST; }
//Validator.java
public class Validator<T> implements Rule<T> {
private List<Rule<? super T>> tests = new ArrayList<Rule<? super T>>();
private ValidationStrategy<T> validationStrategy;
public Validator(ValidationStrategy<T> validationStrategy) {
this.validationStrategy = validationStrategy;
for (RulePriority rp : RulePriority.values()) {
this.tests.addAll(validationStrategy.getRules(rulePriority));
}
}
public List<ErrorMessage> validate(T value) {
List<ErrorMessage> errors = new ArrayList<String>();
for (RulePriority rp : RulePriority.values()) {
for (Rule<? super T> rule : validationStrategy.getRules(rp)) {
errors.addAll(rule.validate(value));
}
if (errors.size() > 0) {
break;
}
}
return errors;
}
How about creating an abstract base class to handle rule comparisons:
From there, your PrioritizedValidator (which requires PrioritizedRule) will sort() its collection in the start of Validate (if its collection of rules has been modified since the last validate; that’s the right time to sort the collection, since we don’t want to repeat the sort on every modification if there are consecutive modifications, or do a sort if we don’t need to), and the Validate loop should early-out if its list of error messages is non-empty across a transition between rule priorities:
I’m not sure what you mean by “…without bringing in a rules engine”, but defining rules to sort themselves is probably the most graceful approach. Be careful, though- any two PrioritizedRule-s will have to be comparable to each other, which is why I’m recommending PrioritizedRule be an abstract base instead of an interface, because that’s where the compareTo implementation needs to live, for consistency. Your compareTo does not need to be consistent with equals unless you try to keep your collection in a sorted set, which can never end well (PrioritizedRule can’t be aware of enough to make itself consistent with equals!), so don’t try.
Alternatively, implement a Comparator>, but again, your Rule interface will need to be modified to expose enough information to sort with.