Search in sources :

Example 1 with ConstraintValidator

use of io.micronaut.validation.validator.constraints.ConstraintValidator in project micronaut-core by micronaut-projects.

the class DefaultValidator method validateParameterInternal.

@SuppressWarnings("unchecked")
private <T> void validateParameterInternal(@NonNull Class<T> rootClass, @Nullable T object, @NonNull Object[] argumentValues, @NonNull DefaultConstraintValidatorContext context, @NonNull Set overallViolations, @NonNull String parameterName, @NonNull Class<?> parameterType, int parameterIndex, @NonNull AnnotationMetadata annotationMetadata, @Nullable Object parameterValue) {
    final String currentMessageTemplate = context.getMessageTemplate().orElse(null);
    try {
        context.addParameterNode(parameterName, parameterIndex);
        final List<Class<? extends Annotation>> constraintTypes = annotationMetadata.getAnnotationTypesByStereotype(Constraint.class);
        // Constraints applied to the parameter
        for (Class<? extends Annotation> constraintType : constraintTypes) {
            final ConstraintValidator constraintValidator = constraintValidatorRegistry.findConstraintValidator(constraintType, parameterType).orElse(null);
            if (constraintValidator != null) {
                final AnnotationValue<? extends Annotation> annotationValue = annotationMetadata.getAnnotation(constraintType);
                if (annotationValue != null && !constraintValidator.isValid(parameterValue, annotationValue, context)) {
                    final String messageTemplate = buildMessageTemplate(context, annotationValue, annotationMetadata);
                    final Map<String, Object> variables = newConstraintVariables(annotationValue, parameterValue, annotationMetadata);
                    overallViolations.add(new DefaultConstraintViolation(object, rootClass, object, parameterValue, messageSource.interpolate(messageTemplate, MessageSource.MessageContext.of(variables)), messageTemplate, new PathImpl(context.currentPath), new DefaultConstraintDescriptor(annotationMetadata, constraintType, annotationValue), argumentValues));
                }
            }
        }
    } finally {
        context.removeLast();
        context.messageTemplate(currentMessageTemplate);
    }
}
Also used : ConstraintValidator(io.micronaut.validation.validator.constraints.ConstraintValidator) Annotation(java.lang.annotation.Annotation)

Example 2 with ConstraintValidator

use of io.micronaut.validation.validator.constraints.ConstraintValidator in project micronaut-core by micronaut-projects.

the class DefaultValidator method validatePojoInternal.

@SuppressWarnings("unchecked")
private <T> void validatePojoInternal(@NonNull Class<T> rootClass, @Nullable T object, @Nullable Object[] argumentValues, @NonNull DefaultConstraintValidatorContext context, @NonNull Set overallViolations, @NonNull Class<?> parameterType, @NonNull Object parameterValue, Class<? extends Annotation> pojoConstraint, AnnotationValue constraintAnnotation) {
    final ConstraintValidator constraintValidator = constraintValidatorRegistry.findConstraintValidator(pojoConstraint, parameterType).orElse(null);
    if (constraintValidator != null) {
        final String currentMessageTemplate = context.getMessageTemplate().orElse(null);
        if (!constraintValidator.isValid(parameterValue, constraintAnnotation, context)) {
            BeanIntrospection<Object> beanIntrospection = getBeanIntrospection(parameterValue);
            if (beanIntrospection == null) {
                throw new ValidationException("Passed object [" + parameterValue + "] cannot be introspected. Please annotate with @Introspected");
            }
            AnnotationMetadata beanAnnotationMetadata = beanIntrospection.getAnnotationMetadata();
            AnnotationValue<? extends Annotation> annotationValue = beanAnnotationMetadata.getAnnotation(pojoConstraint);
            final String propertyValue = "";
            final String messageTemplate = buildMessageTemplate(context, annotationValue, beanAnnotationMetadata);
            final Map<String, Object> variables = newConstraintVariables(annotationValue, propertyValue, beanAnnotationMetadata);
            overallViolations.add(new DefaultConstraintViolation(object, rootClass, object, parameterValue, messageSource.interpolate(messageTemplate, MessageSource.MessageContext.of(variables)), messageTemplate, new PathImpl(context.currentPath), new DefaultConstraintDescriptor(beanAnnotationMetadata, pojoConstraint, annotationValue), argumentValues));
        }
        context.messageTemplate(currentMessageTemplate);
    }
}
Also used : ConstraintValidator(io.micronaut.validation.validator.constraints.ConstraintValidator) ValidationException(javax.validation.ValidationException) AnnotationMetadata(io.micronaut.core.annotation.AnnotationMetadata)

Aggregations

ConstraintValidator (io.micronaut.validation.validator.constraints.ConstraintValidator)2 AnnotationMetadata (io.micronaut.core.annotation.AnnotationMetadata)1 Annotation (java.lang.annotation.Annotation)1 ValidationException (javax.validation.ValidationException)1