Class DefaultValidator

  • All Implemented Interfaces:
    io.micronaut.inject.annotation.AnnotatedElementValidator, io.micronaut.inject.validation.BeanDefinitionValidator, ExecutableMethodValidator, ReactiveValidator, Validator, javax.validation.executable.ExecutableValidator, javax.validation.Validator
    Direct Known Subclasses:
    DefaultAnnotatedElementValidator

    @Singleton
    @Primary
    @Requires(property="micronaut.validator.enabled",
              value="true",
              defaultValue="true")
    public class DefaultValidator
    extends java.lang.Object
    implements Validator, ExecutableMethodValidator, ReactiveValidator, io.micronaut.inject.annotation.AnnotatedElementValidator, io.micronaut.inject.validation.BeanDefinitionValidator
    Default implementation of the Validator interface.
    Since:
    1.2
    • Field Summary

      • Fields inherited from interface io.micronaut.inject.validation.BeanDefinitionValidator

        DEFAULT
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      <T> T createValid​(java.lang.Class<T> beanType, java.lang.Object... arguments)
      Create a new valid instance.
      ExecutableMethodValidator forExecutables()
      Overridden variation that returns a ExecutableMethodValidator.
      protected io.micronaut.core.beans.BeanIntrospection<java.lang.Object> getBeanIntrospection​(java.lang.Object object)
      looks up a bean introspection for the given object.
      protected io.micronaut.core.beans.BeanIntrospection<java.lang.Object> getBeanIntrospection​(java.lang.Object object, java.lang.Class<?> definedClass)
      looks up a bean introspection for the given object by instance's class or defined class.
      javax.validation.metadata.BeanDescriptor getConstraintsForClass​(java.lang.Class<?> clazz)  
      <T> T unwrap​(java.lang.Class<T> type)  
      <T> java.util.Set<javax.validation.ConstraintViolation<T>> validate​(io.micronaut.core.beans.BeanIntrospection<T> introspection, T object, java.lang.Class<?>... groups)
      Validate the given introspection and object.
      <T> java.util.Set<javax.validation.ConstraintViolation<T>> validate​(T object, java.lang.Class<?>... groups)  
      <T> void validateBean​(io.micronaut.context.BeanResolutionContext resolutionContext, io.micronaut.inject.BeanDefinition<T> definition, T bean)  
      <T> void validateBeanArgument​(io.micronaut.context.BeanResolutionContext resolutionContext, io.micronaut.inject.InjectionPoint injectionPoint, io.micronaut.core.type.Argument<T> argument, int index, T value)  
      <T> java.util.concurrent.CompletionStage<T> validateCompletionStage​(java.util.concurrent.CompletionStage<T> completionStage, java.lang.Class<?>... groups)
      Validate the given CompletionStage by returning a new CompletionStage that validates the emitted value.
      <T> java.util.Set<javax.validation.ConstraintViolation<T>> validateConstructorParameters​(io.micronaut.core.beans.BeanIntrospection<? extends T> introspection, java.lang.Object[] parameterValues, java.lang.Class<?>... groups)
      Validates parameters for the given introspection and values.
      <T> java.util.Set<javax.validation.ConstraintViolation<T>> validateConstructorParameters​(java.lang.Class<? extends T> beanType, io.micronaut.core.type.Argument<?>[] constructorArguments, java.lang.Object[] parameterValues, java.lang.Class<?>[] groups)
      Validates arguments for the given bean type and constructor arguments.
      <T> java.util.Set<javax.validation.ConstraintViolation<T>> validateConstructorParameters​(java.lang.reflect.Constructor<? extends T> constructor, java.lang.Object[] parameterValues, java.lang.Class<?>... groups)  
      <T> java.util.Set<javax.validation.ConstraintViolation<T>> validateConstructorReturnValue​(java.lang.reflect.Constructor<? extends T> constructor, T createdObject, java.lang.Class<?>... groups)  
      java.util.Set<java.lang.String> validatedAnnotatedElement​(io.micronaut.core.annotation.AnnotatedElement element, java.lang.Object value)  
      <T> java.util.Set<javax.validation.ConstraintViolation<T>> validateParameters​(T object, io.micronaut.inject.ExecutableMethod method, java.lang.Object[] parameterValues, java.lang.Class<?>... groups)
      Validate the parameter values of the given ExecutableMethod.
      <T> java.util.Set<javax.validation.ConstraintViolation<T>> validateParameters​(T object, io.micronaut.inject.ExecutableMethod method, java.util.Collection<io.micronaut.core.type.MutableArgumentValue<?>> argumentValues, java.lang.Class<?>... groups)
      Validate the parameter values of the given ExecutableMethod.
      <T> java.util.Set<javax.validation.ConstraintViolation<T>> validateParameters​(T object, java.lang.reflect.Method method, java.lang.Object[] parameterValues, java.lang.Class<?>... groups)  
      <T> java.util.Set<javax.validation.ConstraintViolation<T>> validateProperty​(T object, java.lang.String propertyName, java.lang.Class<?>... groups)  
      <T> org.reactivestreams.Publisher<T> validatePublisher​(org.reactivestreams.Publisher<T> publisher, java.lang.Class<?>... groups)
      Validate the given publisher by returning a new Publisher that validates each emitted value.
      <T> java.util.Set<javax.validation.ConstraintViolation<T>> validateReturnValue​(T object, io.micronaut.inject.ExecutableMethod<?,​java.lang.Object> executableMethod, java.lang.Object returnValue, java.lang.Class<?>... groups)
      Validates the return value of a ExecutableMethod.
      <T> java.util.Set<javax.validation.ConstraintViolation<T>> validateReturnValue​(T object, java.lang.reflect.Method method, java.lang.Object returnValue, java.lang.Class<?>... groups)  
      <T> java.util.Set<javax.validation.ConstraintViolation<T>> validateValue​(java.lang.Class<T> beanType, java.lang.String propertyName, java.lang.Object value, java.lang.Class<?>... groups)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • DefaultValidator

        protected DefaultValidator​(@NonNull
                                   ValidatorConfiguration configuration)
        Default constructor.
        Parameters:
        configuration - The validator configuration
    • Method Detail

      • validate

        @NonNull
        public <T> java.util.Set<javax.validation.ConstraintViolation<T>> validate​(@NonNull
                                                                                   T object,
                                                                                   @Nullable
                                                                                   java.lang.Class<?>... groups)
        Specified by:
        validate in interface Validator
        Specified by:
        validate in interface javax.validation.Validator
      • validate

        @NonNull
        public <T> java.util.Set<javax.validation.ConstraintViolation<T>> validate​(@NonNull
                                                                                   io.micronaut.core.beans.BeanIntrospection<T> introspection,
                                                                                   @NonNull
                                                                                   T object,
                                                                                   @Nullable
                                                                                   java.lang.Class<?>... groups)
        Validate the given introspection and object.
        Specified by:
        validate in interface Validator
        Type Parameters:
        T - The object type
        Parameters:
        introspection - The introspection
        object - The object
        groups - The groups
        Returns:
        The constraint violations
      • validateProperty

        @NonNull
        public <T> java.util.Set<javax.validation.ConstraintViolation<T>> validateProperty​(@NonNull
                                                                                           T object,
                                                                                           @NonNull
                                                                                           java.lang.String propertyName,
                                                                                           @Nullable
                                                                                           java.lang.Class<?>... groups)
        Specified by:
        validateProperty in interface Validator
        Specified by:
        validateProperty in interface javax.validation.Validator
      • validateValue

        @NonNull
        public <T> java.util.Set<javax.validation.ConstraintViolation<T>> validateValue​(@NonNull
                                                                                        java.lang.Class<T> beanType,
                                                                                        @NonNull
                                                                                        java.lang.String propertyName,
                                                                                        @Nullable
                                                                                        java.lang.Object value,
                                                                                        @Nullable
                                                                                        java.lang.Class<?>... groups)
        Specified by:
        validateValue in interface Validator
        Specified by:
        validateValue in interface javax.validation.Validator
      • validatedAnnotatedElement

        @NonNull
        public java.util.Set<java.lang.String> validatedAnnotatedElement​(@NonNull
                                                                         io.micronaut.core.annotation.AnnotatedElement element,
                                                                         @Nullable
                                                                         java.lang.Object value)
        Specified by:
        validatedAnnotatedElement in interface io.micronaut.inject.annotation.AnnotatedElementValidator
      • createValid

        @NonNull
        public <T> T createValid​(@NonNull
                                 java.lang.Class<T> beanType,
                                 java.lang.Object... arguments)
                          throws javax.validation.ConstraintViolationException
        Description copied from interface: ExecutableMethodValidator
        Create a new valid instance.
        Specified by:
        createValid in interface ExecutableMethodValidator
        Type Parameters:
        T - the generic type
        Parameters:
        beanType - The type
        arguments - The arguments
        Returns:
        The instance
        Throws:
        javax.validation.ConstraintViolationException - If a valid instance couldn't be constructed
      • getConstraintsForClass

        public javax.validation.metadata.BeanDescriptor getConstraintsForClass​(java.lang.Class<?> clazz)
        Specified by:
        getConstraintsForClass in interface javax.validation.Validator
      • unwrap

        public <T> T unwrap​(java.lang.Class<T> type)
        Specified by:
        unwrap in interface javax.validation.Validator
      • validateParameters

        @NonNull
        public <T> java.util.Set<javax.validation.ConstraintViolation<T>> validateParameters​(@NonNull
                                                                                             T object,
                                                                                             @NonNull
                                                                                             io.micronaut.inject.ExecutableMethod method,
                                                                                             @NonNull
                                                                                             java.lang.Object[] parameterValues,
                                                                                             @Nullable
                                                                                             java.lang.Class<?>... groups)
        Description copied from interface: ExecutableMethodValidator
        Validate the parameter values of the given ExecutableMethod.
        Specified by:
        validateParameters in interface ExecutableMethodValidator
        Type Parameters:
        T - The object type
        Parameters:
        object - The object
        method - The method
        parameterValues - The values
        groups - The groups
        Returns:
        The constraint violations.
      • validateParameters

        @NonNull
        public <T> java.util.Set<javax.validation.ConstraintViolation<T>> validateParameters​(@NonNull
                                                                                             T object,
                                                                                             @NonNull
                                                                                             io.micronaut.inject.ExecutableMethod method,
                                                                                             @NonNull
                                                                                             java.util.Collection<io.micronaut.core.type.MutableArgumentValue<?>> argumentValues,
                                                                                             @Nullable
                                                                                             java.lang.Class<?>... groups)
        Description copied from interface: ExecutableMethodValidator
        Validate the parameter values of the given ExecutableMethod.
        Specified by:
        validateParameters in interface ExecutableMethodValidator
        Type Parameters:
        T - The object type
        Parameters:
        object - The object
        method - The method
        argumentValues - The values
        groups - The groups
        Returns:
        The constraint violations.
      • validateParameters

        @NonNull
        public <T> java.util.Set<javax.validation.ConstraintViolation<T>> validateParameters​(@NonNull
                                                                                             T object,
                                                                                             @NonNull
                                                                                             java.lang.reflect.Method method,
                                                                                             @NonNull
                                                                                             java.lang.Object[] parameterValues,
                                                                                             @Nullable
                                                                                             java.lang.Class<?>... groups)
        Specified by:
        validateParameters in interface ExecutableMethodValidator
        Specified by:
        validateParameters in interface javax.validation.executable.ExecutableValidator
      • validateReturnValue

        @NonNull
        public <T> java.util.Set<javax.validation.ConstraintViolation<T>> validateReturnValue​(@NonNull
                                                                                              T object,
                                                                                              @NonNull
                                                                                              java.lang.reflect.Method method,
                                                                                              @Nullable
                                                                                              java.lang.Object returnValue,
                                                                                              @Nullable
                                                                                              java.lang.Class<?>... groups)
        Specified by:
        validateReturnValue in interface ExecutableMethodValidator
        Specified by:
        validateReturnValue in interface javax.validation.executable.ExecutableValidator
      • validateReturnValue

        @NonNull
        public <T> java.util.Set<javax.validation.ConstraintViolation<T>> validateReturnValue​(@NonNull
                                                                                              T object,
                                                                                              @NonNull
                                                                                              io.micronaut.inject.ExecutableMethod<?,​java.lang.Object> executableMethod,
                                                                                              @Nullable
                                                                                              java.lang.Object returnValue,
                                                                                              @Nullable
                                                                                              java.lang.Class<?>... groups)
        Description copied from interface: ExecutableMethodValidator
        Validates the return value of a ExecutableMethod.
        Specified by:
        validateReturnValue in interface ExecutableMethodValidator
        Type Parameters:
        T - The object type
        Parameters:
        object - The object
        executableMethod - The method
        returnValue - The return value
        groups - The groups
        Returns:
        A set of contstraint violations
      • validateConstructorParameters

        @NonNull
        public <T> java.util.Set<javax.validation.ConstraintViolation<T>> validateConstructorParameters​(@NonNull
                                                                                                        java.lang.reflect.Constructor<? extends T> constructor,
                                                                                                        @NonNull
                                                                                                        java.lang.Object[] parameterValues,
                                                                                                        @Nullable
                                                                                                        java.lang.Class<?>... groups)
        Specified by:
        validateConstructorParameters in interface ExecutableMethodValidator
        Specified by:
        validateConstructorParameters in interface javax.validation.executable.ExecutableValidator
      • validateConstructorParameters

        @NonNull
        public <T> java.util.Set<javax.validation.ConstraintViolation<T>> validateConstructorParameters​(@NonNull
                                                                                                        io.micronaut.core.beans.BeanIntrospection<? extends T> introspection,
                                                                                                        @NonNull
                                                                                                        java.lang.Object[] parameterValues,
                                                                                                        @Nullable
                                                                                                        java.lang.Class<?>... groups)
        Description copied from interface: ExecutableMethodValidator
        Validates parameters for the given introspection and values.
        Specified by:
        validateConstructorParameters in interface ExecutableMethodValidator
        Type Parameters:
        T - The bean type.
        Parameters:
        introspection - The introspection
        parameterValues - The parameter values
        groups - The groups
        Returns:
        The constraint violations
      • validateConstructorParameters

        public <T> java.util.Set<javax.validation.ConstraintViolation<T>> validateConstructorParameters​(java.lang.Class<? extends T> beanType,
                                                                                                        io.micronaut.core.type.Argument<?>[] constructorArguments,
                                                                                                        @NonNull
                                                                                                        java.lang.Object[] parameterValues,
                                                                                                        @Nullable
                                                                                                        java.lang.Class<?>[] groups)
        Description copied from interface: ExecutableMethodValidator
        Validates arguments for the given bean type and constructor arguments.
        Specified by:
        validateConstructorParameters in interface ExecutableMethodValidator
        Type Parameters:
        T - The generic type of the bean
        Parameters:
        beanType - The bean type
        constructorArguments - The constructor arguments
        parameterValues - The parameter values
        groups - The validation groups
        Returns:
        A set of constraint violations, if any
      • validateConstructorReturnValue

        @NonNull
        public <T> java.util.Set<javax.validation.ConstraintViolation<T>> validateConstructorReturnValue​(@NonNull
                                                                                                         java.lang.reflect.Constructor<? extends T> constructor,
                                                                                                         @NonNull
                                                                                                         T createdObject,
                                                                                                         @Nullable
                                                                                                         java.lang.Class<?>... groups)
        Specified by:
        validateConstructorReturnValue in interface ExecutableMethodValidator
        Specified by:
        validateConstructorReturnValue in interface javax.validation.executable.ExecutableValidator
      • getBeanIntrospection

        @Nullable
        protected io.micronaut.core.beans.BeanIntrospection<java.lang.Object> getBeanIntrospection​(@NonNull
                                                                                                   java.lang.Object object,
                                                                                                   @NonNull
                                                                                                   java.lang.Class<?> definedClass)
        looks up a bean introspection for the given object by instance's class or defined class.
        Parameters:
        object - The object, never null
        definedClass - The defined class of the object, never null
        Returns:
        The introspection or null
      • getBeanIntrospection

        @Nullable
        protected io.micronaut.core.beans.BeanIntrospection<java.lang.Object> getBeanIntrospection​(@NonNull
                                                                                                   java.lang.Object object)
        looks up a bean introspection for the given object.
        Parameters:
        object - The object, never null
        Returns:
        The introspection or null
      • validatePublisher

        @NonNull
        public <T> org.reactivestreams.Publisher<T> validatePublisher​(@NonNull
                                                                      org.reactivestreams.Publisher<T> publisher,
                                                                      java.lang.Class<?>... groups)
        Description copied from interface: ReactiveValidator
        Validate the given publisher by returning a new Publisher that validates each emitted value. If a constraint violation error occurs a ConstraintViolationException will be thrown.
        Specified by:
        validatePublisher in interface ReactiveValidator
        Type Parameters:
        T - The generic type
        Parameters:
        publisher - The publisher
        groups - The groups
        Returns:
        The publisher
      • validateCompletionStage

        @NonNull
        public <T> java.util.concurrent.CompletionStage<T> validateCompletionStage​(@NonNull
                                                                                   java.util.concurrent.CompletionStage<T> completionStage,
                                                                                   java.lang.Class<?>... groups)
        Description copied from interface: ReactiveValidator
        Validate the given CompletionStage by returning a new CompletionStage that validates the emitted value. If a constraint violation error occurs a ConstraintViolationException will be thrown.
        Specified by:
        validateCompletionStage in interface ReactiveValidator
        Type Parameters:
        T - The generic type
        Parameters:
        completionStage - The completion stage
        groups - The groups
        Returns:
        The publisher
      • validateBeanArgument

        public <T> void validateBeanArgument​(@NonNull
                                             io.micronaut.context.BeanResolutionContext resolutionContext,
                                             @NonNull
                                             io.micronaut.inject.InjectionPoint injectionPoint,
                                             @NonNull
                                             io.micronaut.core.type.Argument<T> argument,
                                             int index,
                                             @Nullable
                                             T value)
                                      throws io.micronaut.context.exceptions.BeanInstantiationException
        Specified by:
        validateBeanArgument in interface io.micronaut.inject.validation.BeanDefinitionValidator
        Throws:
        io.micronaut.context.exceptions.BeanInstantiationException
      • validateBean

        public <T> void validateBean​(@NonNull
                                     io.micronaut.context.BeanResolutionContext resolutionContext,
                                     @NonNull
                                     io.micronaut.inject.BeanDefinition<T> definition,
                                     @NonNull
                                     T bean)
                              throws io.micronaut.context.exceptions.BeanInstantiationException
        Specified by:
        validateBean in interface io.micronaut.inject.validation.BeanDefinitionValidator
        Throws:
        io.micronaut.context.exceptions.BeanInstantiationException