Refine search
private static String toString(Set<? extends ConstraintViolation<?>> constraintViolations) { return constraintViolations.stream() .map( cv -> cv == null ? "null" : cv.getPropertyPath() + ": " + cv.getMessage() ) .collect( Collectors.joining( ", " ) ); } }
public ConstraintViolationMessage(ConstraintViolation<?> violation, MessageSource msgSrc, Locale locale) { this.violation = violation; this.message = msgSrc.getMessage(violation.getMessageTemplate(), new Object[] { violation.getLeafBean().getClass().getSimpleName(), violation.getPropertyPath().toString(), violation.getInvalidValue() }, violation.getMessage(), locale); }
/** * Perform validation of the given bean. * @param bean the bean instance to validate * @see javax.validation.Validator#validate */ protected void doValidate(Object bean) { Assert.state(this.validator != null, "No Validator set"); Object objectToValidate = AopProxyUtils.getSingletonTarget(bean); if (objectToValidate == null) { objectToValidate = bean; } Set<ConstraintViolation<Object>> result = this.validator.validate(objectToValidate); if (!result.isEmpty()) { StringBuilder sb = new StringBuilder("Bean state is invalid: "); for (Iterator<ConstraintViolation<Object>> it = result.iterator(); it.hasNext();) { ConstraintViolation<Object> violation = it.next(); sb.append(violation.getPropertyPath()).append(" - ").append(violation.getMessage()); if (it.hasNext()) { sb.append("; "); } } throw new BeanInitializationException(sb.toString()); } }
if (cv1.getMessage() != null ? !cv1.getMessage().equals(cv2.getMessage()) : cv2.getMessage() != null) if (cv1.getPropertyPath() != null ? !comparePropertyPath(cv1.getPropertyPath(), cv2.getPropertyPath()) : cv2.getPropertyPath() != null)
private static List<String> messagesFor(Set<ConstraintViolation<Object>> violations, String context) { ImmutableList.Builder<String> messages = new ImmutableList.Builder<String>(); for (ConstraintViolation<?> violation : violations) { if (context != null && !context.isEmpty() ) messages.add(context + "." + violation.getPropertyPath().toString() + " " + violation.getMessage()); else messages.add(violation.getPropertyPath().toString() + " " + violation.getMessage()); } return messages.build(); } }
message.setMessage(error.getMessage()); message.setMessageTemplate(error.getMessageTemplate()); message.setPath(error.getPropertyPath().toString()); message.setRootBeanId(rootId); errorMessages.add(message);
public static <T> String format(ConstraintViolation<T> v) { if (v.getConstraintDescriptor().getAnnotation() instanceof ValidationMethod) { return v.getMessage(); } else { return String.format("%s %s", v.getPropertyPath(), v.getMessage()); } }
protected Response handleConstraintViolationException(ConstraintViolationException cve) { ViolationReport report = new ViolationReport(); for (ConstraintViolation cv : cve.getConstraintViolations()) { report.addConstraintViolation(new RestConstraintViolation( cv.getPropertyPath().toString(), cv.getMessage(), cv.getInvalidValue() == null ? "null" : cv.getInvalidValue().toString())); } // TODO for now just do xml output return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(report).type(ContentType.TEXT_XML_UTF_8).build(); } }
protected Response handleConstraintViolationException(ConstraintViolationException cve) { ViolationReport report = new ViolationReport(); for (ConstraintViolation cv : cve.getConstraintViolations()) { report.addConstraintViolation(new RestConstraintViolation( cv.getPropertyPath().toString(), cv.getMessage(), cv.getInvalidValue() == null ? "null" : cv.getInvalidValue().toString())); } // TODO for now just do xml output return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(report).type(ContentType.TEXT_XML_UTF_8).build(); } }
private static <T> String formatMessage(Set<ConstraintViolation<T>> violations) { StringBuilder sb = new StringBuilder(); sb.append("Configuration task validation failed."); for (ConstraintViolation<T> violation : violations) { sb.append(" "); sb.append(violation.getPropertyPath()); sb.append(" "); sb.append(violation.getMessage()); sb.append(" but got "); sb.append(violation.getInvalidValue()); } return sb.toString(); } }
/** * Perform validation of the given bean. * @param bean the bean instance to validate * @see javax.validation.Validator#validate */ protected void doValidate(Object bean) { Assert.state(this.validator != null, "No Validator set"); Object objectToValidate = AopProxyUtils.getSingletonTarget(bean); if (objectToValidate == null) { objectToValidate = bean; } Set<ConstraintViolation<Object>> result = this.validator.validate(objectToValidate); if (!result.isEmpty()) { StringBuilder sb = new StringBuilder("Bean state is invalid: "); for (Iterator<ConstraintViolation<Object>> it = result.iterator(); it.hasNext();) { ConstraintViolation<Object> violation = it.next(); sb.append(violation.getPropertyPath()).append(" - ").append(violation.getMessage()); if (it.hasNext()) { sb.append("; "); } } throw new BeanInitializationException(sb.toString()); } }
/** * @throws IllegalArgumentException 当校验有错误的时候抛出异常 */ public static <T> void throwsIfInvalid(T object, Class<?>... groups) { final Set<ConstraintViolation<T>> constraintViolations = validate(object, groups); if (!constraintViolations.isEmpty()) { final ImmutableMap.Builder<String, String> errorBuilder = ImmutableMap.builder(); for (ConstraintViolation<T> violation : constraintViolations) { errorBuilder.put(violation.getPropertyPath().toString(), violation.getMessage()); } throw new IllegalArgumentException(errorBuilder.build().toString()); } } }
private <T> void validate(Supplier<Set<ConstraintViolation<T>>> validatorSetFunction) { if (validator == null) { logger.warn("validator is null!"); return; } SimpleValidateResults results = new SimpleValidateResults(); validatorSetFunction.get() .forEach(violation -> results.addResult(violation.getPropertyPath().toString(), violation.getMessage())); if (!results.isSuccess()) { throw new ValidationException(results); } }
@Override public Response toResponse(javax.validation.ValidationException e) { final StringBuilder strBuilder = new StringBuilder("[ValidationException] "); for (ConstraintViolation<?> cv : ((ConstraintViolationException) e).getConstraintViolations()) { strBuilder.append(cv.getPropertyPath().toString() + " " + cv.getMessage()); } CommonResultView resultView = CommonResultView.fail(400, 400, strBuilder.toString(), null); return Response.status(resultView.getStatus()) .type(MediaType.APPLICATION_JSON) .entity(resultView).build(); } }
public static <T> T tryValidate(T bean, Class... group) { Set<ConstraintViolation<T>> violations = getValidator().validate(bean, group); if (!violations.isEmpty()) { SimpleValidateResults results = new SimpleValidateResults(); for (ConstraintViolation<T> violation : violations) { results.addResult(violation.getPropertyPath().toString(), violation.getMessage()); } throw new ValidationException(results); } return bean; } }
/** * Gets the human friendly location of where the violation was raised. */ public static String getMessage(ConstraintViolation<?> v, Invocable invocable) { final Pair<Path, ? extends ConstraintDescriptor<?>> of = Pair.of(v.getPropertyPath(), v.getConstraintDescriptor()); final String cachePrefix = PREFIX_CACHE.getIfPresent(of); if (cachePrefix == null) { final String prefix = calculatePrefix(v, invocable); PREFIX_CACHE.put(of, prefix); return prefix + v.getMessage(); } return cachePrefix + v.getMessage(); }
@ExceptionHandler(ConstraintViolationException.class) @ResponseStatus(HttpStatus.BAD_REQUEST) ResponseMessage handleConstraintViolationException(ConstraintViolationException e) { SimpleValidateResults results = new SimpleValidateResults(); for (ConstraintViolation<?> violation : e.getConstraintViolations()) { results.addResult(violation.getPropertyPath().toString(), violation.getMessage()); } List<ValidateResults.Result> errorResults = results.getResults(); return ResponseMessage .error(400, errorResults.isEmpty() ? "" : errorResults.get(0).getMessage()) .result(errorResults); }
@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; } } }
String msg = "property " + violation.getPropertyPath(); msg += " in " + violation.getRootBeanClass(); msg += " " + violation.getMessage(); sortedViolations.add(msg);
@Override public void validate(Entity entity, Serializable instance, Map<String, FieldMetadata> mergedProperties, RecordHelper recordHelper, boolean validateUnsubmittedProperties) { if (isUseDefaultEntityValidations()) { super.validate(entity, instance, mergedProperties, recordHelper, validateUnsubmittedProperties); } Set<ConstraintViolation<Serializable>> violations = getValidator().validate(instance); for (ConstraintViolation<Serializable> violation : violations) { entity.addValidationError(violation.getPropertyPath().toString(), violation.getMessage()); } }