Skip to content
Standalone entity validation with flexible exceptions
Java Shell
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.

Entity validation for Spring/Spring Boot

CircleCI Maven Central Code Quality Codecov GitHub last commit GitHub top language GitHub repo size GitHub issues GitHub pull requests GitHub

Warning! This repository is in early development! Some parts of the API may change until the 1.0 version. If you decide to use it, make sure you key your dependency to a specific version in Maven/Gradle!

This package provides validation for Spring and Spring Boot by collecting all fields from the input and then validating them all at once. The returned object can be used to display all errors on a form.

For example, you could create a controller like this:

public class HelloController {
    public String hello(
        String name
    ) {
        return "Hello, " + name;

In this case the @ApiParam annotation, which normally serves Swagger documentation, will be used for validation purposes. This library supports Swagger and Java validation constraints.


This package can be installed from Maven Central:


Usage with Spring Boot

The library includes bindings for Spring Boot to automatically parse various annotations and use them as validators, such as:

  • Swagger annotations (e.g. @ApiOperation)
  • Java constraints (e.g. @Max)

However, in order to use the automatic validation, you will need to provide a factory to throw exceptions.

import zone.refactor.spring.validation.chain.ExceptionFactory;

class ValidationExceptionFactory extends ExceptionFactory<MyValidationException> {
    void create(Map<String, Collection<String>> errors) throws MyValidationException {
        throw new MyValidationException();

The errors parameter will contain a key-value set where the keys are the request field names and the value is a collection of errors.

Once you have set this up you can add annotations to your controllers. For examples see the test controller.

Independent usage

If you wish to use the validation without the annotation processing you can do so using the ValidatorChain class:

ValidatorChain<String> validatorChain = new ValidatorChain<>(

    new RequiredValidator<String>()

Map<String, String> data = new HashMap<>();
data.put("my-required-field", "my-value");

//Throws exception if validation fails.

As before, the ValidatorChain requires an ExceptionFactory implementation. This factory is responsible for creating a validation factory is the validation fails. This is done such that the actual exception type is flexible.

Writing validators

If you wish to implement a custom validator, you may do so by implementing the Validator interface:

public interface Validator {
    String getErrorKey();
    boolean isValid(@Nullable Object value);

The getErrorKey function is supposed to return a unique error key to identify the error.

The isValid method should return true if the passed value is valid. Special attention should be paid that the value may be a type you don't expect.

You can’t perform that action at this time.