Search in sources :

Example 21 with MessageInterpolator

use of jakarta.validation.MessageInterpolator in project graphql-java-extended-validation by graphql-java.

the class ELDiscover method main.

public static void main(String[] args) throws Exception {
    elDirect();
    Map<String, Object> attributes = new HashMap<>();
    ConstraintAnnotationDescriptor.Builder<NotNull> constraintBuilder = new ConstraintAnnotationDescriptor.Builder<>(NotNull.class, attributes);
    ConstraintDescriptorImpl<NotNull> constraintDescriptor = new ConstraintDescriptorImpl<>(ConstraintHelper.forAllBuiltinConstraints(), null, constraintBuilder.build(), ConstraintLocationKind.FIELD, ConstraintType.GENERIC);
    User user = new User();
    user.setAge(18);
    ResourceBundleMessageInterpolator messageInterpolator = new ResourceBundleMessageInterpolator();
    PathImpl rootPath = PathImpl.createRootPath();
    ExpressionLanguageFeatureLevel level = ExpressionLanguageFeatureLevel.BEAN_METHODS;
    MessageInterpolator.Context context = new MessageInterpolatorContext(constraintDescriptor, user, null, rootPath, Collections.emptyMap(), Collections.emptyMap(), level, true);
    print("${validatedValue.age}", messageInterpolator, context);
    print("${validatedValue}", messageInterpolator, context);
    print("[1,2,3,4].stream().sum()", messageInterpolator, context);
    print("${4.0>= 3}", messageInterpolator, context);
    print("${! empty validatedValue}", messageInterpolator, context);
    print("#{cube=(x->x*x*x);cube(2)}", messageInterpolator, context);
}
Also used : ConstraintAnnotationDescriptor(org.hibernate.validator.internal.util.annotation.ConstraintAnnotationDescriptor) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ConstraintDescriptorImpl(org.hibernate.validator.internal.metadata.descriptor.ConstraintDescriptorImpl) PathImpl(org.hibernate.validator.internal.engine.path.PathImpl) NotNull(jakarta.validation.constraints.NotNull) MessageInterpolatorContext(org.hibernate.validator.internal.engine.MessageInterpolatorContext) ResourceBundleMessageInterpolator(org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator) ExpressionLanguageFeatureLevel(org.hibernate.validator.messageinterpolation.ExpressionLanguageFeatureLevel) ResourceBundleMessageInterpolator(org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator) MessageInterpolator(jakarta.validation.MessageInterpolator)

Example 22 with MessageInterpolator

use of jakarta.validation.MessageInterpolator in project mojarra by eclipse-ee4j.

the class BeanValidator method getBeanValidator.

// ----------------------------------------------------- Private helper methods for bean validation
// MOJARRA IMPLEMENTATION NOTE: identical code exists in Mojarra's com.sun.faces.util.BeanValidation
private static jakarta.validation.Validator getBeanValidator(FacesContext context) {
    ValidatorFactory validatorFactory = getValidatorFactory(context);
    ValidatorContext validatorContext = validatorFactory.usingContext();
    MessageInterpolator facesMessageInterpolator = new FacesAwareMessageInterpolator(context, validatorFactory.getMessageInterpolator());
    validatorContext.messageInterpolator(facesMessageInterpolator);
    return validatorContext.getValidator();
}
Also used : ValidatorFactory(jakarta.validation.ValidatorFactory) ValidatorContext(jakarta.validation.ValidatorContext) MessageInterpolator(jakarta.validation.MessageInterpolator)

Example 23 with MessageInterpolator

use of jakarta.validation.MessageInterpolator in project today-framework by TAKETODAY.

the class CustomValidatorBean method afterPropertiesSet.

@Override
public void afterPropertiesSet() {
    if (this.validatorFactory == null) {
        this.validatorFactory = Validation.buildDefaultValidatorFactory();
    }
    ValidatorContext validatorContext = this.validatorFactory.usingContext();
    MessageInterpolator targetInterpolator = this.messageInterpolator;
    if (targetInterpolator == null) {
        targetInterpolator = this.validatorFactory.getMessageInterpolator();
    }
    validatorContext.messageInterpolator(new LocaleContextMessageInterpolator(targetInterpolator));
    if (this.traversableResolver != null) {
        validatorContext.traversableResolver(this.traversableResolver);
    }
    setTargetValidator(validatorContext.getValidator());
}
Also used : ValidatorContext(jakarta.validation.ValidatorContext) MessageInterpolator(jakarta.validation.MessageInterpolator)

Example 24 with MessageInterpolator

use of jakarta.validation.MessageInterpolator in project today-framework by TAKETODAY.

the class MessageInterpolatorFactoryTests method getObjectShouldReturnMessageSourceMessageInterpolatorDelegateWithResourceBundleMessageInterpolator.

@Test
void getObjectShouldReturnMessageSourceMessageInterpolatorDelegateWithResourceBundleMessageInterpolator() {
    MessageSource messageSource = mock(MessageSource.class);
    MessageInterpolatorFactory interpolatorFactory = new MessageInterpolatorFactory(messageSource);
    MessageInterpolator interpolator = interpolatorFactory.get();
    assertThat(interpolator).isInstanceOf(MessageSourceMessageInterpolator.class);
    assertThat(interpolator).hasFieldOrPropertyWithValue("messageSource", messageSource);
    assertThat(ReflectionTestUtils.getField(interpolator, "messageInterpolator")).isInstanceOf(ResourceBundleMessageInterpolator.class);
}
Also used : MessageSource(cn.taketoday.context.MessageSource) MessageInterpolator(jakarta.validation.MessageInterpolator) ResourceBundleMessageInterpolator(org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator) Test(org.junit.jupiter.api.Test)

Example 25 with MessageInterpolator

use of jakarta.validation.MessageInterpolator in project hibernate-validator by hibernate.

the class EscapedInterpolationVariableTest method setUp.

@BeforeTest
public void setUp() {
    MessageInterpolator interpolator = new ResourceBundleMessageInterpolator(new ResourceBundleLocator() {

        @Override
        public ResourceBundle getResourceBundle(Locale locale) {
            return new ResourceBundle() {

                @Override
                protected Object handleGetObject(String key) {
                    if ("key-1".equals(key)) {
                        return "\\{escapedParameterKey\\}";
                    } else if ("key-2".equals(key)) {
                        // since {} are unbalanced the original key (key-2) should be returned from the interpolation
                        return "{escapedParameterKey\\}";
                    } else if ("key-3".equals(key)) {
                        // since {} are unbalanced the original key (key-3) should be returned from the interpolation
                        return "\\{escapedParameterKey}";
                    } else if ("key-4".equals(key)) {
                        return "foo";
                    } else {
                        fail("Unexpected key: " + key);
                    }
                    return null;
                }

                @Override
                public Enumeration<String> getKeys() {
                    throw new UnsupportedOperationException();
                }
            };
        }
    }, false);
    HibernateValidatorConfiguration config = ValidatorUtil.getConfiguration();
    ValidatorFactory factory = config.messageInterpolator(interpolator).buildValidatorFactory();
    validator = factory.getValidator();
}
Also used : Locale(java.util.Locale) ResourceBundleMessageInterpolator(org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator) Enumeration(java.util.Enumeration) ValidatorFactory(jakarta.validation.ValidatorFactory) ResourceBundleLocator(org.hibernate.validator.spi.resourceloading.ResourceBundleLocator) ResourceBundle(java.util.ResourceBundle) HibernateValidatorConfiguration(org.hibernate.validator.HibernateValidatorConfiguration) ResourceBundleMessageInterpolator(org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator) MessageInterpolator(jakarta.validation.MessageInterpolator) BeforeTest(org.testng.annotations.BeforeTest)

Aggregations

MessageInterpolator (jakarta.validation.MessageInterpolator)33 ValidatorFactory (jakarta.validation.ValidatorFactory)14 ResourceBundleMessageInterpolator (org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator)13 Test (org.testng.annotations.Test)11 Test (org.junit.jupiter.api.Test)8 Locale (java.util.Locale)7 Validator (jakarta.validation.Validator)5 ValidatorContext (jakarta.validation.ValidatorContext)5 ConstraintViolation (jakarta.validation.ConstraintViolation)4 ConstraintValidatorFactory (jakarta.validation.ConstraintValidatorFactory)3 GenericBootstrap (jakarta.validation.bootstrap.GenericBootstrap)3 ProviderSpecificBootstrap (jakarta.validation.bootstrap.ProviderSpecificBootstrap)3 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 Method (java.lang.reflect.Method)3 MessageSource (cn.taketoday.context.MessageSource)2 Resource (cn.taketoday.core.io.Resource)2 HibernateValidator (org.hibernate.validator.HibernateValidator)2 HibernateValidatorConfiguration (org.hibernate.validator.HibernateValidatorConfiguration)2 PredefinedScopeHibernateValidator (org.hibernate.validator.PredefinedScopeHibernateValidator)2