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