use of io.micronaut.validation.validator.extractors.SimpleValueReceiver in project micronaut-core by micronaut-projects.
the class DefaultValidator method validatePropertyInternal.
private <T> void validatePropertyInternal(@Nullable Class<T> rootBeanClass, @Nullable T rootBean, @Nullable Object object, @NonNull DefaultConstraintValidatorContext context, @NonNull Set<ConstraintViolation<Object>> overallViolations, @NonNull Class propertyType, @NonNull AnnotatedElement constrainedProperty, @Nullable Object propertyValue) {
final AnnotationMetadata annotationMetadata = constrainedProperty.getAnnotationMetadata();
final List<Class<? extends Annotation>> constraintTypes = annotationMetadata.getAnnotationTypesByStereotype(Constraint.class);
for (Class<? extends Annotation> constraintType : constraintTypes) {
ValueExtractor<Object> valueExtractor = null;
if (propertyValue != null && !annotationMetadata.hasAnnotation(Valid.class)) {
// noinspection unchecked
valueExtractor = valueExtractorRegistry.findUnwrapValueExtractor((Class<Object>) propertyValue.getClass()).orElse(null);
}
if (valueExtractor != null) {
valueExtractor.extractValues(propertyValue, (SimpleValueReceiver) (nodeName, extractedValue) -> valueConstraintOnProperty(rootBeanClass, rootBean, object, context, overallViolations, constrainedProperty, propertyType, extractedValue, constraintType));
} else {
valueConstraintOnProperty(rootBeanClass, rootBean, object, context, overallViolations, constrainedProperty, propertyType, propertyValue, constraintType);
}
}
}
use of io.micronaut.validation.validator.extractors.SimpleValueReceiver in project micronaut-core by micronaut-projects.
the class DefaultValidator method validateParametersInternal.
private <T> void validateParametersInternal(@NonNull Class<T> rootClass, @Nullable T object, @NonNull Object[] parameters, Argument[] arguments, int argLen, DefaultConstraintValidatorContext context, Set overallViolations, Path.Node parentNode) {
for (int i = 0; i < argLen; i++) {
Argument argument = arguments[i];
final Class<?> parameterType = argument.getType();
final AnnotationMetadata annotationMetadata = argument.getAnnotationMetadata();
final boolean hasValid = annotationMetadata.hasStereotype(Validator.ANN_VALID);
final boolean hasConstraint = annotationMetadata.hasStereotype(Validator.ANN_CONSTRAINT);
if (!hasValid && !hasConstraint) {
continue;
}
Object parameterValue = parameters[i];
ValueExtractor<Object> valueExtractor = null;
final boolean hasValue = parameterValue != null;
final boolean isValid = hasValue && hasValid;
final boolean isPublisher = hasValue && Publishers.isConvertibleToPublisher(parameterType);
if (isPublisher) {
instrumentPublisherArgumentWithValidation(rootClass, object, parameters, context, i, argument, parameterType, annotationMetadata, parameterValue, isValid);
} else {
final boolean isCompletionStage = hasValue && CompletionStage.class.isAssignableFrom(parameterType);
if (isCompletionStage) {
instrumentCompletionStageArgumentWithValidation(rootClass, object, parameters, context, i, argument, annotationMetadata, parameterValue, isValid);
} else {
if (hasValue) {
// noinspection unchecked
valueExtractor = (ValueExtractor<Object>) valueExtractorRegistry.findUnwrapValueExtractor(parameterType).orElse(null);
}
int finalIndex = i;
if (valueExtractor != null) {
valueExtractor.extractValues(parameterValue, (SimpleValueReceiver) (nodeName, unwrappedValue) -> validateParameterInternal(rootClass, object, parameters, context, overallViolations, argument.getName(), unwrappedValue == null ? Object.class : unwrappedValue.getClass(), finalIndex, annotationMetadata, unwrappedValue));
} else {
validateParameterInternal(rootClass, object, parameters, context, overallViolations, argument.getName(), parameterType, finalIndex, annotationMetadata, parameterValue);
}
if (isValid) {
if (context.validatedObjects.contains(parameterValue)) {
// already validated
continue;
}
// cascade to bean
// noinspection unchecked
valueExtractor = (ValueExtractor<Object>) valueExtractorRegistry.findValueExtractor(parameterType).orElse(null);
if (valueExtractor != null) {
valueExtractor.extractValues(parameterValue, new ValueExtractor.ValueReceiver() {
@Override
public void value(String nodeName, Object object1) {
}
@Override
public void iterableValue(String nodeName, Object iterableValue) {
if (iterableValue != null && context.validatedObjects.contains(iterableValue)) {
return;
}
cascadeToIterableValue(context, rootClass, object, parameterValue, parentNode, argument, iterableValue, overallViolations, null, null, true);
}
@Override
public void indexedValue(String nodeName, int i, Object iterableValue) {
if (iterableValue != null && context.validatedObjects.contains(iterableValue)) {
return;
}
cascadeToIterableValue(context, rootClass, object, parameterValue, parentNode, argument, iterableValue, overallViolations, i, null, true);
}
@Override
public void keyedValue(String nodeName, Object key, Object keyedValue) {
if (keyedValue != null && context.validatedObjects.contains(keyedValue)) {
return;
}
cascadeToIterableValue(context, rootClass, object, parameterValue, parentNode, argument, keyedValue, overallViolations, null, key, false);
}
});
} else {
final BeanIntrospection<Object> beanIntrospection = getBeanIntrospection(parameterValue, parameterType);
if (beanIntrospection != null) {
try {
context.addParameterNode(argument.getName(), i);
cascadeToOneIntrospection(context, object, parameterValue, beanIntrospection, overallViolations);
} finally {
context.removeLast();
}
} else {
context.addParameterNode(argument.getName(), i);
overallViolations.add(createIntrospectionConstraintViolation(rootClass, object, context, parameterType, parameterValue, parameters));
context.removeLast();
}
}
}
}
}
}
}
Aggregations