Search in sources :

Example 11 with ConstraintViolation

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);
        }
    }
}
Also used : TraversableResolver(jakarta.validation.TraversableResolver) ConstraintViolation(jakarta.validation.ConstraintViolation) ConstraintViolationException(jakarta.validation.ConstraintViolationException) Validator(jakarta.validation.Validator) HashSet(java.util.HashSet)

Example 12 with ConstraintViolation

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);
    }
}
Also used : ValidatorFactory(jakarta.validation.ValidatorFactory) ConstraintViolation(jakarta.validation.ConstraintViolation) Validator(jakarta.validation.Validator)

Example 13 with ConstraintViolation

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);
    }
}
Also used : ValidatorFactory(jakarta.validation.ValidatorFactory) ConstraintViolation(jakarta.validation.ConstraintViolation)

Example 14 with ConstraintViolation

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;
}
Also used : Type(org.jboss.resteasy.api.validation.ConstraintType.Type) ResteasyConstraintViolation(org.jboss.resteasy.api.validation.ResteasyConstraintViolation) ConstraintViolation(jakarta.validation.ConstraintViolation) ConstraintType(org.jboss.resteasy.api.validation.ConstraintType)

Example 15 with ConstraintViolation

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());
    }
}
Also used : ResteasyConstraintViolation(org.jboss.resteasy.api.validation.ResteasyConstraintViolation) ConstraintViolation(jakarta.validation.ConstraintViolation) Validator(jakarta.validation.Validator) ConstraintDeclarationException(jakarta.validation.ConstraintDeclarationException) ValidationException(jakarta.validation.ValidationException) PrivilegedActionException(java.security.PrivilegedActionException) ResteasyViolationException(org.jboss.resteasy.api.validation.ResteasyViolationException) ConstraintViolationException(jakarta.validation.ConstraintViolationException) ConstraintDefinitionException(jakarta.validation.ConstraintDefinitionException) GroupDefinitionException(jakarta.validation.GroupDefinitionException)

Aggregations

ConstraintViolation (jakarta.validation.ConstraintViolation)397 Test (org.testng.annotations.Test)309 Validator (jakarta.validation.Validator)258 TestForIssue (org.hibernate.validator.testutil.TestForIssue)134 HibernateValidator (org.hibernate.validator.HibernateValidator)123 ValidatorUtil.getValidator (org.hibernate.validator.testutils.ValidatorUtil.getValidator)70 ExecutableValidator (jakarta.validation.executable.ExecutableValidator)60 ConstraintValidator (jakarta.validation.ConstraintValidator)57 ConstraintMapping (org.hibernate.validator.cfg.ConstraintMapping)54 Method (java.lang.reflect.Method)46 ValidatorFactory (jakarta.validation.ValidatorFactory)41 Test (org.junit.Test)39 ConstraintViolationException (jakarta.validation.ConstraintViolationException)35 Path (jakarta.validation.Path)35 NotNull (jakarta.validation.constraints.NotNull)32 HibernateValidatorConfiguration (org.hibernate.validator.HibernateValidatorConfiguration)27 NotNullDef (org.hibernate.validator.cfg.defs.NotNullDef)19 SizeDef (org.hibernate.validator.cfg.defs.SizeDef)18 Size (jakarta.validation.constraints.Size)17 PropertyNode (org.hibernate.validator.path.PropertyNode)14