Nested Java Bean Validation

Somebody asked me recently why their POJO validator was not working as expected. They had already followed the “code I wrote” down to a T. Emphasis on the quotes, because trust me that block of code for bean validation they used as reference from an older project I did, was pretty much a very trivial few lines. Then again, everything is almost like that in Spring Boot. Also it was more of correctly annotating the beans, rather than on whether or not there was something lacking with the Validator.

By the way, for context, this bean validation is the JSR 380 specification for the Java API. Otherwise commonly known as Bean Validation 2.0 too. This allows for enforcing that the properties of a bean meets a specific criteria using annotations such as @NotNull, @Min, and @Email.

Now going back, I looked at how validation was done for a third party vendor data on that older project they used for reference, and compared that with what they have. It turns out there was a simple difference.

They were doing validation on nested objects. One big and complex layers of objects for which that particular implementation I wrote would not work – as is – for their use case. Fortunately, all that was left to do was add just another annotation to make it work. No big code rewrite needed.

Assuming we have a Book class like the one below.

public class Book {

    @NotEmpty
    private String title;

    private String datePublished

    @NotNull
    private Author author;
}

Then the Author class looks like this here.

public class Author {

    @NotEmpty
    private String firstName;

    @NotEmpty
    private String lastName

    @NotEmpty
    @Size(min=7)
    @Email
    private String email;
}

Validating on the Book object, they were able to verify that it worked according to the criteria they set for it. Rightly so.

However, when they set to test out the validation rules to include the Author object, none of the validation worked. Why’s that? They asked me.

Like I mentioned earlier, it is just lacking an annotation to tell the Validator to also validate on the other bean. I remember having encountered the same question way back. So, the Book object validation works at that level because if you assign the Author a null value, that will trigger the constraint violation. But it stops there.

To include the validation to be done down to the next bean (Author), we only need to add @Valid annotation to that field in the Book class. The Book class above will now look like this with the single line modification.

public class Book {

    @NotEmpty
    private String title;

    private String datePublished

    @NotNull
    @Valid
    private Author author;
}

And that’s about it! The validation for nested beans should now work like a charm.

Do that for every other one of your Java objects you want to include in your validations. I suppose there is a faster way to do this so we don’t have to address each field, but I forget how to do that. I suppose a class level validation should work? I think I will need to review my notes on that one.

Similar Posts: