Search in sources :

Example 1 with ConstraintDescriptor

use of jakarta.validation.metadata.ConstraintDescriptor in project wicket by apache.

the class PropertyValidator method onComponentTag.

@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
public void onComponentTag(Component component, ComponentTag tag) {
    super.onComponentTag(component, tag);
    BeanValidationContext config = BeanValidationConfiguration.get();
    Validator validator = config.getValidator();
    Property property = getProperty();
    // find any tag modifiers that apply to the constraints of the property
    // being validated
    // and allow them to modify the component tag
    Iterator<ConstraintDescriptor<?>> it = new ConstraintIterator(validator, property, getGroups());
    while (it.hasNext()) {
        ConstraintDescriptor<?> desc = it.next();
        ITagModifier modifier = config.getTagModifier(desc.getAnnotation().annotationType());
        if (modifier != null) {
            modifier.modify((FormComponent<?>) component, tag, desc.getAnnotation());
        }
    }
}
Also used : ConstraintDescriptor(jakarta.validation.metadata.ConstraintDescriptor) Validator(jakarta.validation.Validator) INullAcceptingValidator(org.apache.wicket.validation.INullAcceptingValidator)

Example 2 with ConstraintDescriptor

use of jakarta.validation.metadata.ConstraintDescriptor in project hibernate-orm by hibernate.

the class TypeSafeActivator method applySize.

private static void applySize(Property property, ConstraintDescriptor<?> descriptor, PropertyDescriptor propertyDescriptor) {
    if (Size.class.equals(descriptor.getAnnotation().annotationType()) && String.class.equals(propertyDescriptor.getElementClass())) {
        @SuppressWarnings("unchecked") ConstraintDescriptor<Size> sizeConstraint = (ConstraintDescriptor<Size>) descriptor;
        int max = sizeConstraint.getAnnotation().max();
        for (Column col : property.getColumns()) {
            if (max < Integer.MAX_VALUE) {
                col.setLength(max);
            }
        }
    }
}
Also used : Column(org.hibernate.mapping.Column) Size(jakarta.validation.constraints.Size) ConstraintDescriptor(jakarta.validation.metadata.ConstraintDescriptor)

Example 3 with ConstraintDescriptor

use of jakarta.validation.metadata.ConstraintDescriptor in project hibernate-orm by hibernate.

the class TypeSafeActivator method applyDigits.

private static void applyDigits(Property property, ConstraintDescriptor<?> descriptor) {
    if (Digits.class.equals(descriptor.getAnnotation().annotationType())) {
        @SuppressWarnings("unchecked") ConstraintDescriptor<Digits> digitsConstraint = (ConstraintDescriptor<Digits>) descriptor;
        int integerDigits = digitsConstraint.getAnnotation().integer();
        int fractionalDigits = digitsConstraint.getAnnotation().fraction();
        for (Selectable selectable : property.getSelectables()) {
            if (selectable instanceof Column) {
                Column col = (Column) selectable;
                col.setPrecision(integerDigits + fractionalDigits);
                col.setScale(fractionalDigits);
            }
        }
    }
}
Also used : Selectable(org.hibernate.mapping.Selectable) Column(org.hibernate.mapping.Column) Digits(jakarta.validation.constraints.Digits) ConstraintDescriptor(jakarta.validation.metadata.ConstraintDescriptor)

Example 4 with ConstraintDescriptor

use of jakarta.validation.metadata.ConstraintDescriptor in project hibernate-validator by hibernate.

the class MessageInterpolatorContextTest method testContextWithRightDescriptorAndValueAndRootBeanClassIsPassedToMessageInterpolator.

@Test
@TestForIssue(jiraKey = "HV-333")
public void testContextWithRightDescriptorAndValueAndRootBeanClassIsPassedToMessageInterpolator() {
    // use a easy mock message interpolator for verifying that the right MessageInterpolatorContext
    // will be passed
    MessageInterpolator mock = createMock(MessageInterpolator.class);
    Configuration<?> config = ValidatorUtil.getConfiguration().messageInterpolator(mock);
    Validator validator = config.buildValidatorFactory().getValidator();
    BeanDescriptor beanDescriptor = validator.getConstraintsForClass(TestBean.class);
    PropertyDescriptor propertyDescriptor = beanDescriptor.getConstraintsForProperty("test");
    Set<ConstraintDescriptor<?>> constraintDescriptors = propertyDescriptor.getConstraintDescriptors();
    assertTrue(constraintDescriptors.size() == 1);
    // prepare the mock interpolator to expect the right interpolate call
    String validatedValue = "value";
    expect(mock.interpolate(MESSAGE, new MessageInterpolatorContext(constraintDescriptors.iterator().next(), validatedValue, TestBean.class, null, Collections.<String, Object>emptyMap(), Collections.<String, Object>emptyMap(), ExpressionLanguageFeatureLevel.BEAN_METHODS, false))).andReturn("invalid");
    replay(mock);
    Set<ConstraintViolation<TestBean>> violations = validator.validate(new TestBean(validatedValue));
    assertThat(violations).containsOnlyViolations(violationOf(Size.class).withMessage("invalid"));
    // verify that the right context was passed
    verify(mock);
}
Also used : PropertyDescriptor(jakarta.validation.metadata.PropertyDescriptor) MessageInterpolatorContext(org.hibernate.validator.internal.engine.MessageInterpolatorContext) HibernateMessageInterpolatorContext(org.hibernate.validator.messageinterpolation.HibernateMessageInterpolatorContext) BeanDescriptor(jakarta.validation.metadata.BeanDescriptor) ConstraintDescriptor(jakarta.validation.metadata.ConstraintDescriptor) ConstraintViolation(jakarta.validation.ConstraintViolation) Validator(jakarta.validation.Validator) 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 5 with ConstraintDescriptor

use of jakarta.validation.metadata.ConstraintDescriptor in project hibernate-validator by hibernate.

the class ParameterTermResolverTest method createNotHibernateContextWithConstraintDescriptorAttr.

@SuppressWarnings({ "rawtypes", "unchecked" })
private static MessageInterpolator.Context createNotHibernateContextWithConstraintDescriptorAttr(String attrName, Object attrValue) {
    final Map<String, Object> attrs = createConstraintDescriptorAttr(attrName, attrValue);
    final MessageInterpolator.Context context = EasyMock.mock(MessageInterpolator.Context.class);
    final ConstraintDescriptor constraintDescriptor = EasyMock.mock(ConstraintDescriptor.class);
    EasyMock.expect(context.getConstraintDescriptor()).andStubReturn(constraintDescriptor);
    EasyMock.expect(constraintDescriptor.getAttributes()).andStubReturn(attrs);
    EasyMock.replay(context);
    EasyMock.replay(constraintDescriptor);
    return context;
}
Also used : ConstraintDescriptor(jakarta.validation.metadata.ConstraintDescriptor) MessageInterpolator(jakarta.validation.MessageInterpolator)

Aggregations

ConstraintDescriptor (jakarta.validation.metadata.ConstraintDescriptor)25 Test (org.testng.annotations.Test)11 BeanDescriptor (jakarta.validation.metadata.BeanDescriptor)10 CrossParameterDescriptor (jakarta.validation.metadata.CrossParameterDescriptor)4 PropertyDescriptor (jakarta.validation.metadata.PropertyDescriptor)4 Column (org.hibernate.mapping.Column)4 TestForIssue (org.hibernate.validator.testutil.TestForIssue)4 MethodDescriptor (jakarta.validation.metadata.MethodDescriptor)3 ParameterDescriptor (jakarta.validation.metadata.ParameterDescriptor)3 ReturnValueDescriptor (jakarta.validation.metadata.ReturnValueDescriptor)3 Selectable (org.hibernate.mapping.Selectable)3 ValidatorUtil.getBeanDescriptor (org.hibernate.validator.testutils.ValidatorUtil.getBeanDescriptor)3 Test (org.junit.Test)3 MessageInterpolator (jakarta.validation.MessageInterpolator)2 Validator (jakarta.validation.Validator)2 ConstructorDescriptor (jakarta.validation.metadata.ConstructorDescriptor)2 ContainerElementTypeDescriptor (jakarta.validation.metadata.ContainerElementTypeDescriptor)2 ElementDescriptor (jakarta.validation.metadata.ElementDescriptor)2 ConstraintDescriptorImpl (org.hibernate.validator.internal.metadata.descriptor.ConstraintDescriptorImpl)2 HibernateMessageInterpolatorContext (org.hibernate.validator.messageinterpolation.HibernateMessageInterpolatorContext)2