Search in sources :

Example 1 with DecimalMin

use of jakarta.validation.constraints.DecimalMin in project jsonschema-generator by victools.

the class JakartaValidationModule method resolveNumberInclusiveMinimum.

/**
 * Determine a number type's minimum (inclusive) value.
 *
 * @param member the field or method to check
 * @return specified inclusive minimum value (or null)
 * @see Min
 * @see DecimalMin
 * @see PositiveOrZero
 */
protected BigDecimal resolveNumberInclusiveMinimum(MemberScope<?, ?> member) {
    Min minAnnotation = this.getAnnotationFromFieldOrGetter(member, Min.class, Min::groups);
    if (minAnnotation != null) {
        return new BigDecimal(minAnnotation.value());
    }
    DecimalMin decimalMinAnnotation = this.getAnnotationFromFieldOrGetter(member, DecimalMin.class, DecimalMin::groups);
    if (decimalMinAnnotation != null && decimalMinAnnotation.inclusive()) {
        return new BigDecimal(decimalMinAnnotation.value());
    }
    PositiveOrZero positiveAnnotation = this.getAnnotationFromFieldOrGetter(member, PositiveOrZero.class, PositiveOrZero::groups);
    if (positiveAnnotation != null) {
        return BigDecimal.ZERO;
    }
    return null;
}
Also used : DecimalMin(jakarta.validation.constraints.DecimalMin) Min(jakarta.validation.constraints.Min) PositiveOrZero(jakarta.validation.constraints.PositiveOrZero) DecimalMin(jakarta.validation.constraints.DecimalMin) BigDecimal(java.math.BigDecimal)

Example 2 with DecimalMin

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

the class MinValidatorForNumberTest method testIsValidDecimalMinExclusive.

@Test
@TestForIssue(jiraKey = "HV-256")
public void testIsValidDecimalMinExclusive() {
    boolean inclusive = false;
    ConstraintAnnotationDescriptor.Builder<DecimalMin> descriptorBuilder = new ConstraintAnnotationDescriptor.Builder<>(DecimalMin.class);
    descriptorBuilder.setAttribute("value", "1500E-2");
    descriptorBuilder.setAttribute("inclusive", inclusive);
    descriptorBuilder.setMessage("{validator.min}");
    DecimalMin m = descriptorBuilder.build().getAnnotation();
    testDecimalMin(m, inclusive);
}
Also used : ConstraintAnnotationDescriptor(org.hibernate.validator.internal.util.annotation.ConstraintAnnotationDescriptor) DecimalMin(jakarta.validation.constraints.DecimalMin) Test(org.testng.annotations.Test) TestForIssue(org.hibernate.validator.testutil.TestForIssue)

Example 3 with DecimalMin

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

the class MinValidatorForNumberTest method testIsValidDecimalMinWithDecimalFractionInConstraint.

@Test
public void testIsValidDecimalMinWithDecimalFractionInConstraint() {
    class Foo {

        @DecimalMin("15.0001")
        private final Number num;

        Foo(final Number num) {
            this.num = num;
        }
    }
    Validator validator = getValidator();
    assertThat(validator.validate(new Foo(15))).containsOnlyViolations(violationOf(DecimalMin.class));
    assertNoViolations(validator.validate(new Foo(16)));
    assertThat(validator.validate(new Foo(15.00001))).containsOnlyViolations(violationOf(DecimalMin.class));
    assertNoViolations(validator.validate(new Foo(15.01)));
    assertThat(validator.validate(new Foo(BigDecimal.valueOf(15.00001)))).containsOnlyViolations(violationOf(DecimalMin.class));
    assertNoViolations(validator.validate(new Foo(BigDecimal.valueOf(15.01))));
}
Also used : MinValidatorForNumber(org.hibernate.validator.internal.constraintvalidators.bv.number.bound.MinValidatorForNumber) DecimalMinValidatorForNumber(org.hibernate.validator.internal.constraintvalidators.bv.number.bound.decimal.DecimalMinValidatorForNumber) DecimalMin(jakarta.validation.constraints.DecimalMin) Validator(jakarta.validation.Validator) AbstractMinValidator(org.hibernate.validator.internal.constraintvalidators.bv.number.bound.AbstractMinValidator) ValidatorUtil.getValidator(org.hibernate.validator.testutils.ValidatorUtil.getValidator) AbstractDecimalMinValidator(org.hibernate.validator.internal.constraintvalidators.bv.number.bound.decimal.AbstractDecimalMinValidator) Test(org.testng.annotations.Test)

Example 4 with DecimalMin

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

the class MinValidatorForNumberTest method testInitializeDecimalMinWithInvalidValue.

@Test(expectedExceptions = IllegalArgumentException.class)
public void testInitializeDecimalMinWithInvalidValue() {
    ConstraintAnnotationDescriptor.Builder<DecimalMin> descriptorBuilder = new ConstraintAnnotationDescriptor.Builder<>(DecimalMin.class);
    descriptorBuilder.setAttribute("value", "foobar");
    descriptorBuilder.setMessage("{validator.min}");
    DecimalMin m = descriptorBuilder.build().getAnnotation();
    DecimalMinValidatorForNumber constraint = new DecimalMinValidatorForNumber();
    constraint.initialize(m);
}
Also used : ConstraintAnnotationDescriptor(org.hibernate.validator.internal.util.annotation.ConstraintAnnotationDescriptor) DecimalMin(jakarta.validation.constraints.DecimalMin) DecimalMinValidatorForNumber(org.hibernate.validator.internal.constraintvalidators.bv.number.bound.decimal.DecimalMinValidatorForNumber) Test(org.testng.annotations.Test)

Example 5 with DecimalMin

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

the class BeanValidationBindingsTestCase method testAllFacetsAndAnnotations.

/**
 * Tests that the XJC detects all facets and generates their respective
 * annotations correctly.
 */
public void testAllFacetsAndAnnotations() throws Throwable {
    pkg = "rs";
    Class<?>[] c = roundTrip(RICH_SCHEMA_PATH, pkg);
    Class<?> Main = c[0];
    Class<?> Numbers = c[1];
    Class<?> NumberWithHiddenValueAttribute = c[2];
    Class<?> Strings = c[4];
    XmlElement xmlElement;
    Size size;
    DecimalMax decimalMax;
    DecimalMin decimalMin;
    Pattern.List patternList;
    Pattern pattern;
    /* Main.class */
    Field numbers = Main.getDeclaredField("numbers");
    assertNotNull(numbers.getAnnotation(Valid.class));
    assertNotNull(numbers.getAnnotation(NotNull.class));
    xmlElement = numbers.getAnnotation(XmlElement.class);
    assertFalse(xmlElement.nillable());
    Field strings = Main.getDeclaredField("strings");
    size = strings.getAnnotation(Size.class);
    assertTrue(size.min() == 1 && size.max() == 2);
    assertNotNull(strings.getAnnotation(Valid.class));
    assertNotNull(strings.getAnnotation(NotNull.class));
    xmlElement = strings.getAnnotation(XmlElement.class);
    assertFalse(xmlElement.nillable());
    Field unsignedByte = Main.getDeclaredField("unsignedByte");
    decimalMax = unsignedByte.getAnnotation(DecimalMax.class);
    assertEquals(decimalMax.value(), "255");
    assertTrue(decimalMax.inclusive());
    decimalMin = unsignedByte.getAnnotation(DecimalMin.class);
    assertEquals(decimalMin.value(), "0");
    assertTrue(decimalMin.inclusive());
    Field byteArray = Main.getDeclaredField("byteArray");
    size = byteArray.getAnnotation(Size.class);
    assertTrue(size.max() == 18);
    Field someCollection = Main.getDeclaredField("someCollection");
    size = someCollection.getAnnotation(Size.class);
    assertTrue(size.min() == 1);
    assertNotNull(someCollection.getAnnotation(Valid.class));
    assertNotNull(someCollection.getAnnotation(NotNull.class));
    Field optionalElement = Main.getDeclaredField("optionalElement");
    size = optionalElement.getAnnotation(Size.class);
    assertTrue(size.min() == 0);
    assertNotNull(optionalElement.getAnnotation(Valid.class));
    assertNull(optionalElement.getAnnotation(NotNull.class));
    Field optionalElementWithSimpleType = Main.getDeclaredField("optionalElementWithSimpleType");
    assertNull(optionalElementWithSimpleType.getAnnotation(NotNull.class));
    decimalMin = optionalElementWithSimpleType.getAnnotation(DecimalMin.class);
    assertEquals(decimalMin.value(), "1");
    decimalMax = optionalElementWithSimpleType.getAnnotation(DecimalMax.class);
    assertEquals(decimalMax.value(), "31");
    /* Numbers.class */
    Field minInclusive = Numbers.getDeclaredField("minInclusive");
    decimalMin = minInclusive.getAnnotation(DecimalMin.class);
    assertEquals(decimalMin.value(), "1000");
    assertTrue(decimalMin.inclusive());
    Field maxInclusive = Numbers.getDeclaredField("maxInclusive");
    decimalMax = maxInclusive.getAnnotation(DecimalMax.class);
    assertEquals(decimalMax.value(), "1000");
    assertTrue(decimalMax.inclusive());
    Field minExclusive = Numbers.getDeclaredField("minExclusive");
    decimalMin = minExclusive.getAnnotation(DecimalMin.class);
    assertEquals(decimalMin.value(), "0");
    assertFalse(decimalMin.inclusive());
    Field maxExclusive = Numbers.getDeclaredField("maxExclusive");
    decimalMax = maxExclusive.getAnnotation(DecimalMax.class);
    assertEquals(decimalMax.value(), "1000");
    assertFalse(decimalMax.inclusive());
    Field minMaxExclusive = Numbers.getDeclaredField("minMaxExclusive");
    decimalMax = minMaxExclusive.getAnnotation(DecimalMax.class);
    assertEquals(decimalMax.value(), "9223372");
    assertFalse(decimalMax.inclusive());
    decimalMin = minMaxExclusive.getAnnotation(DecimalMin.class);
    assertEquals(decimalMin.value(), "0");
    assertFalse(decimalMin.inclusive());
    /* NumberWithHiddenValueAttribute.class */
    Field value = NumberWithHiddenValueAttribute.getDeclaredField("value");
    assertNotNull(value.getAnnotation(XmlValue.class));
    size = value.getAnnotation(Size.class);
    assertTrue(size.min() == 1 && size.max() == 5);
    Field code = NumberWithHiddenValueAttribute.getDeclaredField("code");
    assertNotNull(code.getAnnotation(NotNull.class));
    Field whatNumber = NumberWithHiddenValueAttribute.getDeclaredField("whatNumber");
    XmlAttribute xmlAttribute = whatNumber.getAnnotation(XmlAttribute.class);
    assertTrue(xmlAttribute.required());
    /* Strings.class */
    Field regexShorthands = Strings.getDeclaredField("regexShorthands");
    patternList = regexShorthands.getAnnotation(Pattern.List.class);
    Pattern[] patterns = patternList.value();
    assertEquals("[:A-Z_a-z\\u00C0-\\u00D6\\u00D8-\\u00F6\\u00F8-\\u02FF\\u0370-\\u037D\\u037F-\\u1FFF\\u200C-\\u200D\\u2070-\\u218F\\u2C00-\\u2FEF\\u3001-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFFD]", patterns[0].regexp());
    assertEquals("[^:A-Z_a-z\\u00C0-\\u00D6\\u00D8-\\u00F6\\u00F8-\\u02FF\\u0370-\\u037D\\u037F-\\u1FFF\\u200C-\\u200D\\u2070-\\u218F\\u2C00-\\u2FEF\\u3001-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFFD]", patterns[1].regexp());
    assertEquals("[-.0-9:A-Z_a-z\\u00B7\\u00C0-\\u00D6\\u00D8-\\u00F6\\u00F8-\\u037D\\u037F-\\u1FFF\\u200C-\\u200D\\u203F\\u2040\\u2070-\\u218F\\u2C00-\\u2FEF\\u3001-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFFD]", patterns[2].regexp());
    assertEquals("[^-.0-9:A-Z_a-z\\u00B7\\u00C0-\\u00D6\\u00D8-\\u00F6\\u00F8-\\u037D\\u037F-\\u1FFF\\u200C-\\u200D\\u203F\\u2040\\u2070-\\u218F\\u2C00-\\u2FEF\\u3001-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFFD]", patterns[3].regexp());
    assertEquals("\\p{Nd}", patterns[4].regexp());
    assertEquals("\\P{Nd}", patterns[5].regexp());
    assertEquals("[\\u0009-\\u000D\\u0020\\u0085\\u00A0\\u1680\\u180E\\u2000-\\u200A\\u2028\\u2029\\u202F\\u205F\\u3000]", patterns[6].regexp());
    assertEquals("[^\\u0009-\\u000D\\u0020\\u0085\\u00A0\\u1680\\u180E\\u2000-\\u200A\\u2028\\u2029\\u202F\\u205F\\u3000]", patterns[7].regexp());
    assertEquals("[\\u0009\\u0020\\u00A0\\u1680\\u180E\\u2000-\\u200A\\u202F\\u205F\\u3000]", patterns[8].regexp());
    assertEquals("[^\\u0009\\u0020\\u00A0\\u1680\\u180E\\u2000\\u2001-\\u200A\\u202F\\u205F\\u3000]", patterns[9].regexp());
    assertEquals("[^\\u000A-\\u000D\\u0085\\u2028\\u2029]", patterns[10].regexp());
    assertEquals("(?:(?>\\u000D\\u000A)|[\\u000A\\u000B\\u000C\\u000D\\u0085\\u2028\\u2029])", patterns[11].regexp());
    Field idType = Strings.getDeclaredField("idType");
    pattern = idType.getAnnotation(Pattern.class);
    assertEquals(pattern.regexp(), "[[:A-Z_a-z\\u00C0-\\u00D6\\u00D8-\\u00F6\\u00F8-\\u02FF\\u0370-\\u037D\\u037F-\\u1FFF\\u200C-\\u200D\\u2070-\\u218F\\u2C00-\\u2FEF\\u3001-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFFD]-[:]][[-.0-9:A-Z_a-z\\u00B7\\u00C0-\\u00D6\\u00D8-\\u00F6\\u00F8-\\u037D\\u037F-\\u1FFF\\u200C-\\u200D\\u203F\\u2040\\u2070-\\u218F\\u2C00-\\u2FEF\\u3001-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFFD]-[:]]*");
    assertTrue(idType.getAnnotation(Size.class).max() == 100);
    Field genericString = Strings.getDeclaredField("genericString");
    assertTrue(genericString.getAnnotation(Size.class).min() == 0);
    assertTrue(genericString.getAnnotation(Size.class).max() == 1024);
    Field maxLength = Strings.getDeclaredField("maxLength");
    assertTrue(maxLength.getAnnotation(Size.class).max() == 1024);
    Field minLength = Strings.getDeclaredField("minLength");
    assertTrue(minLength.getAnnotation(Size.class).min() == 0);
}
Also used : Pattern(jakarta.validation.constraints.Pattern) XmlAttribute(jakarta.xml.bind.annotation.XmlAttribute) Size(jakarta.validation.constraints.Size) DecimalMin(jakarta.validation.constraints.DecimalMin) NotNull(jakarta.validation.constraints.NotNull) Field(java.lang.reflect.Field) Valid(jakarta.validation.Valid) XmlElement(jakarta.xml.bind.annotation.XmlElement) XmlValue(jakarta.xml.bind.annotation.XmlValue) ArrayList(java.util.ArrayList) List(java.util.List) DecimalMax(jakarta.validation.constraints.DecimalMax)

Aggregations

DecimalMin (jakarta.validation.constraints.DecimalMin)11 Test (org.testng.annotations.Test)7 ConstraintAnnotationDescriptor (org.hibernate.validator.internal.util.annotation.ConstraintAnnotationDescriptor)6 DecimalMinValidatorForNumber (org.hibernate.validator.internal.constraintvalidators.bv.number.bound.decimal.DecimalMinValidatorForNumber)3 DecimalMax (jakarta.validation.constraints.DecimalMax)2 Min (jakarta.validation.constraints.Min)2 Pattern (jakarta.validation.constraints.Pattern)2 Size (jakarta.validation.constraints.Size)2 BigDecimal (java.math.BigDecimal)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 DecimalMinValidatorForCharSequence (org.hibernate.validator.internal.constraintvalidators.bv.number.bound.decimal.DecimalMinValidatorForCharSequence)2 TestForIssue (org.hibernate.validator.testutil.TestForIssue)2 Valid (jakarta.validation.Valid)1 Validator (jakarta.validation.Validator)1 Digits (jakarta.validation.constraints.Digits)1 Max (jakarta.validation.constraints.Max)1 NotNull (jakarta.validation.constraints.NotNull)1 Positive (jakarta.validation.constraints.Positive)1 PositiveOrZero (jakarta.validation.constraints.PositiveOrZero)1