io.alauda.kubernetes.api.builder.ValidationUtils Maven / Gradle / Ivy
package io.alauda.kubernetes.api.builder;
import javax.validation.Validation;
import java.util.Set;
import javax.validation.ValidationException;
import javax.validation.ValidatorFactory;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import javax.validation.Validator;
public final class ValidationUtils{
public static void validate(T item){
Validator validator = null;
try {
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
validator = factory.getValidator();
} catch (ValidationException e) {
return;
}
Set> violations = validator.validate(item);
if (!violations.isEmpty()) {
StringBuilder sb = new StringBuilder("Constraint Validations: ");
boolean first = true;
for (ConstraintViolation violation : violations) {
if (first) {
first = false;
} else {
sb.append(", ");
}
Object leafBean = violation.getLeafBean();
sb.append(violation.getPropertyPath() + " " + violation.getMessage() + " on bean: " + leafBean);
}
throw new ConstraintViolationException(sb.toString(), violations);
}
}
}