Search in sources :

Example 1 with Negative

use of jakarta.validation.constraints.Negative 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 Negative

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

the class NegativePositiveValidatorForStringTest method testIsValidNegativeValidator.

@Test
public void testIsValidNegativeValidator() {
    ConstraintAnnotationDescriptor.Builder<Negative> descriptorBuilder = new ConstraintAnnotationDescriptor.Builder<>(Negative.class);
    descriptorBuilder.setMessage("{validator.negative}");
    Negative m = descriptorBuilder.build().getAnnotation();
    NegativeValidatorForCharSequence constraint = new NegativeValidatorForCharSequence();
    constraint.initialize(m);
    assertTrue(constraint.isValid(null, null));
    assertFalse(constraint.isValid("15", null));
    assertFalse(constraint.isValid("15.0", null));
    assertFalse(constraint.isValid("0", null));
    assertTrue(constraint.isValid("-10", null));
    assertTrue(constraint.isValid("-14.99", null));
    // number format exception
    assertFalse(constraint.isValid("15l", null));
}
Also used : NegativeValidatorForCharSequence(org.hibernate.validator.internal.constraintvalidators.bv.number.sign.NegativeValidatorForCharSequence) ConstraintAnnotationDescriptor(org.hibernate.validator.internal.util.annotation.ConstraintAnnotationDescriptor) Negative(jakarta.validation.constraints.Negative) Test(org.testng.annotations.Test)

Aggregations

Negative (jakarta.validation.constraints.Negative)2 DecimalMax (jakarta.validation.constraints.DecimalMax)1 BigDecimal (java.math.BigDecimal)1 NegativeValidatorForCharSequence (org.hibernate.validator.internal.constraintvalidators.bv.number.sign.NegativeValidatorForCharSequence)1 ConstraintAnnotationDescriptor (org.hibernate.validator.internal.util.annotation.ConstraintAnnotationDescriptor)1 Test (org.testng.annotations.Test)1