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.UniqueGuiceyBundleValidation bundle activates implicit method validations for guice beans. It means that if method have any javax.validtion 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
ValidationModulefor more info. Bundle essentially just provide shortcuts for module configurations.Bundle also binds
ValidatorandExecutableValidator, so they become available for injection. Custom validators may use guice injections.WARNING: do not obtain validator directly from
ValidatorFactorybecause it will not be able to wire guice injections for validators requiring it. Module substituteValidatorinstance in dropwizardEnvironmentso custom guice-aware validators may be used on rest resources too,- Since:
- 26.12.2019
-
-
Constructor Summary
Constructors Constructor Description ValidationBundle()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidinitialize(ru.vyarus.dropwizard.guice.module.installer.bundle.GuiceyBootstrap bootstrap)voidrun(ru.vyarus.dropwizard.guice.module.installer.bundle.GuiceyEnvironment environment)ValidationBundlestrictGroupsDeclaration()By default, (Default) group is always added to groups defined withValidationGroupsannotation.ValidationBundletargetClasses(com.google.inject.matcher.Matcher<? super java.lang.Class> matcher)Customize target classes to apply validation on.ValidationBundletargetMethods(com.google.inject.matcher.Matcher<? super java.lang.reflect.Method> matcher)Customize target methods to apply validation on.ValidationBundlevalidateAnnotatedOnly()Activates explicit mode, when onlyValidateOnExecutionannotated methods (or all methods in annotated class) are validated.ValidationBundlevalidateAnnotatedOnly(java.lang.Class<? extends java.lang.annotation.Annotation> annotation)Same asvalidateAnnotatedOnly(), but you can specify custom annotation.
-
-
-
Method Detail
-
targetClasses
public ValidationBundle targetClasses(com.google.inject.matcher.Matcher<? super java.lang.Class> matcher)
Customize target classes to apply validation on. By default, it would be all classes not annotated withPath.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 java.lang.reflect.Method> matcher)
Customize target methods to apply validation on. By default, all methods except annotated with rest annotations (GET, linkPOST, etc.) are allowed (seeRestMethodMatcher. 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 usevalidateAnnotatedOnly(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 onlyValidateOnExecutionannotated methods (or all methods in annotated class) are validated.Shortcut for
ValidationModule.validateAnnotatedOnly().- Returns:
- bundle instance
-
validateAnnotatedOnly
public ValidationBundle validateAnnotatedOnly(java.lang.Class<? extends java.lang.annotation.Annotation> annotation)
Same asvalidateAnnotatedOnly(), 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 withValidationGroupsannotation.Calling this method disables default behavior: after calling it,
Defaultmust 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 java.lang.Exception- Throws:
java.lang.Exception
-
-