Search code examples
jakarta-eebean-validationcustom-validators

Create Validator wrapper tag


I created the custom validator tag which wrap @Pattern.

Here,

@Pattern(regexp = "~")
@Target({ ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER })
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = {})
public @interface CustomConstraint {

    String message() default "";
    Class[] groups() default {};
    Class[] payload() default {};
}

But, this tag has the problem which cannot set the attribute of "message". How I set the attribute of "message"?


Solution

  • What you are looking for is @OverridesAttribute:

    @Pattern(regexp = "~")
    @Target({ ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER })
    @Retention(RetentionPolicy.RUNTIME)
    @Constraint(validatedBy = {})
    public @interface CustomConstraint {
    
        String message() default "";
        Class[] groups() default {};
        Class[] payload() default {};
    
        @OverridesAttribute(constraint = Pattern.class, name = "message")
        String patternMessage() default "";
    }