FlowingCode / backend-core

Commons utilities for back-end enterprise features
Apache License 2.0
4 stars 1 forks source link

Improve Validation code #74

Closed javier-godoy closed 4 months ago

javier-godoy commented 1 year ago

In order torun all the validators, you need to copy-paste the following code.

List<Validator<T>> validators = ((ValidationSupport<T>) this).getValidators(DeletionValidator.class);
List<ErrorDescription> errors = validators.stream().flatMap(val->val.validate(entity).stream()).collect(Collectors.toList());
if (!errors.isEmpty()) {
    throw new DeletionValidationException(errors);
}

There should be a utility method for that purpose. For instance, in ValidationSupport add:

List<ErrorDescription> validate(Class<Validator<T>> validatorType, T t) {
    List<Validator<T>> validators = ((ValidationSupport<T>) this).getValidators(validatorType);
    return validators.stream().flatMap(val -> val.validate(t).stream()).collect(Collectors.toList());
}