I’m often finding the need to validate a set of conditions, and instead of failing early (returning false or throwing an exception when the first condition is not fulfilled), I need to aggregate the results and report the individual failures.
I’m currently either using a list with custom entries (basically an entry consists of the type of the failure and some informative message) or some kind of observer (which also just aggregates the failures), but I have a feeling that this should be a common problem and that there should be some existing pattern for solving this.
Yes, it is a common problem, and both your approaches are good.
javax.validation.Validator, which is the standard for java validation, uses the former. It returns aSetofConstraintViolationssIf it fits your case, I would recommend using
javax.validationinstead of something custom. It’s a spec with multiple providers, one of which is hibernate-validator (no need to use hibernate to use the validation project)