io.micronaut.validation.validator.constraints.DefaultConstraintValidators Maven / Gradle / Ivy
/*
* Copyright 2017-2020 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.constraints;
import io.micronaut.context.BeanContext;
import io.micronaut.context.Qualifier;
import io.micronaut.core.annotation.Introspected;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.core.reflect.ReflectionUtils;
import io.micronaut.core.type.Argument;
import io.micronaut.core.util.ArgumentUtils;
import io.micronaut.core.util.ArrayUtils;
import io.micronaut.core.util.CollectionUtils;
import io.micronaut.core.util.clhm.ConcurrentLinkedHashMap;
import io.micronaut.inject.qualifiers.Qualifiers;
import io.micronaut.inject.qualifiers.TypeArgumentQualifier;
import jakarta.inject.Inject;
import jakarta.inject.Singleton;
import jakarta.validation.constraints.DecimalMax;
import jakarta.validation.constraints.DecimalMin;
import jakarta.validation.constraints.Digits;
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.Pattern;
import jakarta.validation.constraints.Size;
import java.lang.annotation.Annotation;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
/**
* A factory bean that contains implementation for many of the default validations.
* This approach is preferred as it generates less classes and smaller byte code than defining a
* validator class for each case.
*
* @author graemerocher
* @since 1.2
*/
@Singleton
@Introspected
public class DefaultConstraintValidators implements ConstraintValidatorRegistry {
private final Map> validatorCache = new ConcurrentLinkedHashMap.
Builder>().initialCapacity(10).maximumWeightedCapacity(40).build();
@Nullable
private final BeanContext beanContext;
private final Map> internalValidators;
/**
* Default constructor.
*/
public DefaultConstraintValidators() {
this(null);
}
/**
* Constructor used for DI.
*
* @param beanContext The bean context
*/
@Inject
public DefaultConstraintValidators(@Nullable BeanContext beanContext) {
this.beanContext = beanContext;
List, ConstraintValidator, ?>>> constraintValidators = InternalConstraintValidators.getConstraintValidators();
Map> validatorMap = CollectionUtils.newHashMap(constraintValidators.size());
for (Map.Entry, ConstraintValidator, ?>> entry : constraintValidators) {
Argument
© 2015 - 2025 Weber Informatics LLC | Privacy Policy