Search in sources :

Example 6 with ValidatorFactory

use of javax.validation.ValidatorFactory in project torodb by torodb.

the class ConfigUtils method validateBean.

public static <T> void validateBean(T config) {
    ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
    Validator validator = factory.getValidator();
    Set<ConstraintViolation<T>> constraintViolations = validator.validate(config);
    if (!constraintViolations.isEmpty()) {
        IllegalArgumentException illegalArgumentException = transformConstraintsValidation(constraintViolations);
        throw illegalArgumentException;
    }
}
Also used : ValidatorFactory(javax.validation.ValidatorFactory) ConstraintViolation(javax.validation.ConstraintViolation) Validator(javax.validation.Validator)

Example 7 with ValidatorFactory

use of javax.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(javax.validation.ValidatorFactory)

Example 8 with ValidatorFactory

use of javax.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) {
        throw new IntegrationException("Unable to build the default ValidatorFactory", e);
    }
}
Also used : ValidatorFactory(javax.validation.ValidatorFactory) MappingException(org.hibernate.MappingException) ClassLoadingException(org.hibernate.boot.registry.classloading.spi.ClassLoadingException)

Example 9 with ValidatorFactory

use of javax.validation.ValidatorFactory in project fb-botmill by BotMill.

the class FbBotMillBean method validate.

/**
	 * Validates the {@link FbBotMillResponse}.
	 *
	 * @param response
	 *            the response
	 * @return true if the response is valid, false otherwise.
	 */
protected boolean validate(FbBotMillResponse response) {
    // If validations are not enabled, returns true.
    if (!FbBotMillContext.getInstance().isValidationEnabled()) {
        return true;
    }
    boolean valid = true;
    ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
    Validator validator = factory.getValidator();
    Set<ConstraintViolation<FbBotMillResponse>> violations = validator.validate(response);
    for (ConstraintViolation<FbBotMillResponse> v : violations) {
        valid = false;
        logger.error("FbBotMillResponse validation error. Message: [{}] Value: [{}], Class: [{}], Field: [{}]", v.getMessage(), v.getInvalidValue(), v.getRootBean(), v.getPropertyPath());
    }
    if (valid == false) {
        // Sends the constraint violations through the callback.
        List<FbBotMillMonitor> registeredMonitors = FbBotMillContext.getInstance().getRegisteredMonitors();
        for (FbBotMillMonitor monitor : registeredMonitors) {
            monitor.onValidationError(response, violations);
        }
    }
    return valid;
}
Also used : ValidatorFactory(javax.validation.ValidatorFactory) FbBotMillResponse(co.aurasphere.botmill.fb.model.outcoming.FbBotMillResponse) ConstraintViolation(javax.validation.ConstraintViolation) FbBotMillMonitor(co.aurasphere.botmill.fb.support.FbBotMillMonitor) Validator(javax.validation.Validator)

Example 10 with ValidatorFactory

use of javax.validation.ValidatorFactory in project ORCID-Source by ORCID.

the class ChangeSecurityDetailsValidatorTest method resetValidator.

@Before
public void resetValidator() {
    ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
    validator = factory.getValidator();
}
Also used : ValidatorFactory(javax.validation.ValidatorFactory) Before(org.junit.Before)

Aggregations

ValidatorFactory (javax.validation.ValidatorFactory)48 Validator (javax.validation.Validator)13 Test (org.junit.Test)12 ConstraintViolation (javax.validation.ConstraintViolation)9 Before (org.junit.Before)8 HashMap (java.util.HashMap)5 InitialContext (javax.naming.InitialContext)5 BeanManager (javax.enterprise.inject.spi.BeanManager)4 ConstraintValidatorFactory (javax.validation.ConstraintValidatorFactory)4 ValidationException (javax.validation.ValidationException)4 HibernateValidator (org.hibernate.validator.HibernateValidator)4 HibernateValidatorConfiguration (org.hibernate.validator.HibernateValidatorConfiguration)4 ArrayList (java.util.ArrayList)3 TransactionManager (javax.transaction.TransactionManager)3 TransactionSynchronizationRegistry (javax.transaction.TransactionSynchronizationRegistry)3 CapabilityServiceSupport (org.jboss.as.controller.capability.CapabilityServiceSupport)3 ProxyBeanManager (org.jboss.as.jpa.beanmanager.ProxyBeanManager)3 PhaseOnePersistenceUnitServiceImpl (org.jboss.as.jpa.service.PhaseOnePersistenceUnitServiceImpl)3 ServiceName (org.jboss.msc.service.ServiceName)3 ServiceRegistryException (org.jboss.msc.service.ServiceRegistryException)3