Search in sources :

Example 1 with NegativeOrZero

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

the class NegativePositiveValidatorForStringTest method testIsValidNegativeOrZeroValidator.

@Test
public void testIsValidNegativeOrZeroValidator() {
    ConstraintAnnotationDescriptor.Builder<NegativeOrZero> descriptorBuilder = new ConstraintAnnotationDescriptor.Builder<>(NegativeOrZero.class);
    descriptorBuilder.setMessage("{validator.negativeOrZero}");
    NegativeOrZero m = descriptorBuilder.build().getAnnotation();
    NegativeOrZeroValidatorForCharSequence constraint = new NegativeOrZeroValidatorForCharSequence();
    constraint.initialize(m);
    assertTrue(constraint.isValid(null, null));
    assertFalse(constraint.isValid("15", null));
    assertFalse(constraint.isValid("15.0", null));
    assertTrue(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 : NegativeOrZeroValidatorForCharSequence(org.hibernate.validator.internal.constraintvalidators.bv.number.sign.NegativeOrZeroValidatorForCharSequence) ConstraintAnnotationDescriptor(org.hibernate.validator.internal.util.annotation.ConstraintAnnotationDescriptor) NegativeOrZero(jakarta.validation.constraints.NegativeOrZero) Test(org.testng.annotations.Test)

Example 2 with NegativeOrZero

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

the class JakartaValidationModule method resolveNumberInclusiveMaximum.

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

Aggregations

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