Skip to content

Validation

By default, dropwizard allows you to use validation annotations on rest services. This module allows you to use validation annotations the same way on any guice bean method.

Bundle is actually a wrapper for guice-validator project.

Setup

Maven:

<dependency>
  <groupId>ru.vyarus.guicey</groupId>
  <artifactId>guicey-validation</artifactId>
  <version>7.1.4</version>
</dependency>

Gradle:

implementation 'ru.vyarus.guicey:guicey-validation:7.1.4'

Omit version if guicey BOM used

Usage

By default, no setup required: bundle will be loaded automatically with the bundles lookup mechanism (enabled by default). So just add jar into classpath and annotations will work.

For example:

import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
import ru.vyarus.guicey.annotations.lifecycle.PostStartup;

public class SampleBean {    

    private void doSomething(@NotNull String param) {        
    }

}

Call bean.doSomething(null) will fail with ConstraintValidationException.

For more usage examples see guice-validator documentation

Explicit mode

By default, validations work in implicit mode: any method containing validation annotations would trigger validation on call.

If you want more explicitly mark methods requiring validation then register bundle manually:

.bundles(new ValidationBundle()
                    .validateAnnotatedOnly())

Now, only methods annotated with @ValidateOnExecution (or all methods in annotated class) will trigger validation.

If you want, you can use your own annotation:

.bundles(new ValidationBundle()
                .validateAnnotatedOnly(MyAnnotation.class))

Reducing scope

By default, validation is not applied to resource classes (annotated with @Path) because dropwizard already performs validation there. And rest methods, annotated with @GET, @POST, etc. are skipped (required for complex declaration cases, like dynamic resource mappings or sub resources).

You can reduce this scope even further:

.bundles(new ValidationBundle()
                    .targetClasses(Matchers.subclassesOf(SomeService.class)
                         .and(Matchers.not(Matchers.annotatedWith(Path.class)))))

Here SomeService is excluded from validation (its methods would not trigger validation). Note that default condition (not resource) is appended.

Or excluding methods:

.bundles(new ValidationBundle()
                    .targetMethods(Matchers.annotatedWith(SuppressValidation.class)
                         .and(new DirectMethodMatcher())))

Now methods annotated with @SuppressValidation will not be validated. Note that .and(new DirectMethodMatcher()) condition was added to also exclude synthetic and bridge methods (jvm generated methods).

Note

You can verify AOP appliance with guicey .printGuiceAopMap() report.

Validation groups

By default, Default validation group is always enabled allowing you to not specify groups for each call.

This could be disabled with bundle option:

.bundles(new ValidationBundle().strictGroupsDeclaration())

Read more in guice-validator docs.