Search in sources :

Example 1 with Max

use of jakarta.validation.constraints.Max in project minijax by minijax.

the class MinijaxConstraintDescriptor method build.

@SuppressWarnings("unchecked")
public static <T extends Annotation> MinijaxConstraintDescriptor<T> build(final AnnotatedType annotatedType, final T annotation) {
    final Constraint constraint = annotation.annotationType().getAnnotation(Constraint.class);
    if (constraint == null) {
        return null;
    }
    final Class<?> valueClass = ReflectionUtils.getRawType(annotatedType);
    final Class<?> annotationClass = annotation.annotationType();
    if (constraint.validatedBy().length > 0) {
        return buildDeclaredValidator(annotation, constraint.validatedBy()[0]);
    } else if (annotationClass == AssertFalse.class) {
        return (MinijaxConstraintDescriptor<T>) buildAssertFalseValidator((AssertFalse) annotation, valueClass);
    } else if (annotationClass == AssertTrue.class) {
        return (MinijaxConstraintDescriptor<T>) buildAssertTrueValidator((AssertTrue) annotation, valueClass);
    } else if (annotationClass == Max.class) {
        return (MinijaxConstraintDescriptor<T>) buildMaxValidator((Max) annotation, valueClass);
    } else if (annotationClass == Min.class) {
        return (MinijaxConstraintDescriptor<T>) buildMinValidator((Min) annotation, valueClass);
    } else if (annotationClass == NotBlank.class) {
        return (MinijaxConstraintDescriptor<T>) buildNotBlankValidator((NotBlank) annotation, valueClass);
    } else if (annotationClass == NotEmpty.class) {
        return (MinijaxConstraintDescriptor<T>) buildNotEmptyValidator((NotEmpty) annotation, valueClass);
    } else if (annotationClass == NotNull.class) {
        return (MinijaxConstraintDescriptor<T>) buildNotNullValidator((NotNull) annotation);
    } else if (annotationClass == Pattern.class) {
        return (MinijaxConstraintDescriptor<T>) buildPatternValidator((Pattern) annotation, valueClass);
    } else if (annotationClass == Size.class) {
        return (MinijaxConstraintDescriptor<T>) buildSizeValidator((Size) annotation, valueClass);
    } else {
        throw new ValidationException("Unrecognized constraint annotation: " + annotation);
    }
}
Also used : Pattern(jakarta.validation.constraints.Pattern) ValidationException(jakarta.validation.ValidationException) Constraint(jakarta.validation.Constraint) Max(jakarta.validation.constraints.Max) Size(jakarta.validation.constraints.Size) AssertTrue(jakarta.validation.constraints.AssertTrue) NotNull(jakarta.validation.constraints.NotNull) Min(jakarta.validation.constraints.Min) NotBlank(jakarta.validation.constraints.NotBlank) AssertFalse(jakarta.validation.constraints.AssertFalse) NotEmpty(jakarta.validation.constraints.NotEmpty)

Example 2 with Max

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

the class ResourceBundleMessageInterpolatorTest method testCorrectMessageInterpolationIfParameterCannotBeReplaced.

@Test
@TestForIssue(jiraKey = "HV-182")
public void testCorrectMessageInterpolationIfParameterCannotBeReplaced() {
    ConstraintAnnotationDescriptor.Builder<Max> descriptorBuilder = new ConstraintAnnotationDescriptor.Builder<>(Max.class);
    String message = "Message should stay unchanged since {fubar} is not replaceable";
    descriptorBuilder.setMessage(message);
    descriptorBuilder.setAttribute("value", 10L);
    ConstraintAnnotationDescriptor<Max> maxDescriptor = descriptorBuilder.build();
    ConstraintDescriptorImpl<Max> constraintDescriptor = new ConstraintDescriptorImpl<Max>(ConstraintHelper.forAllBuiltinConstraints(), null, maxDescriptor, ConstraintLocationKind.FIELD);
    interpolator = new ResourceBundleMessageInterpolator(new TestResourceBundleLocator());
    MessageInterpolator.Context messageInterpolatorContext = createMessageInterpolatorContext(constraintDescriptor);
    String actual = interpolator.interpolate(maxDescriptor.getMessage(), messageInterpolatorContext);
    assertEquals(actual, message, "The message should not have changed.");
}
Also used : ResourceBundleMessageInterpolator(org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator) ConstraintAnnotationDescriptor(org.hibernate.validator.internal.util.annotation.ConstraintAnnotationDescriptor) Max(jakarta.validation.constraints.Max) ConstraintDescriptorImpl(org.hibernate.validator.internal.metadata.descriptor.ConstraintDescriptorImpl) ResourceBundleMessageInterpolator(org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator) MessageInterpolator(jakarta.validation.MessageInterpolator) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest) TestForIssue(org.hibernate.validator.testutil.TestForIssue)

Example 3 with Max

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

the class MaxValidatorForNumberTest method testIsValidMax.

@Test
public void testIsValidMax() {
    ConstraintAnnotationDescriptor.Builder<Max> descriptorBuilder = new ConstraintAnnotationDescriptor.Builder<>(Max.class);
    descriptorBuilder.setAttribute("value", 15L);
    descriptorBuilder.setMessage("{validator.max}");
    Max m = descriptorBuilder.build().getAnnotation();
    testMax(m, true);
}
Also used : ConstraintAnnotationDescriptor(org.hibernate.validator.internal.util.annotation.ConstraintAnnotationDescriptor) DecimalMax(jakarta.validation.constraints.DecimalMax) Max(jakarta.validation.constraints.Max) Test(org.testng.annotations.Test)

Example 4 with Max

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

the class MinMaxValidatorBoundaryTest method testMaxBoundaryValue.

@Test
public void testMaxBoundaryValue() {
    Validator validator = ValidatorUtil.getValidator();
    this.min = Long.MAX_VALUE;
    this.max = 9223372036854775807L;
    // Current max value is bigger, should fail, but it doesn't
    Set<ConstraintViolation<MinMaxValidatorBoundaryTest>> constraintViolations = validator.validate(this);
    assertThat(constraintViolations).containsOnlyViolations(violationOf(Max.class));
}
Also used : Max(jakarta.validation.constraints.Max) ConstraintViolation(jakarta.validation.ConstraintViolation) AbstractMaxValidator(org.hibernate.validator.internal.constraintvalidators.bv.number.bound.AbstractMaxValidator) Validator(jakarta.validation.Validator) Test(org.testng.annotations.Test)

Example 5 with Max

use of jakarta.validation.constraints.Max in project hibernate-orm by hibernate.

the class TypeSafeActivator method applyMax.

private static void applyMax(Property property, ConstraintDescriptor<?> descriptor, Dialect dialect) {
    if (Max.class.equals(descriptor.getAnnotation().annotationType())) {
        @SuppressWarnings("unchecked") ConstraintDescriptor<Max> maxConstraint = (ConstraintDescriptor<Max>) descriptor;
        long max = maxConstraint.getAnnotation().value();
        for (Selectable selectable : property.getSelectables()) {
            if (selectable instanceof Column) {
                Column col = (Column) selectable;
                String checkConstraint = col.getQuotedName(dialect) + "<=" + max;
                applySQLCheck(col, checkConstraint);
            }
        }
    }
}
Also used : Max(jakarta.validation.constraints.Max) Selectable(org.hibernate.mapping.Selectable) Column(org.hibernate.mapping.Column) ConstraintDescriptor(jakarta.validation.metadata.ConstraintDescriptor)

Aggregations

Max (jakarta.validation.constraints.Max)10 Test (org.testng.annotations.Test)6 DecimalMax (jakarta.validation.constraints.DecimalMax)5 ConstraintAnnotationDescriptor (org.hibernate.validator.internal.util.annotation.ConstraintAnnotationDescriptor)5 TestForIssue (org.hibernate.validator.testutil.TestForIssue)3 MessageInterpolator (jakarta.validation.MessageInterpolator)2 Min (jakarta.validation.constraints.Min)2 Pattern (jakarta.validation.constraints.Pattern)2 Size (jakarta.validation.constraints.Size)2 BigDecimal (java.math.BigDecimal)2 ConstraintDescriptorImpl (org.hibernate.validator.internal.metadata.descriptor.ConstraintDescriptorImpl)2 ResourceBundleMessageInterpolator (org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator)2 BeforeTest (org.testng.annotations.BeforeTest)2 Constraint (jakarta.validation.Constraint)1 ConstraintViolation (jakarta.validation.ConstraintViolation)1 ValidationException (jakarta.validation.ValidationException)1 Validator (jakarta.validation.Validator)1 AssertFalse (jakarta.validation.constraints.AssertFalse)1 AssertTrue (jakarta.validation.constraints.AssertTrue)1 DecimalMin (jakarta.validation.constraints.DecimalMin)1