All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.quarkus.hibernate.validator.runtime.HibernateBeanValidationConfigValidator Maven / Gradle / Ivy

Go to download

Validate object properties (field, getter) and method parameters for your beans (REST, CDI, Jakarta Persistence)

There is a newer version: 3.15.0
Show newest version
package io.quarkus.hibernate.validator.runtime;

import java.util.Set;

import jakarta.validation.Validation;
import jakarta.validation.Validator;
import jakarta.validation.ValidatorFactory;

import org.hibernate.validator.PredefinedScopeHibernateValidator;
import org.hibernate.validator.PredefinedScopeHibernateValidatorConfiguration;
import org.hibernate.validator.internal.engine.constraintvalidation.ConstraintValidatorFactoryImpl;

import io.smallrye.config.validator.BeanValidationConfigValidator;

public class HibernateBeanValidationConfigValidator implements BeanValidationConfigValidator {

    public HibernateBeanValidationConfigValidator(Set constraints, Set> classesToBeValidated) {
        PredefinedScopeHibernateValidatorConfiguration configuration = Validation
                .byProvider(PredefinedScopeHibernateValidator.class)
                .configure();

        // TODO - There is no way to retrieve locales from configuration here (even manually). We need to add a way to configure the validator from SmallRye Config.
        configuration
                .ignoreXmlConfiguration()
                .builtinConstraints(constraints)
                .initializeBeanMetaData(classesToBeValidated)
                .constraintValidatorFactory(new ConstraintValidatorFactoryImpl());

        ConfigValidatorHolder.initialize(configuration.buildValidatorFactory());
    }

    @Override
    public Validator getValidator() {
        return ConfigValidatorHolder.getValidator();
    }

    // Store in a holder, so we can easily reference it and shutdown the validator
    public static class ConfigValidatorHolder {
        private static ValidatorFactory validatorFactory;
        private static Validator validator;

        static void initialize(ValidatorFactory validatorFactory) {
            ConfigValidatorHolder.validatorFactory = validatorFactory;
            ConfigValidatorHolder.validator = validatorFactory.getValidator();
        }

        static ValidatorFactory getValidatorFactory() {
            return validatorFactory;
        }

        static Validator getValidator() {
            return validator;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy