Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a bean validation context to control property validation #317

Merged
merged 2 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright 2017-2024 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.validation.validator;

import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.beans.BeanProperty;
import java.util.Arrays;
import java.util.List;

/**
* Context object to allow configuring validation behaviour.
*/
public interface BeanValidationContext {
/**
* The default validation context.
*/
BeanValidationContext DEFAULT = new DefaultBeanValidationContext(List.of());

/**
* The validation groups.
* @return The groups
*/
default List<Class<?>> groups() {
return List.of();
}

/**
* Create a validation context from the given groups.
* @param groups The groups
* @return The context
*/
static @NonNull BeanValidationContext fromGroups(Class<?>... groups) {
return new DefaultBeanValidationContext(
groups != null ? Arrays.asList(groups) : List.of()
);
}

/**
* Hook to allow exclusion of properties during validation.
* @param object The object being validated
* @param property The property being validated.
* @return True if it should be validated.
* @param <T> The object type
*/
default <T> boolean isPropertyValidated(
@NonNull T object, @NonNull BeanProperty<T, Object> property) {
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2017-2024 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.validation.validator;

import io.micronaut.core.annotation.Internal;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.util.CollectionUtils;
import java.util.List;

/**
* A default implementation of {@link BeanValidationContext}.
* @param groups The groups
*/
@Internal
record DefaultBeanValidationContext(
@NonNull
List<Class<?>> groups)
implements BeanValidationContext {
public DefaultBeanValidationContext {
if (CollectionUtils.isEmpty(groups)) {
groups = List.of();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import io.micronaut.core.annotation.Nullable;
import io.micronaut.core.beans.BeanIntrospection;
import io.micronaut.core.util.ArgumentUtils;
import io.micronaut.core.util.ArrayUtils;
import io.micronaut.core.util.CollectionUtils;
import io.micronaut.validation.validator.constraints.ConstraintValidatorContext;
import jakarta.validation.ClockProvider;
import jakarta.validation.ConstraintViolation;
Expand Down Expand Up @@ -64,6 +64,7 @@ final class DefaultConstraintValidatorContext<R> implements ConstraintValidatorC
boolean disableDefaultConstraintViolation;
ConstraintDescriptor<Annotation> constraint;

private final BeanValidationContext validationContext;
private final DefaultValidator defaultValidator;
private final BeanIntrospection<R> beanIntrospection;
private final R rootBean;
Expand All @@ -84,33 +85,41 @@ final class DefaultConstraintValidatorContext<R> implements ConstraintValidatorC
private Map<Class<?>, Class<?>> convertedGroups = Collections.emptyMap();
private Set<ConstraintViolation<R>> currentViolations = new LinkedHashSet<>();

DefaultConstraintValidatorContext(DefaultValidator defaultValidator, BeanIntrospection<R> beanIntrospection, R rootBean, Class<?>... groups) {
this(defaultValidator, beanIntrospection, rootBean, null, null, new ValidationPath(), new LinkedHashSet<>(), processGroups(groups), Collections.emptyList());
DefaultConstraintValidatorContext(DefaultValidator defaultValidator, BeanIntrospection<R> beanIntrospection, R rootBean, BeanValidationContext validationContext) {
this(defaultValidator, beanIntrospection, validationContext, rootBean, null, new ValidationPath(), new LinkedHashSet<>(), null, Collections.emptyList());
}

private DefaultConstraintValidatorContext(DefaultValidator defaultValidator,
BeanIntrospection<R> beanIntrospection,
R rootBean,
Object[] executableParameterValues,
BeanValidationContext validationContext, R rootBean,
Object executableReturnValue,
ValidationPath path,
Set<ConstraintViolation<R>> overallViolations,
List<Class<?>> definedGroups,
Object[] executableParameterValues,
List<Class<?>> currentGroups) {
this.validationContext = validationContext;
this.defaultValidator = defaultValidator;
this.beanIntrospection = beanIntrospection;
this.rootBean = rootBean;
this.rootClass = beanIntrospection == null ? (rootBean == null ? null : (Class<R>) rootBean.getClass()) : beanIntrospection.getBeanType();
this.executableParameterValues = executableParameterValues;
this.executableReturnValue = executableReturnValue;
this.definedGroups = definedGroups;
this.definedGroups = processGroups(validationContext.groups());
this.currentGroups = currentGroups;
this.currentPath = path != null ? path : new ValidationPath();
this.overallViolations = overallViolations;
}

private static List<Class<?>> processGroups(Class<?>[] definedGroups) {
if (ArrayUtils.isEmpty(definedGroups)) {
/**
* The validation context.
* @return The context
*/
public @NonNull BeanValidationContext getValidationContext() {
return validationContext;
}

private static List<Class<?>> processGroups(List<Class<?>> definedGroups) {
if (CollectionUtils.isEmpty(definedGroups)) {
return DEFAULT_GROUPS;
}
sanityCheckGroups(definedGroups);
Expand All @@ -121,7 +130,7 @@ private static List<Class<?>> processGroups(Class<?>[] definedGroups) {
return Collections.unmodifiableList(groupList);
}

private static void sanityCheckGroups(Class<?>[] groups) {
private static void sanityCheckGroups(List<Class<?>> groups) {
ArgumentUtils.requireNonNull("groups", groups);

for (Class<?> clazz : groups) {
Expand Down Expand Up @@ -365,7 +374,7 @@ Optional<String> getMessageTemplate() {
}

DefaultConstraintValidatorContext<R> copy() {
return new DefaultConstraintValidatorContext<>(defaultValidator, beanIntrospection, rootBean, executableParameterValues, executableReturnValue, new ValidationPath(currentPath), new LinkedHashSet<>(overallViolations), definedGroups, currentGroups);
return new DefaultConstraintValidatorContext<>(defaultValidator, beanIntrospection, validationContext, rootBean, executableReturnValue, new ValidationPath(currentPath), new LinkedHashSet<>(overallViolations), executableParameterValues, currentGroups);
}

@Internal
Expand Down
Loading
Loading