Search in sources :

Example 16 with ConstraintDescriptor

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

the class TypeSafeActivator method applyMin.

private static void applyMin(Property property, ConstraintDescriptor<?> descriptor, Dialect dialect) {
    if (Min.class.equals(descriptor.getAnnotation().annotationType())) {
        @SuppressWarnings("unchecked") ConstraintDescriptor<Min> minConstraint = (ConstraintDescriptor<Min>) descriptor;
        long min = minConstraint.getAnnotation().value();
        for (Selectable selectable : property.getSelectables()) {
            if (selectable instanceof Column) {
                Column col = (Column) selectable;
                String checkConstraint = col.getQuotedName(dialect) + ">=" + min;
                applySQLCheck(col, checkConstraint);
            }
        }
    }
}
Also used : Min(jakarta.validation.constraints.Min) Selectable(org.hibernate.mapping.Selectable) Column(org.hibernate.mapping.Column) ConstraintDescriptor(jakarta.validation.metadata.ConstraintDescriptor)

Example 17 with ConstraintDescriptor

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

the class TypeSafeActivator method applyMax.

private static void applyMax(Property property, ConstraintDescriptor<?> descriptor, Dialect dialect) {
    if (Max.class.equals(descriptor.getAnnotation().annotationType())) {
        @SuppressWarnings("unchecked") ConstraintDescriptor<Max> maxConstraint = (ConstraintDescriptor<Max>) descriptor;
        long max = maxConstraint.getAnnotation().value();
        for (Selectable selectable : property.getSelectables()) {
            if (selectable instanceof Column) {
                Column col = (Column) selectable;
                String checkConstraint = col.getQuotedName(dialect) + "<=" + max;
                applySQLCheck(col, checkConstraint);
            }
        }
    }
}
Also used : Max(jakarta.validation.constraints.Max) Selectable(org.hibernate.mapping.Selectable) Column(org.hibernate.mapping.Column) ConstraintDescriptor(jakarta.validation.metadata.ConstraintDescriptor)

Example 18 with ConstraintDescriptor

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

the class ParameterTermResolverTest method createHibernateContextWithConstraintDescriptorAttr.

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

Example 19 with ConstraintDescriptor

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

the class XMLConfigurationTest method testXMLCrossParameterConstraints.

@Test
public void testXMLCrossParameterConstraints() {
    BeanDescriptor beanDescriptor = validator.getConstraintsForClass(Garage.class);
    MethodDescriptor methodDescriptor = beanDescriptor.getConstraintsForMethod("buildCar", java.util.List.class);
    CrossParameterDescriptor crossParameterDescriptor = methodDescriptor.getCrossParameterDescriptor();
    Set<ConstraintDescriptor<?>> constraintDescriptors = crossParameterDescriptor.getConstraintDescriptors();
    assertCorrectConstraintTypes(constraintDescriptors, ELAssert.class);
    methodDescriptor = beanDescriptor.getConstraintsForMethod("paintCar", int.class);
    ReturnValueDescriptor returnValueDescriptor = methodDescriptor.getReturnValueDescriptor();
    constraintDescriptors = returnValueDescriptor.getConstraintDescriptors();
    assertCorrectConstraintTypes(constraintDescriptors, ELAssert.class);
}
Also used : ReturnValueDescriptor(jakarta.validation.metadata.ReturnValueDescriptor) BeanDescriptor(jakarta.validation.metadata.BeanDescriptor) ConstraintDescriptor(jakarta.validation.metadata.ConstraintDescriptor) CrossParameterDescriptor(jakarta.validation.metadata.CrossParameterDescriptor) MethodDescriptor(jakarta.validation.metadata.MethodDescriptor) Test(org.junit.Test)

Example 20 with ConstraintDescriptor

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

the class XMLConfigurationTest method testXMLConstraintsApplied.

@Test
public void testXMLConstraintsApplied() {
    BeanDescriptor beanDescriptor = validator.getConstraintsForClass(RentalStation.class);
    assertTrue(beanDescriptor.isBeanConstrained());
    ConstructorDescriptor constructorDescriptor = beanDescriptor.getConstraintsForConstructor();
    ReturnValueDescriptor returnValueDescriptor = constructorDescriptor.getReturnValueDescriptor();
    Set<ConstraintDescriptor<?>> constraintDescriptors = returnValueDescriptor.getConstraintDescriptors();
    assertCorrectConstraintTypes(constraintDescriptors, ValidRentalStation.class);
    constructorDescriptor = beanDescriptor.getConstraintsForConstructor(String.class);
    List<ParameterDescriptor> parameterDescriptors = constructorDescriptor.getParameterDescriptors();
    constraintDescriptors = parameterDescriptors.get(0).getConstraintDescriptors();
    assertCorrectConstraintTypes(constraintDescriptors, NotNull.class);
    MethodDescriptor methodDescriptor = beanDescriptor.getConstraintsForMethod("getCustomers");
    returnValueDescriptor = methodDescriptor.getReturnValueDescriptor();
    constraintDescriptors = returnValueDescriptor.getConstraintDescriptors();
    assertCorrectConstraintTypes(constraintDescriptors, NotNull.class, Size.class);
    methodDescriptor = beanDescriptor.getConstraintsForMethod("rentCar", Customer.class, Date.class, int.class);
    parameterDescriptors = methodDescriptor.getParameterDescriptors();
    constraintDescriptors = parameterDescriptors.get(0).getConstraintDescriptors();
    assertCorrectConstraintTypes(constraintDescriptors, NotNull.class);
    constraintDescriptors = parameterDescriptors.get(1).getConstraintDescriptors();
    assertCorrectConstraintTypes(constraintDescriptors, NotNull.class, Future.class);
    constraintDescriptors = parameterDescriptors.get(2).getConstraintDescriptors();
    assertCorrectConstraintTypes(constraintDescriptors, Min.class);
}
Also used : ReturnValueDescriptor(jakarta.validation.metadata.ReturnValueDescriptor) BeanDescriptor(jakarta.validation.metadata.BeanDescriptor) ConstraintDescriptor(jakarta.validation.metadata.ConstraintDescriptor) ParameterDescriptor(jakarta.validation.metadata.ParameterDescriptor) CrossParameterDescriptor(jakarta.validation.metadata.CrossParameterDescriptor) ConstructorDescriptor(jakarta.validation.metadata.ConstructorDescriptor) MethodDescriptor(jakarta.validation.metadata.MethodDescriptor) Date(java.util.Date) Test(org.junit.Test)

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