Search in sources :

Example 1 with ConstraintDescriptorImpl

use of org.hibernate.validator.internal.metadata.descriptor.ConstraintDescriptorImpl in project graphql-java-extended-validation by graphql-java.

the class ResourceBundleMessageInterpolator method buildHibernateContext.

@SuppressWarnings("ConstantConditions")
private MessageInterpolatorContext buildHibernateContext(Map<String, Object> messageParams, ValidationEnvironment validationEnvironment) {
    Object validatedValue = validationEnvironment.getValidatedValue();
    ConstraintAnnotationDescriptor<BridgeAnnotation> annotationDescriptor = new ConstraintAnnotationDescriptor.Builder<>(BridgeAnnotation.class).build();
    ConstraintDescriptorImpl<BridgeAnnotation> constraintDescriptor = new ConstraintDescriptorImpl<>(ConstraintHelper.forAllBuiltinConstraints(), null, annotationDescriptor, ConstraintLocationKind.FIELD, ConstraintType.GENERIC);
    Map<String, Object> expressionVariables = StandardELVariables.standardELVars(validationEnvironment);
    Class<?> rootBeanType = null;
    Path propertyPath = null;
    return new MessageInterpolatorContext(constraintDescriptor, validatedValue, rootBeanType, propertyPath, messageParams, expressionVariables, ExpressionLanguageFeatureLevel.BEAN_PROPERTIES, true);
}
Also used : Path(jakarta.validation.Path) ResultPath(graphql.execution.ResultPath) MessageInterpolatorContext(org.hibernate.validator.internal.engine.MessageInterpolatorContext) ConstraintAnnotationDescriptor(org.hibernate.validator.internal.util.annotation.ConstraintAnnotationDescriptor) ConstraintDescriptorImpl(org.hibernate.validator.internal.metadata.descriptor.ConstraintDescriptorImpl)

Example 2 with ConstraintDescriptorImpl

use of org.hibernate.validator.internal.metadata.descriptor.ConstraintDescriptorImpl in project hibernate-validator by hibernate.

the class ResourceBundleMessageInterpolatorTest method testCorrectMessageInterpolationIfParameterCannotBeReplaced.

@Test
@TestForIssue(jiraKey = "HV-182")
public void testCorrectMessageInterpolationIfParameterCannotBeReplaced() {
    ConstraintAnnotationDescriptor.Builder<Max> descriptorBuilder = new ConstraintAnnotationDescriptor.Builder<>(Max.class);
    String message = "Message should stay unchanged since {fubar} is not replaceable";
    descriptorBuilder.setMessage(message);
    descriptorBuilder.setAttribute("value", 10L);
    ConstraintAnnotationDescriptor<Max> maxDescriptor = descriptorBuilder.build();
    ConstraintDescriptorImpl<Max> constraintDescriptor = new ConstraintDescriptorImpl<Max>(ConstraintHelper.forAllBuiltinConstraints(), null, maxDescriptor, ConstraintLocationKind.FIELD);
    interpolator = new ResourceBundleMessageInterpolator(new TestResourceBundleLocator());
    MessageInterpolator.Context messageInterpolatorContext = createMessageInterpolatorContext(constraintDescriptor);
    String actual = interpolator.interpolate(maxDescriptor.getMessage(), messageInterpolatorContext);
    assertEquals(actual, message, "The message should not have changed.");
}
Also used : ResourceBundleMessageInterpolator(org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator) ConstraintAnnotationDescriptor(org.hibernate.validator.internal.util.annotation.ConstraintAnnotationDescriptor) Max(jakarta.validation.constraints.Max) ConstraintDescriptorImpl(org.hibernate.validator.internal.metadata.descriptor.ConstraintDescriptorImpl) ResourceBundleMessageInterpolator(org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator) MessageInterpolator(jakarta.validation.MessageInterpolator) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest) TestForIssue(org.hibernate.validator.testutil.TestForIssue)

Example 3 with ConstraintDescriptorImpl

use of org.hibernate.validator.internal.metadata.descriptor.ConstraintDescriptorImpl in project hibernate-validator by hibernate.

the class ConstraintValidatorManagerTest method getSingleConstraintDescriptorForProperty.

private ConstraintDescriptorImpl<?> getSingleConstraintDescriptorForProperty(Validator validator, Class<?> clazz, String propertyName) {
    BeanDescriptor beanDescriptor = validator.getConstraintsForClass(clazz);
    PropertyDescriptor propertyDescriptor = beanDescriptor.getConstraintsForProperty(propertyName);
    Set<ConstraintDescriptor<?>> constraintDescriptorSet = propertyDescriptor.getConstraintDescriptors();
    assertEquals(constraintDescriptorSet.size(), 1, "There should be only one constraint descriptor");
    return (ConstraintDescriptorImpl<?>) constraintDescriptorSet.iterator().next();
}
Also used : PropertyDescriptor(jakarta.validation.metadata.PropertyDescriptor) BeanDescriptor(jakarta.validation.metadata.BeanDescriptor) ConstraintDescriptor(jakarta.validation.metadata.ConstraintDescriptor) ConstraintDescriptorImpl(org.hibernate.validator.internal.metadata.descriptor.ConstraintDescriptorImpl)

Example 4 with ConstraintDescriptorImpl

use of org.hibernate.validator.internal.metadata.descriptor.ConstraintDescriptorImpl in project hibernate-validator by hibernate.

the class AnnotationMetaDataProvider method findTypeUseConstraints.

/**
 * Finds type use annotation constraints defined on the type argument.
 */
private Set<MetaConstraint<?>> findTypeUseConstraints(Constrainable constrainable, AnnotatedType typeArgument, TypeVariable<?> typeVariable, TypeArgumentLocation location, Type type) {
    List<ConstraintDescriptorImpl<?>> constraintDescriptors = findConstraints(constrainable, typeArgument.getAnnotations(), ConstraintLocationKind.TYPE_USE);
    if (constraintDescriptors.isEmpty()) {
        return Collections.emptySet();
    }
    Set<MetaConstraint<?>> constraints = newHashSet(constraintDescriptors.size());
    ConstraintLocation constraintLocation = ConstraintLocation.forTypeArgument(location.toConstraintLocation(), typeVariable, type);
    for (ConstraintDescriptorImpl<?> constraintDescriptor : constraintDescriptors) {
        constraints.add(MetaConstraints.create(constraintCreationContext.getTypeResolutionHelper(), constraintCreationContext.getValueExtractorManager(), constraintCreationContext.getConstraintValidatorManager(), constraintDescriptor, constraintLocation));
    }
    return constraints;
}
Also used : ConstraintLocation(org.hibernate.validator.internal.metadata.location.ConstraintLocation) MetaConstraint(org.hibernate.validator.internal.metadata.core.MetaConstraint) ConstraintDescriptorImpl(org.hibernate.validator.internal.metadata.descriptor.ConstraintDescriptorImpl)

Example 5 with ConstraintDescriptorImpl

use of org.hibernate.validator.internal.metadata.descriptor.ConstraintDescriptorImpl 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)

Aggregations

ConstraintDescriptorImpl (org.hibernate.validator.internal.metadata.descriptor.ConstraintDescriptorImpl)13 ConstraintLocation (org.hibernate.validator.internal.metadata.location.ConstraintLocation)5 ConstraintAnnotationDescriptor (org.hibernate.validator.internal.util.annotation.ConstraintAnnotationDescriptor)4 MessageInterpolator (jakarta.validation.MessageInterpolator)3 MetaConstraint (org.hibernate.validator.internal.metadata.core.MetaConstraint)3 ResourceBundleMessageInterpolator (org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator)3 TestForIssue (org.hibernate.validator.testutil.TestForIssue)3 Test (org.testng.annotations.Test)3 Max (jakarta.validation.constraints.Max)2 NotNull (jakarta.validation.constraints.NotNull)2 BeanDescriptor (jakarta.validation.metadata.BeanDescriptor)2 ConstraintDescriptor (jakarta.validation.metadata.ConstraintDescriptor)2 ArrayList (java.util.ArrayList)2 MessageInterpolatorContext (org.hibernate.validator.internal.engine.MessageInterpolatorContext)2 ResolvedType (com.fasterxml.classmate.ResolvedType)1 ResultPath (graphql.execution.ResultPath)1 FunctionalException (io.fluxcapacitor.javaclient.common.exception.FunctionalException)1 Path (jakarta.validation.Path)1 PropertyDescriptor (jakarta.validation.metadata.PropertyDescriptor)1 ValidateUnwrappedValue (jakarta.validation.metadata.ValidateUnwrappedValue)1