throw new ConstraintViolationException("Failed to validate service: " + clazz.getName() + ", method: " + methodName + ", cause: " + violations, violations);
throw new ConstraintViolationException("Failed to validate service: " + clazz.getName() + ", method: " + methodName + ", cause: " + violations, violations);
throw new ConstraintViolationException(result); throw new ConstraintViolationException(result);
@Override public Object parse(final TypeLiteral<?> type, final Context ctx) throws Throwable { Object value = ctx.next(); if (predicate.test(type)) { Validator validator = ctx.require(Validator.class); Set<ConstraintViolation<Object>> violations = validator.validate(value); if (violations.size() > 0) { throw new ConstraintViolationException(violations); } } return value; }
@Override @SuppressWarnings("unchecked") public void afterPropertiesSet() throws Exception { Assert.state(yaml != null, "Yaml document should not be null"); Validator validator = Validation.buildDefaultValidatorFactory().getValidator(); try { logger.trace("Yaml document is\n" + yaml); configuration = (T) (new Yaml(constructor)).load(yaml); Set<ConstraintViolation<T>> errors = validator.validate(configuration); if (!errors.isEmpty()) { logger.error("YAML configuration failed validation"); for (ConstraintViolation<?> error : errors) { logger.error(error.getPropertyPath() + ": " + error.getMessage()); } if (exceptionIfInvalid) { @SuppressWarnings("rawtypes") ConstraintViolationException summary = new ConstraintViolationException((Set) errors); throw summary; } } } catch (YAMLException e) { if (exceptionIfInvalid) { throw e; } } }
@SuppressWarnings({ "rawtypes", "unchecked" }) @Override public void onBeforeSave(BeforeSaveEvent<Object> event) { LOG.debug("Validating object: {}", event.getSource()); Set violations = validator.validate(event.getSource()); if (!violations.isEmpty()) { LOG.info("During object: {} validation violations found: {}", event.getSource(), violations); throw new ConstraintViolationException(violations); } } }
/** * * @param invocation * @return * @throws ConstraintViolationException incase of any constraints * defined on method parameters are violated. */ @Override public Object invoke(MethodInvocation invocation) throws Throwable { if (skipMethod(invocation)) { return invocation.proceed(); } ExecutableValidator executableValidator = validatorProvider.get().forExecutables(); Set<ConstraintViolation<Object>> result = executableValidator.validateParameters( invocation.getThis(), invocation.getMethod(), invocation.getArguments()); if (!result.isEmpty()) { throw new ConstraintViolationException(result); } return invocation.proceed(); }
public static void validate(Object bean) { Set<ConstraintViolation<Object>> violations = getViolations(bean); if (U.notEmpty(violations)) { throw new ConstraintViolationException(violations); } }
throw new ConstraintViolationException(result); throw new ConstraintViolationException(result);
throw new ConstraintViolationException( builder.toString(), propagatedViolations );
@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); } } } }
@Override public void onValidate(final ValidationInterceptorContext ctx) { final Object resource = ctx.getResource(); final Invocable resourceMethod = ctx.getInvocable(); final Object[] args = ctx.getArgs(); final Set<ConstraintViolation<Object>> constraintViolations = new HashSet<>(); final BeanDescriptor beanDescriptor = getConstraintsForClass(resource.getClass()); // Resource validation. if (beanDescriptor.isBeanConstrained()) { constraintViolations.addAll(validate(resource)); } if (resourceMethod != null && configuration.getBootstrapConfiguration().isExecutableValidationEnabled()) { final Method handlingMethod = resourceMethod.getHandlingMethod(); // Resource method validation - input parameters. final MethodDescriptor methodDescriptor = beanDescriptor.getConstraintsForMethod(handlingMethod.getName(), handlingMethod.getParameterTypes()); if (methodDescriptor != null && methodDescriptor.hasConstrainedParameters() && validateOnExecutionHandler.validateMethod(resource.getClass(), resourceMethod.getDefinitionMethod(), resourceMethod.getHandlingMethod())) { constraintViolations.addAll(forExecutables().validateParameters(resource, handlingMethod, args)); } } if (!constraintViolations.isEmpty()) { throw new ConstraintViolationException(constraintViolations); } }
protected void validate(AbstractEntity entity) { Set<ConstraintViolation<AbstractEntity>> validate = validator.validate(entity); if (!CollectionUtils.isEmpty(validate)) { throw new ConstraintViolationException((Set)validate); } }
/** * 调用JSR303的validate方法, 验证失败时抛出ConstraintViolationException. * * @param validator the validator * @param object the object * @param groups the groups * @throws ConstraintViolationException the constraint violation exception */ @SuppressWarnings({"unchecked", "rawtypes"}) public static void validateWithException(Validator validator, Object object, Class<?>... groups) { Set constraintViolations = validator.validate(object, groups); if (!constraintViolations.isEmpty()) { throw new ConstraintViolationException(constraintViolations); } }
private void handleValidationException(Set constraintViolations) throws ConstraintViolationException { if (constraintViolations != null && !constraintViolations.isEmpty()) { Iterator<ConstraintViolation<ConfigBeanProxy>> it = constraintViolations.iterator(); StringBuilder sb = new StringBuilder(); sb.append(MessageFormat.format(i18n.getString("bean.validation.failure"), this.<ConfigBeanProxy>getProxyType().getSimpleName())); String violationMsg = i18n.getString("bean.validation.constraintViolation"); while (it.hasNext()) { ConstraintViolation cv = it.next(); sb.append(" "); sb.append(MessageFormat.format(violationMsg, cv.getMessage(), cv.getPropertyPath())); if (it.hasNext()) { sb.append(i18n.getString("bean.validation.separator")); } } bean.getLock().unlock(); throw new ConstraintViolationException(sb.toString(), constraintViolations); } }
@SuppressWarnings({ "rawtypes", "unchecked" }) @Override public void onBeforeSave(BeforeSaveEvent<Object> event) { LOG.debug("Validating object: {}", event.getSource()); Set violations = validator.validate(event.getSource()); if (!violations.isEmpty()) { LOG.info("During object: {} validation violations found: {}", event.getSource(), violations); throw new ConstraintViolationException(violations); } } }
@Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { Set<ConstraintViolation<Object>> constraintViolations = validator.forExecutables().validateParameters( wrapped, method, args == null ? EMPTY_ARGS : args, groups ); if ( !constraintViolations.isEmpty() ) { throw new ConstraintViolationException( new HashSet<ConstraintViolation<?>>( constraintViolations ) ); } Object result = method.invoke( wrapped, args ); constraintViolations = validator.forExecutables().validateReturnValue( wrapped, method, result, groups ); if ( !constraintViolations.isEmpty() ) { throw new ConstraintViolationException( new HashSet<ConstraintViolation<?>>( constraintViolations ) ); } return result; } }
public static ConstraintViolationException instantiate( SerializationStreamReader streamReader) throws SerializationException { String message = streamReader.readString(); @SuppressWarnings("unchecked") Set<ConstraintViolation<?>> set = (Set<ConstraintViolation<?>>) streamReader .readObject(); return new ConstraintViolationException(message, set); }
@Override public <T> void beforeMethodInvoke(SwaggerInvocation invocation, SwaggerProducerOperation producerOperation, Object[] args) throws ConstraintViolationException { if (null == executableValidator) { ValidatorFactory factory = Validation.byDefaultProvider() .configure() .parameterNameProvider(new DefaultParameterNameProvider()) .buildValidatorFactory(); executableValidator = factory.getValidator().forExecutables(); } Set<ConstraintViolation<Object>> violations = executableValidator.validateParameters(producerOperation.getProducerInstance(), producerOperation.getProducerMethod(), args, Default.class); if (violations.size() > 0) { LOGGER.warn("Parameter validation failed : " + violations.toString()); throw new ConstraintViolationException(violations); } }
throw new ConstraintViolationException( getMessage( ctx.getConstructor(), ctx.getParameters(), violations ), violations throw new ConstraintViolationException( getMessage( ctx.getConstructor(), ctx.getParameters(), violations ), violations