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;
}
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);
}
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);
}
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
}
}
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))));
}
Aggregations