0

I have been trying to execute my validator annotations in specific order using @GroupSequence. The annotations are working according to the specified order but in case the first in priority validator fails, the next in order annotations are not being executed, even for class elements which are only annotated with the latter lower priority validators.

I have tried looking into all other answers including How to validate field level constraint before class level constraint? and Control validation annotations order? but my issue is not covered anywhere.

OrderChecks.class

@GroupSequence({NotNull.class, LengthConstraint.class})
public interface OrderChecks {
}

Pojo.class

@NotNull(groups = NotNull.class)
@LengthConstraint(message = "too long string", groups = 
    LengthConstraint.class)
private String a;


@LengthConstraint(message = "too long string", groups = 
    LengthConstraint.class)
private String b;

Because NotNull.class has higher priority, only it is executed for String a which is fine but @LengthConstraint is not getting executed for String b.

Is there any way to set it such that the ordercheck is done on each class element separately ?

1 Answers1

0

In the case of group sequences:

  • the sequences are executed in order
  • all the constraints of a given sequence are executed
  • as soon as there is a violation for a sequence, it throws a violation and doesn't execute the rest of the sequences.

Keep in mind that sequences were designed to allow things such as , .

That being said, in your case, what you should do is:

  • do not use group sequences
  • use the @Size constraint which allows null values

In general, when you design a new constraint, it should return true for null values.

Guillaume Smet
  • 9,921
  • 22
  • 29
  • Thanks for the answer, but actually this is just a dummy project I made to under group sequences, the actual code is quite complex, that's why I wanted to know if there was some way to make group sequence work my way – Hardik Garg Sep 13 '19 at 11:50
  • As explained, there is not. Group sequences behave like this per design. – Guillaume Smet Sep 13 '19 at 13:40