private static String getViolationPath(final ConstraintViolation violation) { final String propertyPath = violation.getPropertyPath().toString(); return !"".equals(propertyPath) ? propertyPath : ""; }
try { Class<?> beanClazz = violation.getRootBeanClass(); final Iterator<Path.Node> iter = violation.getPropertyPath().iterator(); while (iter.hasNext()) { Path.Node next = iter.next();
@Override public Response toResponse(ConstraintViolationException exception) { LOGGER.debug("Validation constraint violation {}", exception.getConstraintViolations()); ValidationMessage validationMessage = new ValidationMessage(); Set<ConstraintViolation<?>> violations = exception.getConstraintViolations(); Multimap<String, String> errors = ArrayListMultimap.create(); for (ConstraintViolation<?> cv : violations) { String name = StreamSupport.stream(cv.getPropertyPath().spliterator(), false) .map(Path.Node::getName) .reduce((first, second) -> second) .orElseGet(() -> cv.getPropertyPath().toString()); errors.put(name, cv.getMessage()); } validationMessage.setErrors(errors.asMap()); return Response.status(Response.Status.BAD_REQUEST) .entity(validationMessage) .build(); } }
result.getConstraintViolations().stream() .forEach(v -> { Optional<Integer> parameterIndex = StreamSupport.stream(v.getPropertyPath().spliterator(), false) .filter(n -> n.getKind() == ElementKind.PARAMETER) .map(n -> ((Path.ParameterNode) n).getParameterIndex())
@Test public void testSimpleValidationWithClassLevel() { LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean(); validator.afterPropertiesSet(); ValidPerson person = new ValidPerson(); person.setName("Juergen"); person.getAddress().setStreet("Juergen's Street"); Set<ConstraintViolation<ValidPerson>> result = validator.validate(person); assertEquals(1, result.size()); Iterator<ConstraintViolation<ValidPerson>> iterator = result.iterator(); ConstraintViolation<?> cv = iterator.next(); assertEquals("", cv.getPropertyPath().toString()); assertTrue(cv.getConstraintDescriptor().getAnnotation() instanceof NameAddressValid); }
private Path.Node getLeafNode(ConstraintViolation<?> constraintViolation) { Iterator<Path.Node> nodes = constraintViolation.getPropertyPath().iterator(); Path.Node leafNode = null; while ( nodes.hasNext() ) { leafNode = nodes.next(); } return leafNode; } }
@Test public void testSimpleValidation() { LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean(); validator.afterPropertiesSet(); ValidPerson person = new ValidPerson(); Set<ConstraintViolation<ValidPerson>> result = validator.validate(person); assertEquals(2, result.size()); for (ConstraintViolation<ValidPerson> cv : result) { String path = cv.getPropertyPath().toString(); if ("name".equals(path) || "address.street".equals(path)) { assertTrue(cv.getConstraintDescriptor().getAnnotation() instanceof NotNull); } else { fail("Invalid constraint violation with path '" + path + "'"); } } Validator nativeValidator = validator.unwrap(Validator.class); assertTrue(nativeValidator.getClass().getName().startsWith("org.hibernate")); assertTrue(validator.unwrap(ValidatorFactory.class) instanceof HibernateValidatorFactory); assertTrue(validator.unwrap(HibernateValidatorFactory.class) instanceof HibernateValidatorFactory); validator.destroy(); }
@Test public void testSimpleValidationWithCustomProvider() { LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean(); validator.setProviderClass(HibernateValidator.class); validator.afterPropertiesSet(); ValidPerson person = new ValidPerson(); Set<ConstraintViolation<ValidPerson>> result = validator.validate(person); assertEquals(2, result.size()); for (ConstraintViolation<ValidPerson> cv : result) { String path = cv.getPropertyPath().toString(); if ("name".equals(path) || "address.street".equals(path)) { assertTrue(cv.getConstraintDescriptor().getAnnotation() instanceof NotNull); } else { fail("Invalid constraint violation with path '" + path + "'"); } } Validator nativeValidator = validator.unwrap(Validator.class); assertTrue(nativeValidator.getClass().getName().startsWith("org.hibernate")); assertTrue(validator.unwrap(ValidatorFactory.class) instanceof HibernateValidatorFactory); assertTrue(validator.unwrap(HibernateValidatorFactory.class) instanceof HibernateValidatorFactory); validator.destroy(); }
Iterator<Node> nodes = v.getPropertyPath().iterator(); Node firstNode = nodes.next(); if (firstNode.getKind() == ElementKind.METHOD)
@Test public void testEmbeddedCollection() { Session s = openSession(); Transaction tx = s.beginTransaction(); Screen screen = new Screen(); PowerSupply ps = new PowerSupply(); screen.setPowerSupply( ps ); DisplayConnector conn = new DisplayConnector(); conn.setNumber( 0 ); screen.getConnectors().add( conn ); try { s.persist( screen ); s.flush(); fail( "Collection of embedded objects should be validated" ); } catch ( ConstraintViolationException e ) { assertEquals( 1, e.getConstraintViolations().size() ); final ConstraintViolation constraintViolation = e.getConstraintViolations().iterator().next(); assertEquals( Screen.class, constraintViolation.getRootBeanClass() ); // toString works since hibernate validator's Path implementation works accordingly. Should do a Path comparison though assertEquals( "connectors[].number", constraintViolation.getPropertyPath().toString() ); } tx.rollback(); s.close(); }
@Test public void testEmbedded() { Session s = openSession(); Transaction tx = s.beginTransaction(); Screen screen = new Screen(); PowerSupply ps = new PowerSupply(); screen.setPowerSupply( ps ); Button button = new Button(); button.setName( null ); button.setSize( 3 ); screen.setStopButton( button ); try { s.persist( screen ); s.flush(); fail( "@NotNull on embedded property is not evaluated" ); } catch ( ConstraintViolationException e ) { assertEquals( 1, e.getConstraintViolations().size() ); ConstraintViolation<?> cv = e.getConstraintViolations().iterator().next(); assertEquals( Screen.class, cv.getRootBeanClass() ); // toString works since hibernate validator's Path implementation works accordingly. Should do a Path comparison though assertEquals( "stopButton.name", cv.getPropertyPath().toString() ); } tx.rollback(); s.close(); }
private Path.Node getLeafNode(ConstraintViolation<?> constraintViolation) { Iterator<Path.Node> nodes = constraintViolation.getPropertyPath().iterator(); Path.Node leafNode = null; while ( nodes.hasNext() ) { leafNode = nodes.next(); } return leafNode; } }
ResteasyConstraintViolation createResteasyConstraintViolation(ConstraintViolation<?> cv, Type ct) { String path = (suppressPath ? "*" : cv.getPropertyPath().toString()); ResteasyConstraintViolation rcv = new ResteasyConstraintViolation(ct, path, cv.getMessage(), (cv.getInvalidValue() == null ? "null" :cv.getInvalidValue().toString())); return rcv; }
private Path.Node getLeafNode(final ConstraintViolation<?> constraintViolation) { final Iterator<Path.Node> nodes = constraintViolation.getPropertyPath().iterator(); Path.Node leafNode = null; while (nodes.hasNext()) { leafNode = nodes.next(); } return leafNode; } }
protected ResteasyConstraintViolation convertViolation(ConstraintViolation<?> violation) { Type ct = util.getConstraintType(violation); String path = (suppressPath ? "*" : violation.getPropertyPath().toString()); return new ResteasyConstraintViolation(ct, path, violation.getMessage(), convertArrayToString(violation.getInvalidValue())); }
private Path.Node getLeafNode(final ConstraintViolation<?> constraintViolation) { final Iterator<Path.Node> nodes = constraintViolation.getPropertyPath().iterator(); Path.Node leafNode = null; while (nodes.hasNext()) { leafNode = nodes.next(); } return leafNode; } }
ValidationError error = new ValidationError(); error.setMessage(violation.getMessage()); error.setPath(violation.getPropertyPath().toString()); errors.addError(error);
private boolean isSubPathOf(Path p1, Path p2) { Iterator<Path.Node> p1Iter = p1.iterator(); Iterator<Path.Node> p2Iter = p2.iterator(); while ( p1Iter.hasNext() ) { Path.Node p1Node = p1Iter.next(); if ( !p2Iter.hasNext() ) { return false; } Path.Node p2Node = p2Iter.next(); if ( !p1Node.equals( p2Node ) ) { return false; } } return true; }