Search in sources :

Example 26 with Size

use of jakarta.validation.constraints.Size in project hibernate-validator by hibernate.

the class ContainerElementTypeConstraintsForReturnValueXmlMappingTest method canDeclareContainerElementTypeConstraintsForListContainingArrayTypeReturnValueWithXmlMapping.

// HV-1428 Container element support is disabled for arrays
@Test(expectedExceptions = ValidationException.class, expectedExceptionsMessageRegExp = "HV000226:.*")
@TestForIssue(jiraKey = "HV-1291")
public void canDeclareContainerElementTypeConstraintsForListContainingArrayTypeReturnValueWithXmlMapping() {
    Validator validator = getValidator("returnvalue-canDeclareContainerElementTypeConstraintsForListContainingArrayType-mapping.xml");
    IFishTank fishTank = ValidatorUtil.getValidatingProxy(new FishTank(), validator);
    try {
        fishTank.test6();
        fail("Expected exception wasn't raised");
    } catch (ConstraintViolationException e) {
        assertThat(e.getConstraintViolations()).containsOnlyViolations(violationOf(Size.class).withMessage("size must be between 0 and 5"));
    }
}
Also used : Size(jakarta.validation.constraints.Size) ConstraintViolationException(jakarta.validation.ConstraintViolationException) Validator(jakarta.validation.Validator) Test(org.testng.annotations.Test) TestForIssue(org.hibernate.validator.testutil.TestForIssue)

Example 27 with Size

use of jakarta.validation.constraints.Size in project hibernate-validator by hibernate.

the class ContainerElementTypeConstraintsForReturnValueXmlMappingTest method canDeclareContainerElementTypeConstraintsForArrayTypeReturnValueWithXmlMapping.

// HV-1428 Container element support is disabled for arrays
@Test(expectedExceptions = ValidationException.class, expectedExceptionsMessageRegExp = "HV000226:.*")
@TestForIssue(jiraKey = "HV-1291")
public void canDeclareContainerElementTypeConstraintsForArrayTypeReturnValueWithXmlMapping() {
    Validator validator = getValidator("returnvalue-canDeclareContainerElementTypeConstraintsForArrayType-mapping.xml");
    IFishTank fishTank = ValidatorUtil.getValidatingProxy(new FishTank(), validator);
    try {
        fishTank.test5();
        fail("Expected exception wasn't raised");
    } catch (ConstraintViolationException e) {
        assertThat(e.getConstraintViolations()).containsOnlyViolations(violationOf(Size.class).withMessage("size must be between 0 and 5"));
    }
}
Also used : Size(jakarta.validation.constraints.Size) ConstraintViolationException(jakarta.validation.ConstraintViolationException) Validator(jakarta.validation.Validator) Test(org.testng.annotations.Test) TestForIssue(org.hibernate.validator.testutil.TestForIssue)

Example 28 with Size

use of jakarta.validation.constraints.Size in project hibernate-validator by hibernate.

the class AnnotationFactoryTest method createAnnotationProxy.

@Test
public void createAnnotationProxy() {
    AnnotationDescriptor.Builder<Size> descriptorBuilder = new AnnotationDescriptor.Builder<>(Size.class);
    descriptorBuilder.setAttribute("min", 5);
    descriptorBuilder.setAttribute("max", 10);
    Size size = descriptorBuilder.build().getAnnotation();
    assertEquals(size.min(), 5, "Wrong parameter value");
    assertEquals(size.max(), 10, "Wrong parameter value");
}
Also used : AnnotationDescriptor(org.hibernate.validator.internal.util.annotation.AnnotationDescriptor) Size(jakarta.validation.constraints.Size) Test(org.testng.annotations.Test)

Example 29 with Size

use of jakarta.validation.constraints.Size in project eclipselink by eclipse-ee4j.

the class AnnotationsProcessor method addFacets.

/**
 * @since 2.6
 * @author Marcel Valovy
 * @param property property for which facets will be generated
 */
private void addFacets(Property property) {
    final JavaHasAnnotations element = property.getElement();
    if (helper.isAnnotationPresent(element, DecimalMin.class)) {
        DecimalMin a = (DecimalMin) helper.getAnnotation(element, DecimalMin.class);
        DecimalMinFacet facet = new DecimalMinFacet(a.value(), a.inclusive());
        property.addFacet(facet);
    }
    if (helper.isAnnotationPresent(element, DecimalMax.class)) {
        DecimalMax a = (DecimalMax) helper.getAnnotation(element, DecimalMax.class);
        DecimalMaxFacet facet = new DecimalMaxFacet(a.value(), a.inclusive());
        property.addFacet(facet);
    }
    if (helper.isAnnotationPresent(element, Digits.class)) {
        Digits a = (Digits) helper.getAnnotation(element, Digits.class);
        DigitsFacet facet = new DigitsFacet(a.integer(), a.fraction());
        property.addFacet(facet);
    }
    if (helper.isAnnotationPresent(element, Max.class)) {
        Max a = (Max) helper.getAnnotation(element, Max.class);
        MaxFacet facet = new MaxFacet(a.value());
        property.addFacet(facet);
    }
    if (helper.isAnnotationPresent(element, Min.class)) {
        Min a = (Min) helper.getAnnotation(element, Min.class);
        MinFacet facet = new MinFacet(a.value());
        property.addFacet(facet);
    }
    if (helper.isAnnotationPresent(element, NotNull.class)) {
        property.setNotNullAnnotated(true);
    }
    if (helper.isAnnotationPresent(element, Pattern.class)) {
        Pattern a = (Pattern) helper.getAnnotation(element, Pattern.class);
        PatternFacet facet = new PatternFacet(a.regexp(), a.flags());
        property.addFacet(facet);
    }
    /* Example:
            @Pattern.List({
                @Pattern(regexp = "first_expression", message = "first.Pattern.message"),
                @Pattern(regexp = "second_expression", message = "second.Pattern.message"),
                @Pattern(regexp = "third_expression", message = "third.Pattern.message")
        }) */
    if (helper.isAnnotationPresent(element, Pattern.List.class)) {
        Pattern.List a = (Pattern.List) helper.getAnnotation(element, Pattern.List.class);
        PatternListFacet facet = new PatternListFacet(new ArrayList<>());
        for (Pattern pat : a.value()) {
            PatternFacet pf = new PatternFacet(pat.regexp(), pat.flags());
            facet.addPattern(pf);
        }
        property.addFacet(facet);
    }
    if (helper.isAnnotationPresent(element, Size.class)) {
        Size a = (Size) helper.getAnnotation(element, Size.class);
        final int min = a.min();
        final int max = a.max();
        if (min != 0 || max != Integer.MAX_VALUE) {
            // Fixes generation of an empty facet.
            if ("java.lang.String".equals(property.getType().getName())) {
                // @Size serves for both length facet and occurs restriction.
                // For minLength, maxLength.
                SizeFacet facet = new SizeFacet(min, max);
                property.addFacet(facet);
            } else {
                // 0 is default minBoundary.
                if (min > 0)
                    property.setMinOccurs(min);
                // 2^31 is default maxBoundary.
                if (max < Integer.MAX_VALUE)
                    property.setMaxOccurs(max);
            }
        }
    }
}
Also used : MinFacet(org.eclipse.persistence.jaxb.compiler.facets.MinFacet) DecimalMinFacet(org.eclipse.persistence.jaxb.compiler.facets.DecimalMinFacet) Pattern(jakarta.validation.constraints.Pattern) Max(jakarta.validation.constraints.Max) DecimalMax(jakarta.validation.constraints.DecimalMax) Size(jakarta.validation.constraints.Size) Digits(jakarta.validation.constraints.Digits) PatternFacet(org.eclipse.persistence.jaxb.compiler.facets.PatternFacet) DigitsFacet(org.eclipse.persistence.jaxb.compiler.facets.DigitsFacet) DecimalMin(jakarta.validation.constraints.DecimalMin) DecimalMaxFacet(org.eclipse.persistence.jaxb.compiler.facets.DecimalMaxFacet) PatternListFacet(org.eclipse.persistence.jaxb.compiler.facets.PatternListFacet) Min(jakarta.validation.constraints.Min) DecimalMin(jakarta.validation.constraints.DecimalMin) SizeFacet(org.eclipse.persistence.jaxb.compiler.facets.SizeFacet) MaxFacet(org.eclipse.persistence.jaxb.compiler.facets.MaxFacet) DecimalMaxFacet(org.eclipse.persistence.jaxb.compiler.facets.DecimalMaxFacet) ArrayList(java.util.ArrayList) XmlList(jakarta.xml.bind.annotation.XmlList) List(java.util.List) JavaHasAnnotations(org.eclipse.persistence.jaxb.javamodel.JavaHasAnnotations) DecimalMax(jakarta.validation.constraints.DecimalMax) DecimalMinFacet(org.eclipse.persistence.jaxb.compiler.facets.DecimalMinFacet)

Aggregations

Size (jakarta.validation.constraints.Size)29 Test (org.testng.annotations.Test)22 ConstraintViolationException (jakarta.validation.ConstraintViolationException)16 Validator (jakarta.validation.Validator)15 ConstraintMapping (org.hibernate.validator.cfg.ConstraintMapping)14 TestForIssue (org.hibernate.validator.testutil.TestForIssue)14 SizeDef (org.hibernate.validator.cfg.defs.SizeDef)11 HibernateValidator (org.hibernate.validator.HibernateValidator)8 ConstraintViolation (jakarta.validation.ConstraintViolation)5 ArrayList (java.util.ArrayList)5 Min (jakarta.validation.constraints.Min)4 Pattern (jakarta.validation.constraints.Pattern)4 ExecutableValidator (jakarta.validation.executable.ExecutableValidator)4 NotNull (jakarta.validation.constraints.NotNull)3 List (java.util.List)3 AssertFalse (jakarta.validation.constraints.AssertFalse)2 AssertTrue (jakarta.validation.constraints.AssertTrue)2 DecimalMax (jakarta.validation.constraints.DecimalMax)2 DecimalMin (jakarta.validation.constraints.DecimalMin)2 Max (jakarta.validation.constraints.Max)2