Class ValidationBundle

java.lang.Object
ru.vyarus.dropwizard.guice.module.context.unique.item.UniqueGuiceyBundle
ru.vyarus.guicey.validation.ValidationBundle
All Implemented Interfaces:
ru.vyarus.dropwizard.guice.module.installer.bundle.GuiceyBundle

public class ValidationBundle extends ru.vyarus.dropwizard.guice.module.context.unique.item.UniqueGuiceyBundle
Validation bundle activates implicit method validations for guice beans. It means that if method have any jakarta.validation annotations (contraints for parameters ot return value) then validations would be performed. By default, dropwizard applies validation only to rest resources, this bundle activates validations for all guice beans.

Bundle registered automatically by bundles lookup. But if you want to configure it, simply register it directly (and lookup-provided instance would be ignored).

See ValidationModule for more info. Bundle essentially just provide shortcuts for module configurations.

Bundle also binds Validator and ExecutableValidator, so they become available for injection. Custom validators may use guice injections.

WARNING: do not obtain validator directly from ValidatorFactory because it will not be able to wire guice injections for validators requiring it. Module substitute Validator instance in dropwizard Environment so custom guice-aware validators may be used on rest resources too,

Since:
26.12.2019
  • Constructor Details

    • ValidationBundle

      public ValidationBundle()
  • Method Details

    • targetClasses

      public ValidationBundle targetClasses(com.google.inject.matcher.Matcher<? super Class> matcher)
      Customize target classes to apply validation on. By default, it would be all classes not annotated with Path.

      If you declare your own target matcher, make sure it also avoids rest services: yourMatcher.and(Matchers.not(Matchers.annotatedWith(Path.class))).

      Shortcut for ValidationModule.targetClasses(Matcher).

      Parameters:
      matcher - matcher
      Returns:
      bundle instance
    • targetMethods

      public ValidationBundle targetMethods(com.google.inject.matcher.Matcher<? super Method> matcher)
      Customize target methods to apply validation on. By default, all methods except annotated with rest annotations (GET, link POST, etc.) are allowed (see RestMethodMatcher. Also, synthetic methods avoided.

      It is better to also exclude synthetic and bridge methods from matching: you can simply add direct method matcher: yourMatcher.and(new DirectMethodMatcher()).

      Shortcut for ValidationModule.targetMethods(Matcher).

      Note: it is possible to "implement" explicit mode with this matcher (like Matchers.annotatedWith(MyAnn.class)), but better use validateAnnotatedOnly(Class). Method call will produce correct log and eventually will extend your matcher with annotation condition. But, if you want to implement exclusion annotation, then method matcher is the best choice: Matchers.not(Matchers.annotatedWith(SuppressValidation.class)) will lead to validation suppression on all annotated methods.

      Parameters:
      matcher - matcher
      Returns:
      bundle instance
    • validateAnnotatedOnly

      public ValidationBundle validateAnnotatedOnly()
      Activates explicit mode, when only ValidateOnExecution annotated methods (or all methods in annotated class) are validated.

      Shortcut for ValidationModule.validateAnnotatedOnly().

      Returns:
      bundle instance
    • validateAnnotatedOnly

      public ValidationBundle validateAnnotatedOnly(Class<? extends Annotation> annotation)
      Same as validateAnnotatedOnly(), but you can specify custom annotation.

      Shortcut for ValidationModule.validateAnnotatedOnly(Class).

      Parameters:
      annotation - annotation to trigger validation
      Returns:
      bundle instance
    • strictGroupsDeclaration

      public ValidationBundle strictGroupsDeclaration()
      By default, (Default) group is always added to groups defined with ValidationGroups annotation.

      Calling this method disables default behavior: after calling it, Default must be explicitly declared.

      Returns:
      bundle instance
    • initialize

      public void initialize(ru.vyarus.dropwizard.guice.module.installer.bundle.GuiceyBootstrap bootstrap)
    • run

      public void run(ru.vyarus.dropwizard.guice.module.installer.bundle.GuiceyEnvironment environment) throws Exception
      Throws:
      Exception