Search in sources :

Example 6 with ValidatorFactory

use of jakarta.validation.ValidatorFactory in project minijax by minijax.

the class ValidationTest method setUpValidator.

@BeforeAll
public static void setUpValidator() {
    // tag::setUpValidator[]
    final ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
    validator = factory.getValidator();
// end::setUpValidator[]
}
Also used : ValidatorFactory(jakarta.validation.ValidatorFactory) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 7 with ValidatorFactory

use of jakarta.validation.ValidatorFactory in project hibernate-orm by hibernate.

the class BeanValidationProvidedFactoryTest method configure.

@Override
protected void configure(Configuration cfg) {
    super.configure(cfg);
    final MessageInterpolator messageInterpolator = new MessageInterpolator() {

        public String interpolate(String s, Context context) {
            return "Oops";
        }

        public String interpolate(String s, Context context, Locale locale) {
            return interpolate(s, context);
        }
    };
    final jakarta.validation.Configuration<?> configuration = Validation.byDefaultProvider().configure();
    configuration.messageInterpolator(messageInterpolator);
    ValidatorFactory vf = configuration.buildValidatorFactory();
    cfg.getProperties().put("jakarta.persistence.validation.factory", vf);
    cfg.setProperty("javax.persistence.validation.mode", "AUTO");
}
Also used : Locale(java.util.Locale) ValidatorFactory(jakarta.validation.ValidatorFactory) MessageInterpolator(jakarta.validation.MessageInterpolator)

Example 8 with ValidatorFactory

use of jakarta.validation.ValidatorFactory in project hibernate-orm by hibernate.

the class TypeSafeActivator method activate.

@SuppressWarnings("UnusedDeclaration")
public static void activate(ActivationContext activationContext) {
    final ValidatorFactory factory;
    try {
        factory = getValidatorFactory(activationContext);
    } catch (IntegrationException e) {
        if (activationContext.getValidationModes().contains(ValidationMode.CALLBACK)) {
            throw new IntegrationException("Bean Validation provider was not available, but 'callback' validation was requested", e);
        }
        if (activationContext.getValidationModes().contains(ValidationMode.DDL)) {
            throw new IntegrationException("Bean Validation provider was not available, but 'ddl' validation was requested", e);
        }
        LOG.debug("Unable to acquire Bean Validation ValidatorFactory, skipping activation");
        return;
    }
    applyRelationalConstraints(factory, activationContext);
    applyCallbackListeners(factory, activationContext);
}
Also used : ValidatorFactory(jakarta.validation.ValidatorFactory)

Example 9 with ValidatorFactory

use of jakarta.validation.ValidatorFactory in project hibernate-orm by hibernate.

the class TypeSafeActivator method getValidatorFactory.

private static ValidatorFactory getValidatorFactory(ActivationContext activationContext) {
    // IMPL NOTE : We can either be provided a ValidatorFactory or make one.  We can be provided
    // a ValidatorFactory in 2 different ways.  So here we "get" a ValidatorFactory in the following order:
    // 1) Look into SessionFactoryOptions.getValidatorFactoryReference()
    // 2) Look into ConfigurationService
    // 3) build a new ValidatorFactory
    // 1 - look in SessionFactoryOptions.getValidatorFactoryReference()
    ValidatorFactory factory = resolveProvidedFactory(activationContext.getSessionFactory().getSessionFactoryOptions());
    if (factory != null) {
        return factory;
    }
    // 2 - look in ConfigurationService
    factory = resolveProvidedFactory(activationContext.getServiceRegistry().getService(ConfigurationService.class));
    if (factory != null) {
        return factory;
    }
    // 3 - build our own
    try {
        return Validation.buildDefaultValidatorFactory();
    } catch (Exception e) {
        LOG.infof(e, "Error calling `%s`", "jakarta.validation.Validation#buildDefaultValidatorFactory");
        throw new IntegrationException("Unable to build the default ValidatorFactory", e);
    }
}
Also used : ValidatorFactory(jakarta.validation.ValidatorFactory) MappingException(org.hibernate.MappingException) ClassLoadingException(org.hibernate.boot.registry.classloading.spi.ClassLoadingException)

Example 10 with ValidatorFactory

use of jakarta.validation.ValidatorFactory in project gocd-groovy-dsl-config-plugin by gocd-contrib.

the class TestBase method validate.

protected static void validate(Object object, Consumer<Set<ConstraintViolation<Object>>> errorHandler) {
    ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
    Validator validator = validatorFactory.getValidator();
    Set<ConstraintViolation<Object>> violations = validator.validate(object);
    if (!violations.isEmpty()) {
        errorHandler.accept(violations);
    }
}
Also used : ValidatorFactory(jakarta.validation.ValidatorFactory) ConstraintViolation(jakarta.validation.ConstraintViolation) Validator(jakarta.validation.Validator)

Aggregations

ValidatorFactory (jakarta.validation.ValidatorFactory)161 Validator (jakarta.validation.Validator)79 Test (org.testng.annotations.Test)76 HibernateValidator (org.hibernate.validator.HibernateValidator)74 ConstraintViolation (jakarta.validation.ConstraintViolation)41 TestForIssue (org.hibernate.validator.testutil.TestForIssue)30 HibernateValidatorConfiguration (org.hibernate.validator.HibernateValidatorConfiguration)24 ConstraintValidator (jakarta.validation.ConstraintValidator)23 BeforeClass (org.junit.BeforeClass)23 Test (org.junit.Test)17 MessageInterpolator (jakarta.validation.MessageInterpolator)14 HibernateValidatorFactory (org.hibernate.validator.HibernateValidatorFactory)14 NotNull (jakarta.validation.constraints.NotNull)13 ConstraintValidatorFactory (jakarta.validation.ConstraintValidatorFactory)9 Locale (java.util.Locale)7 BeforeTest (org.testng.annotations.BeforeTest)7 ValidationException (jakarta.validation.ValidationException)6 PredefinedScopeHibernateValidator (org.hibernate.validator.PredefinedScopeHibernateValidator)6 BeforeAll (org.junit.jupiter.api.BeforeAll)6 BeforeMethod (org.testng.annotations.BeforeMethod)6