Search in sources :

Example 1 with AssertTrue

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

use of jakarta.validation.constraints.AssertTrue in project eclipselink by eclipse-ee4j.

the class BeanValidationBindingsTestCase method testFacetCustomizationsAndCustomFacets.

/**
 * Tests customizations, i.e. facet customizations + custom facets, i.e.
 * Future, Past, AssertTrue, AssertFalse.
 */
public void testFacetCustomizationsAndCustomFacets() throws Throwable {
    pkg = "cs";
    xjcGenerateJavaSourcesWithCustomizations(CUSTOMIZED_SCHEMA_PATH);
    compileGeneratedSources(createCompileList(pkg));
    Class<?> custom = loadCompiledClasses(createLoadList(pkg))[0];
    Field generic = custom.getDeclaredField("generic");
    Size s = generic.getAnnotation(Size.class);
    assertEquals(s.message(), "Hello, world!");
    assertEquals(s.groups()[0], BindingTeam.class);
    assertEquals(s.groups()[1], RocketTeam.class);
    assertEquals(s.max(), 4);
    Pattern p1 = generic.getAnnotation(Pattern.List.class).value()[0];
    Pattern p2 = generic.getAnnotation(Pattern.List.class).value()[1];
    assertEquals(p1.message(), p2.message());
    assertTrue(Arrays.equals(p1.groups(), p2.groups()));
    assertEquals(p1.message(), "Hello.");
    assertEquals(p1.groups()[0], Object.class);
    assertEquals(p1.regexp(), "10");
    Future f = generic.getAnnotation(Future.class);
    assertEquals(f.message(), "Welcome to the Future!");
    assertEquals(f.groups()[0], BindingTeam.class);
    Past p = generic.getAnnotation(Past.class);
    assertEquals(p.message(), "Farewell from the ancestors.");
    assertEquals(p.groups()[0], Ancestors.class);
    AssertTrue at = generic.getAnnotation(AssertTrue.class);
    assertEquals(at.message(), "True fan of the team!");
    assertEquals(at.groups()[0], BindingTeam.class);
    AssertFalse af = generic.getAnnotation(AssertFalse.class);
    assertEquals(af.message(), "false");
    assertEquals(af.groups()[0], Object.class);
    // To test a user custom annotation.
    CustomAnnotation xmlKey = generic.getAnnotation(CustomAnnotation.class);
    assertNotNull(xmlKey);
}
Also used : Field(java.lang.reflect.Field) Pattern(jakarta.validation.constraints.Pattern) Size(jakarta.validation.constraints.Size) Past(jakarta.validation.constraints.Past) AssertTrue(jakarta.validation.constraints.AssertTrue) Future(jakarta.validation.constraints.Future) AssertFalse(jakarta.validation.constraints.AssertFalse)

Aggregations

AssertFalse (jakarta.validation.constraints.AssertFalse)2 AssertTrue (jakarta.validation.constraints.AssertTrue)2 Pattern (jakarta.validation.constraints.Pattern)2 Size (jakarta.validation.constraints.Size)2 Constraint (jakarta.validation.Constraint)1 ValidationException (jakarta.validation.ValidationException)1 Future (jakarta.validation.constraints.Future)1 Max (jakarta.validation.constraints.Max)1 Min (jakarta.validation.constraints.Min)1 NotBlank (jakarta.validation.constraints.NotBlank)1 NotEmpty (jakarta.validation.constraints.NotEmpty)1 NotNull (jakarta.validation.constraints.NotNull)1 Past (jakarta.validation.constraints.Past)1 Field (java.lang.reflect.Field)1