package com.gci.boot.common.util;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author 24642
 */
public class ValidateUtil {
    public static <T> String valid(T t,Class<?> ...groups) {
        ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
        Validator validator = validatorFactory.getValidator();
        Set<ConstraintViolation<T>> validate = validator.validate(t, groups);

        return validate.stream().map(ConstraintViolation::getMessage).collect(Collectors.joining(" "));
    }
}
