Search in sources :

Example 1 with Pattern

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

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

the class JakartaValidationModule method resolveStringPattern.

/**
 * Determine a given text type's pattern.
 *
 * @param member the field or method to check
 * @return specified pattern (or null)
 * @see Pattern
 */
protected String resolveStringPattern(MemberScope<?, ?> member) {
    if (member.getType().isInstanceOf(CharSequence.class)) {
        Pattern patternAnnotation = this.getAnnotationFromFieldOrGetter(member, Pattern.class, Pattern::groups);
        if (patternAnnotation != null) {
            // @Pattern annotation was found, return its (mandatory) regular expression
            return patternAnnotation.regexp();
        }
        Email emailAnnotation = this.getAnnotationFromFieldOrGetter(member, Email.class, Email::groups);
        if (emailAnnotation != null && !".*".equals(emailAnnotation.regexp())) {
            // non-default regular expression on @Email annotation should also be considered
            return emailAnnotation.regexp();
        }
    }
    return null;
}
Also used : Pattern(jakarta.validation.constraints.Pattern) Email(jakarta.validation.constraints.Email)

Example 3 with Pattern

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

the class PatternValidatorTest method testIsValidForEmptyStringRegexp.

@Test
public void testIsValidForEmptyStringRegexp() {
    ConstraintAnnotationDescriptor.Builder<Pattern> descriptorBuilder = new ConstraintAnnotationDescriptor.Builder<>(Pattern.class);
    descriptorBuilder.setAttribute("regexp", "|^.*foo$");
    descriptorBuilder.setMessage("pattern does not match");
    Pattern p = descriptorBuilder.build().getAnnotation();
    PatternValidator constraint = new PatternValidator();
    constraint.initialize(p);
    assertTrue(constraint.isValid(null, null));
    assertTrue(constraint.isValid("", null));
    assertFalse(constraint.isValid("bla bla", null));
    assertTrue(constraint.isValid("foo", null));
    assertTrue(constraint.isValid("a b c foo", null));
}
Also used : Pattern(jakarta.validation.constraints.Pattern) ConstraintAnnotationDescriptor(org.hibernate.validator.internal.util.annotation.ConstraintAnnotationDescriptor) PatternValidator(org.hibernate.validator.internal.constraintvalidators.bv.PatternValidator) Test(org.testng.annotations.Test)

Example 4 with Pattern

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

the class PatternValidatorTest method testIsValidForCharSequence.

@Test
@TestForIssue(jiraKey = "HV-502")
public void testIsValidForCharSequence() {
    ConstraintAnnotationDescriptor.Builder<Pattern> descriptorBuilder = new ConstraintAnnotationDescriptor.Builder<>(Pattern.class);
    descriptorBuilder.setAttribute("regexp", "char sequence");
    Pattern p = descriptorBuilder.build().getAnnotation();
    PatternValidator constraint = new PatternValidator();
    constraint.initialize(p);
    assertTrue(constraint.isValid(new MyCustomStringImpl("char sequence"), null));
}
Also used : Pattern(jakarta.validation.constraints.Pattern) ConstraintAnnotationDescriptor(org.hibernate.validator.internal.util.annotation.ConstraintAnnotationDescriptor) PatternValidator(org.hibernate.validator.internal.constraintvalidators.bv.PatternValidator) MyCustomStringImpl(org.hibernate.validator.testutil.MyCustomStringImpl) Test(org.testng.annotations.Test) TestForIssue(org.hibernate.validator.testutil.TestForIssue)

Example 5 with Pattern

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

the class PatternValidatorTest method testIsValid.

@Test
public void testIsValid() {
    ConstraintAnnotationDescriptor.Builder<Pattern> descriptorBuilder = new ConstraintAnnotationDescriptor.Builder<>(Pattern.class);
    descriptorBuilder.setAttribute("regexp", "foobar");
    descriptorBuilder.setMessage("pattern does not match");
    Pattern p = descriptorBuilder.build().getAnnotation();
    PatternValidator constraint = new PatternValidator();
    constraint.initialize(p);
    assertTrue(constraint.isValid(null, null));
    assertFalse(constraint.isValid("", null));
    assertFalse(constraint.isValid("bla bla", null));
    assertFalse(constraint.isValid("This test is not foobar", null));
}
Also used : Pattern(jakarta.validation.constraints.Pattern) ConstraintAnnotationDescriptor(org.hibernate.validator.internal.util.annotation.ConstraintAnnotationDescriptor) PatternValidator(org.hibernate.validator.internal.constraintvalidators.bv.PatternValidator) Test(org.testng.annotations.Test)

Aggregations

Pattern (jakarta.validation.constraints.Pattern)11 Test (org.testng.annotations.Test)6 Size (jakarta.validation.constraints.Size)4 PatternValidator (org.hibernate.validator.internal.constraintvalidators.bv.PatternValidator)4 ConstraintAnnotationDescriptor (org.hibernate.validator.internal.util.annotation.ConstraintAnnotationDescriptor)4 Field (java.lang.reflect.Field)3 AssertFalse (jakarta.validation.constraints.AssertFalse)2 AssertTrue (jakarta.validation.constraints.AssertTrue)2 DecimalMax (jakarta.validation.constraints.DecimalMax)2 DecimalMin (jakarta.validation.constraints.DecimalMin)2 Max (jakarta.validation.constraints.Max)2 Min (jakarta.validation.constraints.Min)2 NotNull (jakarta.validation.constraints.NotNull)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Constraint (jakarta.validation.Constraint)1 Valid (jakarta.validation.Valid)1 ValidationException (jakarta.validation.ValidationException)1 Digits (jakarta.validation.constraints.Digits)1 Email (jakarta.validation.constraints.Email)1