Search in sources :

Example 1 with PositiveOrZero

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

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

the class NegativePositiveValidatorForStringTest method testIsValidPositiveOrZeroValidator.

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

Aggregations

PositiveOrZero (jakarta.validation.constraints.PositiveOrZero)2 DecimalMin (jakarta.validation.constraints.DecimalMin)1 Min (jakarta.validation.constraints.Min)1 BigDecimal (java.math.BigDecimal)1 PositiveOrZeroValidatorForCharSequence (org.hibernate.validator.internal.constraintvalidators.bv.number.sign.PositiveOrZeroValidatorForCharSequence)1 ConstraintAnnotationDescriptor (org.hibernate.validator.internal.util.annotation.ConstraintAnnotationDescriptor)1 Test (org.testng.annotations.Test)1