use of jakarta.validation.ConstraintViolation in project hibernate-orm by hibernate.
the class BeanValidationEventListener method validate.
private <T> void validate(T object, RepresentationMode mode, EntityPersister persister, SessionFactoryImplementor sessionFactory, GroupsPerOperation.Operation operation) {
if (object == null || mode != RepresentationMode.POJO) {
return;
}
TraversableResolver tr = new HibernateTraversableResolver(persister, associationsPerEntityPersister, sessionFactory);
Validator validator = factory.usingContext().traversableResolver(tr).getValidator();
final Class<?>[] groups = groupsPerOperation.get(operation);
if (groups.length > 0) {
final Set<ConstraintViolation<T>> constraintViolations = validator.validate(object, groups);
if (constraintViolations.size() > 0) {
Set<ConstraintViolation<?>> propagatedViolations = CollectionHelper.setOfSize(constraintViolations.size());
Set<String> classNames = new HashSet<>();
for (ConstraintViolation<?> violation : constraintViolations) {
LOG.trace(violation);
propagatedViolations.add(violation);
classNames.add(violation.getLeafBean().getClass().getName());
}
StringBuilder builder = new StringBuilder();
builder.append("Validation failed for classes ");
builder.append(classNames);
builder.append(" during ");
builder.append(operation.getName());
builder.append(" time for groups ");
builder.append(toString(groups));
builder.append("\nList of constraint violations:[\n");
for (ConstraintViolation<?> violation : constraintViolations) {
builder.append("\t").append(violation.toString()).append("\n");
}
builder.append("]");
throw new ConstraintViolationException(builder.toString(), propagatedViolations);
}
}
}
use of jakarta.validation.ConstraintViolation in project gocd-groovy-dsl-config-plugin by gocd-contrib.
the class TestBase method validate.
protected static void validate(Object object, Consumer<Set<ConstraintViolation<Object>>> errorHandler) {
ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
Validator validator = validatorFactory.getValidator();
Set<ConstraintViolation<Object>> violations = validator.validate(object);
if (!violations.isEmpty()) {
errorHandler.accept(violations);
}
}
use of jakarta.validation.ConstraintViolation in project gocd-groovy-dsl-config-plugin by gocd-contrib.
the class Validator method validate.
public static <T> void validate(T node, Consumer<Set<ConstraintViolation<T>>> errorHandler) {
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
final Set<ConstraintViolation<T>> errors = factory.getValidator().validate(node);
if (!errors.isEmpty()) {
errorHandler.accept(errors);
}
}
use of jakarta.validation.ConstraintViolation in project resteasy by resteasy.
the class GeneralValidatorImpl method alreadyFoundClassOrPropertyConstraint.
private static boolean alreadyFoundClassOrPropertyConstraint(SimpleViolationsContainer container) {
Set<ConstraintViolation<Object>> set = container.getViolations();
if (set.isEmpty()) {
return false;
}
Iterator<ConstraintViolation<Object>> it = set.iterator();
for (ConstraintViolation<?> cv = it.next(); it.hasNext(); cv = it.next()) {
ConstraintType.Type type = util.getConstraintType(cv);
if ((ConstraintType.Type.CLASS.equals(type) || ConstraintType.Type.PROPERTY.equals(type))) {
return true;
}
}
return false;
}
use of jakarta.validation.ConstraintViolation in project resteasy by resteasy.
the class GeneralValidatorImpl method validateAllParameters.
@Override
public void validateAllParameters(HttpRequest request, Object object, Method method, Object[] parameterValues, Class<?>... groups) {
if (method.getParameterCount() == 0) {
checkViolations(request);
return;
}
Validator validator = getValidator(request);
SimpleViolationsContainer violationsContainer = getViolationsContainer(request, object);
Set<ConstraintViolation<Object>> cvs = null;
try {
cvs = validator.forExecutables().validateParameters(object, method, parameterValues, groups);
} catch (Exception e) {
violationsContainer.setException(e);
throw toValidationException(e, violationsContainer);
}
violationsContainer.addViolations(cvs);
if ((violationsContainer.isFieldsValidated() || !GetRestful.isRootResource(object.getClass()) || hasApplicationScope(object)) && violationsContainer.size() > 0) {
throw new ResteasyViolationExceptionImpl(violationsContainer, request.getHttpHeaders().getAcceptableMediaTypes());
}
}
Aggregations