Skip to content

Validator

johnmcclean-aol edited this page Nov 21, 2016 · 2 revisions

Validator allows a set of validation parameters to be lazily configured and then executed against a target to be validated. Errors can either be accumulated or validation can fail on error.

While Xor (or even Try) can be used to store validation results, Validator is an active validator that executes the supplied validation rules.

Accumulate

ValidationResults results  = CumulativeValidator.of((User user)->user.age>18, "too young", "age ok")
					                            .isValid(user->user.email!=null, "user email null","email ok")
												.accumulate(new User(10,"email@email.com"));
	
assertThat(results.getResults().size(),equalTo(2));

Accumulate until fail

ValidationResults<String,String> results  = CumulativeValidator.of((User user)->user.age>18, "too young", "age ok")
												.add(Validator.of((User user)->user.email!=null, "user email null","email ok"))
												.accumulateUntilFail(new User(10,"email@email.com"));
	
assertThat(results.getResults().size(),equalTo(1));
Clone this wiki locally