2

I have create a validation annotation to validate the content of a string. The definition of my annotation looks like this:

@NotNull
@Size(min = 2, max = 128, groups = First.class)
@Pattern(regexp = "^(?!WP_([0-9])+$)[A-Z_][A-Z0-9_-]+", groups = Second.class)
@Target({FIELD, ANNOTATION_TYPE})
@Retention(RUNTIME)
@Documented
@Constraint(validatedBy = {})
@GroupSequence(value={First.class, Second.class})
public @interface ValidIntentName {
    String message() default "";

    Class<?>[] groups() default {};

    Class<? extends Payload>[] payload() default {};
}

My problem is that i cannot order the @Size and @Pattern execution. I've tried a couple of things but no luck so far.

Basically I need @Size to be executed before @Pattern

First and Second interfaces are defined. There are no errors during compilation is just that during run-time @Patter is executed before @Size.

Any idea what I'm doing wrong?

I'm running Java 8.

Thanks

Lahiru Gamage
  • 849
  • 5
  • 14
  • 27
Yonoss
  • 1,242
  • 5
  • 25
  • 41
  • Possible duplicate of https://stackoverflow.com/questions/5571231/control-validation-annotations-order – Afridi Apr 26 '18 at 13:19
  • I don't think `javax.validation` supports such a feature (`@GroupSequence` probably wasn't meant for custom annotations at all). You'll need a custom `ConstraintValidator`. Why do you need to order the constraints? If performance is of concern, why not merge them into one by using `{1,127}` instead of `+`? – crizzis Apr 26 '18 at 13:27
  • I need that to be executed in the order specified (for some busies reason ) and return different exception once they caught something. For the time being I've made the pattern size agnostic so i'm fine. But it will be nice to know how to make them validate in a specified order. – Yonoss Apr 26 '18 at 15:25

0 Answers0