Question

I have the following class:

class Foo
{
   @Valid
   private Bar bar;
}

In Bar, I have:

class Bar
{
   @NotEmpty
   private String baz;
}

I want to validate the property bar.baz from foo. But when I try validateProperty() and pass the string bar.baz as the property name, I get an error that there's no such property. Is it not possible to validate child properties in this way?

Was it helpful?

Solution

As per Bean Validation specification the property must be a direct property of the bean you are validating:

Set> validateProperty(T object, String propertyName, Class... groups) validates a given field or property of an object. An IllegalArgumentException is thrown when validateProperty() is called and object is null or propertyName is null empty or invalid or null is passed to the varargs groups parameter. The property name is the JavaBeans property name (as defined by the JavaBeans Introspector class). This method implements the logic described in Section 4.6, “Validation routine” but only to the given property. @Valid is not honored by this method. This method is useful for partial object validation.

That said, Hibernate Validator supports property paths as you can also see here. To help any further, you would need to post the full stack trace.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top