Search in sources :

Example 1 with PropertyDescriptor

use of jakarta.validation.metadata.PropertyDescriptor in project minijax by minijax.

the class MinijaxValidator method validate.

@Override
public <T> Set<ConstraintViolation<T>> validate(final T object, final Class<?>... groups) {
    final MinijaxConstraintValidatorContext<T> context = new MinijaxConstraintValidatorContext<>(object);
    final BeanDescriptor descriptor = getConstraintsForClass(object.getClass());
    for (final PropertyDescriptor propertyDescriptor : descriptor.getConstrainedProperties()) {
        final Object value = ((MinijaxPropertyDescriptor) propertyDescriptor).getValue(object);
        validateProperty(context, propertyDescriptor, value);
    }
    return context.getResult();
}
Also used : MinijaxPropertyDescriptor(org.minijax.validation.metadata.MinijaxPropertyDescriptor) PropertyDescriptor(jakarta.validation.metadata.PropertyDescriptor) MinijaxPropertyDescriptor(org.minijax.validation.metadata.MinijaxPropertyDescriptor) BeanDescriptor(jakarta.validation.metadata.BeanDescriptor) MinijaxBeanDescriptor(org.minijax.validation.metadata.MinijaxBeanDescriptor)

Example 2 with PropertyDescriptor

use of jakarta.validation.metadata.PropertyDescriptor 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 3 with PropertyDescriptor

use of jakarta.validation.metadata.PropertyDescriptor 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 PropertyDescriptor

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

the class PropertyDescriptorTest method testGetGroupConversionsDeclaredOnGetter.

@Test
public void testGetGroupConversionsDeclaredOnGetter() {
    PropertyDescriptor propertyDescriptor = getPropertyDescriptor(Customer.class, "lastOrder");
    Set<GroupConversionDescriptor> groupConversions = propertyDescriptor.getGroupConversions();
    assertThat(groupConversions).hasSize(2);
    assertThat(groupConversions).containsConversion(CustomerGetterBasic.class, OrderBasic.class);
    assertThat(groupConversions).containsConversion(CustomerGetterComplex.class, OrderComplex.class);
}
Also used : ValidatorUtil.getPropertyDescriptor(org.hibernate.validator.testutils.ValidatorUtil.getPropertyDescriptor) PropertyDescriptor(jakarta.validation.metadata.PropertyDescriptor) GroupConversionDescriptor(jakarta.validation.metadata.GroupConversionDescriptor) Test(org.testng.annotations.Test)

Example 5 with PropertyDescriptor

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

the class ElementDescriptorTest method testAtValidNotDefinedForPropertyDescriptor.

@Test
public void testAtValidNotDefinedForPropertyDescriptor() {
    PropertyDescriptor propertyDescriptor = ValidatorUtil.getPropertyDescriptor(ChildWithoutAtValid2.class, "order");
    assertFalse(propertyDescriptor.isCascaded(), "@Valid given neither locally nor in hierarchy should be reflected by PropertyDescriptor.");
}
Also used : PropertyDescriptor(jakarta.validation.metadata.PropertyDescriptor) Test(org.testng.annotations.Test)

Aggregations

PropertyDescriptor (jakarta.validation.metadata.PropertyDescriptor)20 BeanDescriptor (jakarta.validation.metadata.BeanDescriptor)13 Test (org.testng.annotations.Test)9 Test (org.junit.Test)5 ConstraintDescriptor (jakarta.validation.metadata.ConstraintDescriptor)4 GroupConversionDescriptor (jakarta.validation.metadata.GroupConversionDescriptor)3 MinijaxPropertyDescriptor (org.minijax.validation.metadata.MinijaxPropertyDescriptor)3 TestForIssue (org.hibernate.validator.testutil.TestForIssue)2 ValidatorUtil.getBeanDescriptor (org.hibernate.validator.testutils.ValidatorUtil.getBeanDescriptor)2 ValidatorUtil.getPropertyDescriptor (org.hibernate.validator.testutils.ValidatorUtil.getPropertyDescriptor)2 MinijaxBeanDescriptor (org.minijax.validation.metadata.MinijaxBeanDescriptor)2 ConstraintViolation (jakarta.validation.ConstraintViolation)1 MessageInterpolator (jakarta.validation.MessageInterpolator)1 Validator (jakarta.validation.Validator)1 Default (jakarta.validation.groups.Default)1 ContainerElementTypeDescriptor (jakarta.validation.metadata.ContainerElementTypeDescriptor)1 CrossParameterDescriptor (jakarta.validation.metadata.CrossParameterDescriptor)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Component (org.hibernate.mapping.Component)1