All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.groupbyinc.common.util.ValidatorUtil Maven / Gradle / Ivy

There is a newer version: 198
Show newest version
package com.groupbyinc.common.util;

import com.groupbyinc.common.util.exception.ValidationException;
import org.apache.commons.collections4.CollectionUtils;

import javax.validation.ConstraintViolation;
import javax.validation.Validator;
import java.util.Set;

import static java.util.stream.Collectors.toList;

public class ValidatorUtil {

  private static final String ERR_OBJECT_IS_NULL = "Internal error";

  public static  void validate(Validator validator, T object) throws ValidationException {
    if (object == null) {
      throw new ValidationException(ERR_OBJECT_IS_NULL);
    } else {
      Set> errors = validator.validate(object);
      if (CollectionUtils.isNotEmpty(errors)) {
        throw new ValidationException(errors.stream().map(ConstraintViolation::getMessage).collect(toList()));
      }
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy