Lambda Validations With Java 8

I remember myself writing ugly code when trying to perform some server side validations over my entities. There are some frameworks in the market to solve this problem, but, does it really worth to use a hole framework / library to do such a simple thing like validate an entity? In the other hand, does it make sense to write a lot of painful validation code?

I think we can take advantage of the lambda expressions in java 8 and write our validations in a simple and elegant way without the need of have a lot of dependencies to third party components and 100% the ability to modify our code.

In this article I want to share with you a very simple but useful set of classes you can copy&paste in your project if you want to have simple validation features (based on ‘keep it simple’ concept).

In all this article we will be working with a Person entity which looks like:

NOTE: you can see the source code of this post in my github repository

We will also be using a Validator interface so we can switch both validators we will implement depending on the context:

1 public interface PersonValidator {
2 void validate(Person person);
3 }

Old fashioned validations

First of all, try to remember how you usually validate any kind of entity in java 7 or below. The approach may vary but your code might looks pretty much like this one:

So, in a first look you can see the big amount of boilerplate code we had to write. Of course we can work around to clean this code a little but the result wont vary too much, after that, validations are always a tedious thing to take care of!!

Validations with lambda

Our main objective is to have a cleaner and understandable code. So, the first stept will be to create a Validation interface. This interface will be a @FunctionalInterface and it will have a test method which receives a parameter to be validated and returns the validation result. A ValidationResult is a class with a boolean and a message which represents how the validation ended. TheValidation interface also has an and method and an or method which are already implemented by default based on the test method.

I will also create a SimpleValidation class that implements Validation, it evaluates a java Predicate and returns an ERROR result with the onErrorMessage you sent in case the Predicate fails.

The intent of this class is to be used as a kind of helper to create your custom validations, for example, you can now create a new validation simply writing a code like this one:

So, at this point we are able to rewrite the entire OldFashionedPersonValidator but now using our validation classes and helpers. I will rename it as LambdaPersonValidator so you can differenciate from each other. I would also like to encourage you to compare both versions of the PersonValidator and then tell me which one do you prefer.

As you can see, it took only 4 lines to validate our 4 fields. But that is not the biggest achievment, the great thing here is that our code is pretty expressive and we can read it as if it was a letter. With a few lines of code we ended up using a pretty Sintactic sugarvalidations.

Alright … but how can I be so sure that all this works as expected?

I’ve prepared some special for those sceptical folks. I’ve written some tests in order to be sure both PersonValidator behave the same way. At first, we have an AbstractPersonValidationTest which works as a template for all the scenarios that have to be tested:

After all this tests we can provide one test for each PersonValidator we have, we only have to extend AbstractPersonValidationsTest`:

As a final step, we can run both tests using Junit and we can verify the correctness of the code we wrote:


Thank you for taking the time to read this article, I hope it has been helpful for you and you have enjoyed reading as much as I enjoyed writing it!

Show your support

Clapping shows how much you appreciated Joel Planes’s story.