/** * Extract the rejected value behind the given constraint violation, * for exposure through the Spring errors representation. * @param field the field that caused the binding error * @param violation the corresponding JSR-303 ConstraintViolation * @param bindingResult a Spring BindingResult for the backing object * which contains the current field's value * @return the invalid value to expose as part of the field error * @since 4.2 * @see javax.validation.ConstraintViolation#getInvalidValue() * @see org.springframework.validation.FieldError#getRejectedValue() */ @Nullable protected Object getRejectedValue(String field, ConstraintViolation<Object> violation, BindingResult bindingResult) { Object invalidValue = violation.getInvalidValue(); if (!"".equals(field) && !field.contains("[]") && (invalidValue == violation.getLeafBean() || field.contains("[") || field.contains("."))) { // Possibly a bean constraint with property path: retrieve the actual property value. // However, explicitly avoid this for "address[]" style paths that we can't handle. invalidValue = bindingResult.getRawFieldValue(field); } return invalidValue; }
/** * Extract the rejected value behind the given constraint violation, * for exposure through the Spring errors representation. * @param field the field that caused the binding error * @param violation the corresponding JSR-303 ConstraintViolation * @param bindingResult a Spring BindingResult for the backing object * which contains the current field's value * @return the invalid value to expose as part of the field error * @since 4.2 * @see javax.validation.ConstraintViolation#getInvalidValue() * @see org.springframework.validation.FieldError#getRejectedValue() */ @Nullable protected Object getRejectedValue(String field, ConstraintViolation<Object> violation, BindingResult bindingResult) { Object invalidValue = violation.getInvalidValue(); if (!"".equals(field) && !field.contains("[]") && (invalidValue == violation.getLeafBean() || field.contains("[") || field.contains("."))) { // Possibly a bean constraint with property path: retrieve the actual property value. // However, explicitly avoid this for "address[]" style paths that we can't handle. invalidValue = bindingResult.getRawFieldValue(field); } return invalidValue; }
LOG.trace( violation ); propagatedViolations.add( violation ); classNames.add( violation.getLeafBean().getClass().getName() );
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); }
private boolean ejbsPresent(Set<ConstraintViolation<Object>> set1) { if (ejbsPresent) { return true; } for (ConstraintViolation<Object> cv : set1) { if (isEjb(cv.getLeafBean().getClass())) { ejbsPresent = true; return true; } } return false; }
classNames.add( violation.getLeafBean().getClass().getName() );
Method m = getMethod(v.getLeafBean().getClass(), getterName); if (m.getReturnType().equals(boolean.class))
if (cv1.getLeafBean() != null ? !compareClass(cv1.getLeafBean().getClass(), cv2.getLeafBean().getClass()) : cv2.getLeafBean() != null)
/** * @see ConstraintViolation#getLeafBean */ public Object getLeafBean() { return constraintViolation.getLeafBean(); }
if (error.getLeafBean() != null) { SimpleProxyId<?> stableId = source.getStableId(error.getLeafBean()); if (stableId != null) { leafId = FACTORY.id().as();
private String getPath(final ConstraintViolation violation) { final String leafBeanName = violation.getLeafBean().getClass().getSimpleName(); final String propertyPath = violation.getPropertyPath().toString(); return leafBeanName + (!"".equals(propertyPath) ? '.' + propertyPath : ""); }
private String getPath(final ConstraintViolation violation) { String leafBeanName = violation.getLeafBean().getClass().getSimpleName(); final int proxySuffix = leafBeanName.indexOf("$$EnhancerByGuice"); if (proxySuffix > 0) { leafBeanName = leafBeanName.substring(0, proxySuffix); } final String propertyPath = violation.getPropertyPath().toString(); return leafBeanName + (!"".equals(propertyPath) ? '.' + propertyPath : ""); } }
private void checkConstraints(final Set<ConstraintViolation<Object>> constraintViolations) { Set<ConstraintViolation<?>> propagatedViolations = new HashSet<ConstraintViolation<?>>( constraintViolations.size()); Set<String> classNames = new HashSet<String>(); for (ConstraintViolation<?> violation : constraintViolations) { propagatedViolations.add(violation); classNames.add(violation.getLeafBean().getClass().getName()); } String msg = "Validation failed for classes " + classNames; throw new ConstraintViolationException(msg, propagatedViolations); }
private void checkConstraints(final Set<ConstraintViolation<Object>> constraintViolations) { Set<ConstraintViolation<?>> propagatedViolations = new HashSet<ConstraintViolation<?>>( constraintViolations.size()); Set<String> classNames = new HashSet<String>(); for (ConstraintViolation<?> violation : constraintViolations) { propagatedViolations.add(violation); classNames.add(violation.getLeafBean().getClass().getName()); } String msg = "Validation failed for classes " + classNames; throw new ConstraintViolationException(msg, propagatedViolations); }
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); }
private static Annotation[] getPropertyAnnotations(ConstraintViolation<?> violation, Path.PropertyNode node) { Class<?> leafBeanClass = violation.getLeafBean().getClass(); Set<Annotation> allAnnotations = new HashSet<>(); try { Field field = leafBeanClass.getDeclaredField(node.getName()); allAnnotations.addAll(Arrays.asList(field.getAnnotations())); } catch (NoSuchFieldException e) { // ignore for now } allAnnotations.addAll(readAndWriteMethodAnnotationsForField(leafBeanClass, node.getName())); return allAnnotations.toArray(new Annotation[0]); }
ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); Validator validator = factory.getValidator(); Set<ConstraintViolation<Notice>> constraintViolations = validator.validate(notice); for (ConstraintViolation cv : constraintViolations) { System.out.println("ValidatationConstraint: " + cv.getConstraintDescriptor().getAnnotation()); System.out.println("ValidatationConstraint: " + cv.getConstraintDescriptor()); System.out.println("ValidatationConstraint: " + cv.getMessageTemplate()); System.out.println("ValidatationConstraint: " + cv.getInvalidValue()); System.out.println("ValidatationConstraint: " + cv.getLeafBean()); System.out.println("ValidatationConstraint: " + cv.getRootBeanClass()); System.out.println("ValidatationConstraint: " + cv.getPropertyPath().toString()); System.out.println("ValidatationConstraint: " + cv.getMessage()); }
@SuppressWarnings("unchecked") public <T> void notifyConstraintViolations(T root, Set<ConstraintViolation<?>> constraintViolations) { Map<Object, Set<ConstraintViolation<Object>>> violationsMap = new HashMap<Object, Set<ConstraintViolation<Object>>>(); for (ConstraintViolation<?> violation : constraintViolations) { Object rootBean = violation.getRootBean(); Object leafBean = violation.getLeafBean(); Object bean = leafBean != null ? leafBean : rootBean; Set<ConstraintViolation<Object>> violations = violationsMap.get(bean); if (violations == null) { violations = new HashSet<ConstraintViolation<Object>>(); violationsMap.put(bean, violations); } violations.add((ConstraintViolation<Object>)violation); } for (Object bean : violationsMap.keySet()) { if (bean != root) validationNotifier.notifyConstraintViolations(bean, violationsMap.get(bean)); } validationNotifier.notifyConstraintViolations(root, violationsMap.get(root)); }
@Override public Message convert(final ConstraintViolation<?> violation) { final Object bean = violation.getLeafBean(); final String messageId; if (bean != null) { messageId = createItemNameMessageId(bean.getClass(), getLeafPropertyName(violation)); } else { messageId = ""; } final StringResource stringResource = new ViolationBasedStringResourceWithItemName(messageId, violation); return new ValidationResultMessage(prefix + violation.getPropertyPath(), stringResource, null); }
private ViolationExpectation(ConstraintViolation<?> violation, ViolationExpectationPropertiesToTest propertiesToTest) { this.constraintType = violation.getConstraintDescriptor().getAnnotation().annotationType(); if ( propertiesToTest.testRootBeanClass ) { withRootBeanClass( violation.getRootBeanClass() ); } if ( propertiesToTest.testMessage ) { withMessage( violation.getMessage() ); } if ( propertiesToTest.testInvalidValue ) { withInvalidValue( violation.getInvalidValue() ); } if ( propertiesToTest.testPropertyPath ) { withPropertyPath( new PathExpectation( violation.getPropertyPath() ) ); } if ( propertiesToTest.testLeafBean ) { withLeafBean( violation.getLeafBean() ); } }