use of javax.validation.ConstraintViolation in project javaee7-samples by javaee-samples.
the class ConstructorParametersConstraintsTest method constructorViolationsWhenNullParameters.
@Test
public void constructorViolationsWhenNullParameters() throws NoSuchMethodException, SecurityException {
final MyParameter parameter = new MyParameter();
ExecutableValidator methodValidator = validator.forExecutables();
Constructor<MyBean2> constructor = MyBean2.class.getConstructor(parameter.getClass());
Set<ConstraintViolation<MyBean2>> constraints = methodValidator.validateConstructorParameters(constructor, new Object[] { parameter });
ConstraintViolation<MyBean2> violation = constraints.iterator().next();
assertThat(constraints.size(), equalTo(1));
assertThat(violation.getMessageTemplate(), equalTo("{javax.validation.constraints.NotNull.message}"));
assertThat(violation.getPropertyPath().toString(), equalTo("MyBean2.arg0.value"));
}
use of javax.validation.ConstraintViolation in project jersey by jersey.
the class DefaultConfiguredValidator method validateResult.
@Override
public void validateResult(final Object resource, final Invocable resourceMethod, final Object result) {
if (configuration.getBootstrapConfiguration().isExecutableValidationEnabled()) {
final Set<ConstraintViolation<Object>> constraintViolations = new HashSet<>();
final Method handlingMethod = resourceMethod.getHandlingMethod();
final BeanDescriptor beanDescriptor = getConstraintsForClass(resource.getClass());
final MethodDescriptor methodDescriptor = beanDescriptor.getConstraintsForMethod(handlingMethod.getName(), handlingMethod.getParameterTypes());
final Method definitionMethod = resourceMethod.getDefinitionMethod();
if (methodDescriptor != null && methodDescriptor.hasConstrainedReturnValue() && validateOnExecutionHandler.validateMethod(resource.getClass(), definitionMethod, handlingMethod)) {
constraintViolations.addAll(forExecutables().validateReturnValue(resource, handlingMethod, result));
if (result instanceof Response) {
constraintViolations.addAll(forExecutables().validateReturnValue(resource, handlingMethod, ((Response) result).getEntity()));
}
}
if (!constraintViolations.isEmpty()) {
throw new ConstraintViolationException(constraintViolations);
}
}
}
use of javax.validation.ConstraintViolation in project spring-framework by spring-projects.
the class MethodValidationInterceptor method invoke.
@Override
@SuppressWarnings("unchecked")
public Object invoke(MethodInvocation invocation) throws Throwable {
Class<?>[] groups = determineValidationGroups(invocation);
// Standard Bean Validation 1.1 API
ExecutableValidator execVal = this.validator.forExecutables();
Method methodToValidate = invocation.getMethod();
Set<ConstraintViolation<Object>> result;
try {
result = execVal.validateParameters(invocation.getThis(), methodToValidate, invocation.getArguments(), groups);
} catch (IllegalArgumentException ex) {
// Probably a generic type mismatch between interface and impl as reported in SPR-12237 / HV-1011
// Let's try to find the bridged method on the implementation class...
methodToValidate = BridgeMethodResolver.findBridgedMethod(ClassUtils.getMostSpecificMethod(invocation.getMethod(), invocation.getThis().getClass()));
result = execVal.validateParameters(invocation.getThis(), methodToValidate, invocation.getArguments(), groups);
}
if (!result.isEmpty()) {
throw new ConstraintViolationException(result);
}
Object returnValue = invocation.proceed();
result = execVal.validateReturnValue(invocation.getThis(), methodToValidate, returnValue, groups);
if (!result.isEmpty()) {
throw new ConstraintViolationException(result);
}
return returnValue;
}
use of javax.validation.ConstraintViolation in project torodb by torodb.
the class ConfigUtils method validateBean.
public static <T> void validateBean(T config) {
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
Set<ConstraintViolation<T>> constraintViolations = validator.validate(config);
if (!constraintViolations.isEmpty()) {
IllegalArgumentException illegalArgumentException = transformConstraintsValidation(constraintViolations);
throw illegalArgumentException;
}
}
use of javax.validation.ConstraintViolation in project hibernate-orm by hibernate.
the class BeanValidationEventListener method validate.
private <T> void validate(T object, EntityMode mode, EntityPersister persister, SessionFactoryImplementor sessionFactory, GroupsPerOperation.Operation operation) {
if (object == null || mode != EntityMode.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 = new HashSet<ConstraintViolation<?>>(constraintViolations.size());
Set<String> classNames = new HashSet<String>();
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);
}
}
}
Aggregations