Vluent is a small Java library I have been working on, on and off, for quite a long time. The first commit is from 2017. The idea is that validation rules should be plain simple objects that you can name, test and reuse, and you combine them with a fluent API. No annotation hell, and no if/else chains that nobody wants to touch.
return Vluent.create()
.on(app.name(), NAME_PRESENT)
.when(isSelfEmployed)
.then(app, SELF_EMPLOYED_INCOME)
.validate();
where validators are defined as named variables using Lambda expression in Java:
static final Validator<String> NAME_PRESENT =
name ->
name != null && !name.isBlank()
? ValidationResult.SUCCESS
: ValidationResult.createError("Application name is required");
static final Validator<Application> SELF_EMPLOYED_INCOME =
app ->
app.selfEmployedIncome() != null && app.selfEmployedIncome().signum() > 0
? ValidationResult.SUCCESS
: ValidationResult.createError(
"Self-employed income must be provided and greater than zero");
For a long time it was a side project that I looked at a few times a year. In the last couple of months I finally spent some real time on it, so here is a short overview of what changed.
This was the main thing missing. Most Java code I see in practice runs on Spring Boot, and a validation library that does not fit there is hard to sell. There are now two ways to use Vluent with Spring’s @Valid.
The first one goes through Bean Validation. You put @ValidateBeanWith on the bean, and it works together with the usual @NotBlank and @NotNull annotations. The second one is a new VluentSpringValidator class. It wraps a Vluent validator as a Spring validator, and you register it in an @InitBinder method:
binder.addValidators(new VluentSpringValidator<>(User.class, new UserValidator()));
The nice part of the second option is that you create the validator yourself. So it can be a Spring bean with injected dependencies, for example a repository that checks if an email already exists. In both cases the same Validator
To show how this looks in a real application, I added a small Spring Boot example project to the repository. It uses both options and you can try it with curl.
Until now, a validation chain stopped at the first failed rule and returned only that message. This is fine for a quick check, but annoying when a user submits a form with three mistakes and has to fix them one by one. ValidationResult now keeps a list of all errors in the chain, and you get all of them with getErrors().
The rest is mostly housekeeping, but it matters if I want other people to use the library. The code is now split into an api package and an internal package, and there is a proper module-info.java. Only the api package is exported, so I can change the internals without breaking anyone. Bean Validation and Spring are optional dependencies, so the library itself still has zero transitive dependencies.
All of this will go into the 0.0.5 release. If you want to take a look, the code is on GitHub and the current version is available on Maven Central.