Class DefaultValidator
java.lang.Object
io.micronaut.validation.validator.DefaultValidator
- All Implemented Interfaces:
io.micronaut.inject.annotation.AnnotatedElementValidator
,io.micronaut.inject.validation.BeanDefinitionValidator
,ExecutableMethodValidator
,ReactiveValidator
,Validator
,jakarta.validation.executable.ExecutableValidator
,jakarta.validation.Validator
- Direct Known Subclasses:
DefaultAnnotatedElementValidator
@Singleton
@Primary
@Requires(property="micronaut.validator.enabled",
value="true",
defaultValue="true")
public class DefaultValidator
extends Object
implements Validator, ExecutableMethodValidator, ReactiveValidator, io.micronaut.inject.annotation.AnnotatedElementValidator, io.micronaut.inject.validation.BeanDefinitionValidator
Default implementation of the
Validator
interface.- Since:
- 1.2
- Author:
- graemerocher, Andriy Dmytruk
-
Field Summary
Fields inherited from interface io.micronaut.inject.validation.BeanDefinitionValidator
DEFAULT
Fields inherited from interface io.micronaut.validation.validator.Validator
ANN_CONSTRAINT, ANN_VALID
-
Constructor Summary
ConstructorDescriptionDefaultValidator
(@NonNull ValidatorConfiguration configuration) Default constructor. -
Method Summary
Modifier and TypeMethodDescription<T> T
createValid
(@NonNull Class<T> beanType, Object... arguments) Create a new valid instance.@NonNull ExecutableMethodValidator
Overridden variation that returns aExecutableMethodValidator
.protected <T> @Nullable io.micronaut.core.beans.BeanIntrospection<T>
getBeanIntrospection
(@NonNull Class<T> type) Looks up a bean introspection for the given object.protected <T> @Nullable io.micronaut.core.beans.BeanIntrospection<T>
getBeanIntrospection
(T object) Looks up a bean introspection for the given object.protected <T> @Nullable io.micronaut.core.beans.BeanIntrospection<T>
getBeanIntrospection
(T object, @NonNull Class<T> definedClass) looks up a bean introspection for the given object by instance's class or defined class.io.micronaut.core.beans.BeanIntrospector
jakarta.validation.metadata.BeanDescriptor
getConstraintsForClass
(Class<?> clazz) static String
requireNonEmpty
(String name, String value) Deprecated.It will be private in a future version.static <T> T
requireNonNull
(String name, T value) Deprecated.It will be private in a future version.<T> T
<T> @NonNull Set<jakarta.validation.ConstraintViolation<T>>
validate
(@NonNull io.micronaut.core.beans.BeanIntrospection<T> introspection, T object, @NonNull Class<?>... groups) Validate the given introspection and object.<T> @NonNull Set<jakarta.validation.ConstraintViolation<T>>
<T> void
validateBean
(@NonNull io.micronaut.context.BeanResolutionContext resolutionContext, @NonNull io.micronaut.inject.BeanDefinition<T> definition, T bean) <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, T value) <T> @NonNull CompletionStage<T>
validateCompletionStage
(@NonNull CompletionStage<T> completionStage, @NonNull io.micronaut.core.type.Argument<T> argument, @NonNull Class<?>... groups) Validate the given CompletionStage by returning a new CompletionStage that validates the emitted value.<T> @NonNull Set<jakarta.validation.ConstraintViolation<T>>
validateConstructorParameters
(@NonNull io.micronaut.core.beans.BeanIntrospection<? extends T> introspection, @NonNull Object[] parameterValues, @NonNull Class<?>... groups) Validates parameters for the given introspection and values.<T> @NonNull Set<jakarta.validation.ConstraintViolation<T>>
validateConstructorParameters
(@NonNull Constructor<? extends T> constructor, @NonNull Object[] parameterValues, @NonNull Class<?>... groups) <T> Set<jakarta.validation.ConstraintViolation<T>>
validateConstructorParameters
(Class<? extends T> beanType, io.micronaut.core.type.Argument<?>[] constructorArguments, @NonNull Object[] parameterValues, @NonNull Class<?>[] groups) Validates arguments for the given bean type and constructor arguments.<T> @NonNull Set<jakarta.validation.ConstraintViolation<T>>
validateConstructorReturnValue
(@NonNull Constructor<? extends T> constructor, T createdObject, @NonNull Class<?>... groups) validatedAnnotatedElement
(@NonNull io.micronaut.core.annotation.AnnotatedElement element, @Nullable Object value) <T> @NonNull Set<jakarta.validation.ConstraintViolation<T>>
validateParameters
(T object, @NonNull io.micronaut.inject.ExecutableMethod method, @NonNull Object[] parameterValues, @NonNull Class<?>... groups) Validate the parameter values of the givenExecutableMethod
.<T> @NonNull Set<jakarta.validation.ConstraintViolation<T>>
validateParameters
(T object, @NonNull io.micronaut.inject.ExecutableMethod method, @NonNull Collection<io.micronaut.core.type.MutableArgumentValue<?>> argumentValues, @NonNull Class<?>... groups) Validate the parameter values of the givenExecutableMethod
.<T> @NonNull Set<jakarta.validation.ConstraintViolation<T>>
validateParameters
(T object, @NonNull Method method, @NonNull Object[] parameterValues, @NonNull Class<?>... groups) <T> @NonNull Set<jakarta.validation.ConstraintViolation<T>>
validateProperty
(T object, @NonNull String propertyName, @NonNull Class<?>... groups) <T> @NonNull org.reactivestreams.Publisher<T>
validatePublisher
(@NonNull io.micronaut.core.type.ReturnType<?> returnType, @NonNull org.reactivestreams.Publisher<T> publisher, @NonNull Class<?>... groups) Validate the given publisher by returning a new Publisher that validates each emitted value.<T> @NonNull Set<jakarta.validation.ConstraintViolation<T>>
validateReturnValue
(T bean, @NonNull io.micronaut.inject.ExecutableMethod<?, Object> executableMethod, @Nullable Object returnValue, @NonNull Class<?>... groups) Validates the return value of aExecutableMethod
.<T> @NonNull Set<jakarta.validation.ConstraintViolation<T>>
validateReturnValue
(T object, @NonNull Method method, @Nullable Object returnValue, @NonNull Class<?>... groups) <T> @NonNull Set<jakarta.validation.ConstraintViolation<T>>
validateValue
(@NonNull Class<T> beanType, @NonNull String propertyName, @Nullable Object value, @NonNull Class<?>... groups)
-
Constructor Details
-
DefaultValidator
Default constructor.- Parameters:
configuration
- The validator configuration
-
-
Method Details
-
getBeanIntrospector
public io.micronaut.core.beans.BeanIntrospector getBeanIntrospector()- Returns:
- The bean introspector
-
validate
-
validate
@NonNull public <T> @NonNull Set<jakarta.validation.ConstraintViolation<T>> validate(@NonNull @NonNull io.micronaut.core.beans.BeanIntrospection<T> introspection, @NonNull T object, @NonNull @NonNull Class<?>... groups) Validate the given introspection and object. -
validateProperty
@NonNull public <T> @NonNull Set<jakarta.validation.ConstraintViolation<T>> validateProperty(@NonNull T object, @NonNull @NonNull String propertyName, @NonNull @NonNull Class<?>... groups) - Specified by:
validateProperty
in interfaceValidator
- Specified by:
validateProperty
in interfacejakarta.validation.Validator
-
validateValue
@NonNull public <T> @NonNull Set<jakarta.validation.ConstraintViolation<T>> validateValue(@NonNull @NonNull Class<T> beanType, @NonNull @NonNull String propertyName, @Nullable @Nullable Object value, @NonNull @NonNull Class<?>... groups) - Specified by:
validateValue
in interfaceValidator
- Specified by:
validateValue
in interfacejakarta.validation.Validator
-
validatedAnnotatedElement
@NonNull public @NonNull Set<String> validatedAnnotatedElement(@NonNull @NonNull io.micronaut.core.annotation.AnnotatedElement element, @Nullable @Nullable Object value) - Specified by:
validatedAnnotatedElement
in interfaceio.micronaut.inject.annotation.AnnotatedElementValidator
-
createValid
@NonNull public <T> T createValid(@NonNull @NonNull Class<T> beanType, Object... arguments) throws jakarta.validation.ConstraintViolationException Description copied from interface:ExecutableMethodValidator
Create a new valid instance.- Specified by:
createValid
in interfaceExecutableMethodValidator
- Type Parameters:
T
- the generic type- Parameters:
beanType
- The typearguments
- The arguments- Returns:
- The instance
- Throws:
jakarta.validation.ConstraintViolationException
- If a valid instance couldn't be constructed
-
getConstraintsForClass
- Specified by:
getConstraintsForClass
in interfacejakarta.validation.Validator
-
unwrap
- Specified by:
unwrap
in interfacejakarta.validation.Validator
-
forExecutables
Description copied from interface:Validator
Overridden variation that returns aExecutableMethodValidator
.- Specified by:
forExecutables
in interfaceValidator
- Specified by:
forExecutables
in interfacejakarta.validation.Validator
- Returns:
- The validator
-
validateParameters
@NonNull public <T> @NonNull Set<jakarta.validation.ConstraintViolation<T>> validateParameters(@NonNull T object, @NonNull @NonNull io.micronaut.inject.ExecutableMethod method, @NonNull @NonNull Object[] parameterValues, @NonNull @NonNull Class<?>... groups) Description copied from interface:ExecutableMethodValidator
Validate the parameter values of the givenExecutableMethod
.- Specified by:
validateParameters
in interfaceExecutableMethodValidator
- Type Parameters:
T
- The object type- Parameters:
object
- The objectmethod
- The methodparameterValues
- The valuesgroups
- The groups- Returns:
- The constraint violations.
-
validateParameters
@NonNull public <T> @NonNull Set<jakarta.validation.ConstraintViolation<T>> validateParameters(@NonNull T object, @NonNull @NonNull io.micronaut.inject.ExecutableMethod method, @NonNull @NonNull Collection<io.micronaut.core.type.MutableArgumentValue<?>> argumentValues, @NonNull @NonNull Class<?>... groups) Description copied from interface:ExecutableMethodValidator
Validate the parameter values of the givenExecutableMethod
.- Specified by:
validateParameters
in interfaceExecutableMethodValidator
- Type Parameters:
T
- The object type- Parameters:
object
- The objectmethod
- The methodargumentValues
- The valuesgroups
- The groups- Returns:
- The constraint violations.
-
validateParameters
@NonNull public <T> @NonNull Set<jakarta.validation.ConstraintViolation<T>> validateParameters(@NonNull T object, @NonNull @NonNull Method method, @NonNull @NonNull Object[] parameterValues, @NonNull @NonNull Class<?>... groups) - Specified by:
validateParameters
in interfaceExecutableMethodValidator
- Specified by:
validateParameters
in interfacejakarta.validation.executable.ExecutableValidator
-
validateReturnValue
@NonNull public <T> @NonNull Set<jakarta.validation.ConstraintViolation<T>> validateReturnValue(@NonNull T object, @NonNull @NonNull Method method, @Nullable @Nullable Object returnValue, @NonNull @NonNull Class<?>... groups) - Specified by:
validateReturnValue
in interfaceExecutableMethodValidator
- Specified by:
validateReturnValue
in interfacejakarta.validation.executable.ExecutableValidator
-
validateReturnValue
@NonNull public <T> @NonNull Set<jakarta.validation.ConstraintViolation<T>> validateReturnValue(@NonNull T bean, @NonNull @NonNull io.micronaut.inject.ExecutableMethod<?, Object> executableMethod, @Nullable @Nullable Object returnValue, @NonNull @NonNull Class<?>... groups) Description copied from interface:ExecutableMethodValidator
Validates the return value of aExecutableMethod
.- Specified by:
validateReturnValue
in interfaceExecutableMethodValidator
- Type Parameters:
T
- The object type- Parameters:
bean
- The objectexecutableMethod
- The methodreturnValue
- The return valuegroups
- The groups- Returns:
- A set of contstraint violations
-
validateConstructorParameters
@NonNull public <T> @NonNull Set<jakarta.validation.ConstraintViolation<T>> validateConstructorParameters(@NonNull @NonNull Constructor<? extends T> constructor, @NonNull @NonNull Object[] parameterValues, @NonNull @NonNull Class<?>... groups) - Specified by:
validateConstructorParameters
in interfaceExecutableMethodValidator
- Specified by:
validateConstructorParameters
in interfacejakarta.validation.executable.ExecutableValidator
-
validateConstructorParameters
@NonNull public <T> @NonNull Set<jakarta.validation.ConstraintViolation<T>> validateConstructorParameters(@NonNull @NonNull io.micronaut.core.beans.BeanIntrospection<? extends T> introspection, @NonNull @NonNull Object[] parameterValues, @NonNull @NonNull Class<?>... groups) Description copied from interface:ExecutableMethodValidator
Validates parameters for the given introspection and values.- Specified by:
validateConstructorParameters
in interfaceExecutableMethodValidator
- Type Parameters:
T
- The bean type.- Parameters:
introspection
- The introspectionparameterValues
- The parameter valuesgroups
- The groups- Returns:
- The constraint violations
-
validateConstructorParameters
public <T> Set<jakarta.validation.ConstraintViolation<T>> validateConstructorParameters(Class<? extends T> beanType, io.micronaut.core.type.Argument<?>[] constructorArguments, @NonNull @NonNull Object[] parameterValues, @NonNull @NonNull Class<?>[] groups) Description copied from interface:ExecutableMethodValidator
Validates arguments for the given bean type and constructor arguments.- Specified by:
validateConstructorParameters
in interfaceExecutableMethodValidator
- Type Parameters:
T
- The generic type of the bean- Parameters:
beanType
- The bean typeconstructorArguments
- The constructor argumentsparameterValues
- The parameter valuesgroups
- The validation groups- Returns:
- A set of constraint violations, if any
-
validateConstructorReturnValue
@NonNull public <T> @NonNull Set<jakarta.validation.ConstraintViolation<T>> validateConstructorReturnValue(@NonNull @NonNull Constructor<? extends T> constructor, @NonNull T createdObject, @NonNull @NonNull Class<?>... groups) - Specified by:
validateConstructorReturnValue
in interfaceExecutableMethodValidator
- Specified by:
validateConstructorReturnValue
in interfacejakarta.validation.executable.ExecutableValidator
-
validatePublisher
@NonNull public <T> @NonNull org.reactivestreams.Publisher<T> validatePublisher(@NonNull @NonNull io.micronaut.core.type.ReturnType<?> returnType, @NonNull @NonNull org.reactivestreams.Publisher<T> publisher, @NonNull @NonNull 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 aConstraintViolationException
will be thrown.- Specified by:
validatePublisher
in interfaceReactiveValidator
- Type Parameters:
T
- The generic type- Parameters:
returnType
- The required type of publisherpublisher
- The publishergroups
- The groups- Returns:
- The publisher
-
validateCompletionStage
@NonNull public <T> @NonNull CompletionStage<T> validateCompletionStage(@NonNull @NonNull CompletionStage<T> completionStage, @NonNull @NonNull io.micronaut.core.type.Argument<T> argument, @NonNull @NonNull 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 aConstraintViolationException
will be thrown.- Specified by:
validateCompletionStage
in interfaceReactiveValidator
- Type Parameters:
T
- The generic type- Parameters:
completionStage
- The completion stageargument
- The completion stage element argumentgroups
- The groups- Returns:
- The publisher
-
validateBeanArgument
public <T> void validateBeanArgument(@NonNull @NonNull io.micronaut.context.BeanResolutionContext resolutionContext, @NonNull @NonNull io.micronaut.inject.InjectionPoint injectionPoint, @NonNull @NonNull io.micronaut.core.type.Argument<T> argument, int index, @Nullable T value) throws io.micronaut.context.exceptions.BeanInstantiationException - Specified by:
validateBeanArgument
in interfaceio.micronaut.inject.validation.BeanDefinitionValidator
- Throws:
io.micronaut.context.exceptions.BeanInstantiationException
-
validateBean
public <T> void validateBean(@NonNull @NonNull io.micronaut.context.BeanResolutionContext resolutionContext, @NonNull @NonNull io.micronaut.inject.BeanDefinition<T> definition, @NonNull T bean) throws io.micronaut.context.exceptions.BeanInstantiationException - Specified by:
validateBean
in interfaceio.micronaut.inject.validation.BeanDefinitionValidator
- Throws:
io.micronaut.context.exceptions.BeanInstantiationException
-
getBeanIntrospection
@Nullable protected <T> @Nullable io.micronaut.core.beans.BeanIntrospection<T> getBeanIntrospection(@NonNull T object, @NonNull @NonNull Class<T> definedClass) looks up a bean introspection for the given object by instance's class or defined class.- Type Parameters:
T
- The introspection type- Parameters:
object
- The object, never nulldefinedClass
- The defined class of the object, never null- Returns:
- The introspection or null
-
getBeanIntrospection
@Nullable protected <T> @Nullable io.micronaut.core.beans.BeanIntrospection<T> getBeanIntrospection(@NonNull T object) Looks up a bean introspection for the given object.- Type Parameters:
T
- The introspection type- Parameters:
object
- The object, never null- Returns:
- The introspection or null
-
getBeanIntrospection
@Nullable protected <T> @Nullable io.micronaut.core.beans.BeanIntrospection<T> getBeanIntrospection(@NonNull @NonNull Class<T> type) Looks up a bean introspection for the given object.- Type Parameters:
T
- The introspection type- Parameters:
type
- The object type- Returns:
- The introspection or null
-
requireNonNull
Deprecated.It will be private in a future version.Throws aIllegalArgumentException
if the is null.- Type Parameters:
T
- value Type- Parameters:
name
- check namevalue
- value being checked- Returns:
- the value
-
requireNonEmpty
Deprecated.It will be private in a future version.Throws aIllegalArgumentException
if the null or an empty string.- Parameters:
name
- check namevalue
- value being checked- Returns:
- the value
-