Search in sources :

Example 1 with DecimalMax

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

the class JakartaValidationModule method resolveNumberExclusiveMaximum.

/**
 * Determine a number type's maximum (exclusive) value.
 *
 * @param member the field or method to check
 * @return specified exclusive maximum value (or null)
 * @see DecimalMax#inclusive()
 * @see Negative
 */
protected BigDecimal resolveNumberExclusiveMaximum(MemberScope<?, ?> member) {
    DecimalMax decimalMaxAnnotation = this.getAnnotationFromFieldOrGetter(member, DecimalMax.class, DecimalMax::groups);
    if (decimalMaxAnnotation != null && !decimalMaxAnnotation.inclusive()) {
        return new BigDecimal(decimalMaxAnnotation.value());
    }
    Negative negativeAnnotation = this.getAnnotationFromFieldOrGetter(member, Negative.class, Negative::groups);
    if (negativeAnnotation != null) {
        return BigDecimal.ZERO;
    }
    return null;
}
Also used : Negative(jakarta.validation.constraints.Negative) DecimalMax(jakarta.validation.constraints.DecimalMax) BigDecimal(java.math.BigDecimal)

Example 2 with DecimalMax

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

the class MaxValidatorForNumberTest method testInitializeDecimalMaxWithInvalidValue.

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

Example 3 with DecimalMax

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

the class MaxValidatorForStringTest method testIsValidDecimalMaxExclusive.

@Test
@TestForIssue(jiraKey = "HV-256")
public void testIsValidDecimalMaxExclusive() {
    boolean inclusive = false;
    ConstraintAnnotationDescriptor.Builder<DecimalMax> descriptorBuilder = new ConstraintAnnotationDescriptor.Builder<>(DecimalMax.class);
    descriptorBuilder.setAttribute("value", "15.0E0");
    descriptorBuilder.setAttribute("inclusive", inclusive);
    descriptorBuilder.setMessage("{validator.max}");
    DecimalMax m = descriptorBuilder.build().getAnnotation();
    DecimalMaxValidatorForCharSequence constraint = new DecimalMaxValidatorForCharSequence();
    constraint.initialize(m);
    testMaxValidator(constraint, inclusive);
}
Also used : ConstraintAnnotationDescriptor(org.hibernate.validator.internal.util.annotation.ConstraintAnnotationDescriptor) DecimalMax(jakarta.validation.constraints.DecimalMax) DecimalMaxValidatorForCharSequence(org.hibernate.validator.internal.constraintvalidators.bv.number.bound.decimal.DecimalMaxValidatorForCharSequence) Test(org.testng.annotations.Test) TestForIssue(org.hibernate.validator.testutil.TestForIssue)

Example 4 with DecimalMax

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

the class MaxValidatorForStringTest method testInitializeDecimalMaxWithInvalidValue.

@Test
public void testInitializeDecimalMaxWithInvalidValue() {
    ConstraintAnnotationDescriptor.Builder<DecimalMax> descriptorBuilder = new ConstraintAnnotationDescriptor.Builder<>(DecimalMax.class);
    descriptorBuilder.setAttribute("value", "foobar");
    descriptorBuilder.setMessage("{validator.max}");
    DecimalMax m = descriptorBuilder.build().getAnnotation();
    DecimalMaxValidatorForNumber constraint = new DecimalMaxValidatorForNumber();
    try {
        constraint.initialize(m);
        fail();
    } catch (IllegalArgumentException e) {
    // success
    }
}
Also used : ConstraintAnnotationDescriptor(org.hibernate.validator.internal.util.annotation.ConstraintAnnotationDescriptor) DecimalMaxValidatorForNumber(org.hibernate.validator.internal.constraintvalidators.bv.number.bound.decimal.DecimalMaxValidatorForNumber) DecimalMax(jakarta.validation.constraints.DecimalMax) Test(org.testng.annotations.Test)

Example 5 with DecimalMax

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

the class MaxValidatorForNumberTest method testIsValidDecimalMaxWithDecimalFractionInConstraint.

@Test
public void testIsValidDecimalMaxWithDecimalFractionInConstraint() {
    class Foo {

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

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

Aggregations

DecimalMax (jakarta.validation.constraints.DecimalMax)12 Test (org.testng.annotations.Test)8 ConstraintAnnotationDescriptor (org.hibernate.validator.internal.util.annotation.ConstraintAnnotationDescriptor)7 DecimalMaxValidatorForNumber (org.hibernate.validator.internal.constraintvalidators.bv.number.bound.decimal.DecimalMaxValidatorForNumber)4 DecimalMin (jakarta.validation.constraints.DecimalMin)2 Max (jakarta.validation.constraints.Max)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 DecimalMaxValidatorForCharSequence (org.hibernate.validator.internal.constraintvalidators.bv.number.bound.decimal.DecimalMaxValidatorForCharSequence)2 TestForIssue (org.hibernate.validator.testutil.TestForIssue)2 Valid (jakarta.validation.Valid)1 Validator (jakarta.validation.Validator)1 Digits (jakarta.validation.constraints.Digits)1 Min (jakarta.validation.constraints.Min)1 Negative (jakarta.validation.constraints.Negative)1 NegativeOrZero (jakarta.validation.constraints.NegativeOrZero)1 NotNull (jakarta.validation.constraints.NotNull)1