3

I have a class-level annotation which I use to check someField before checking another field with type Bar

@SomeClassValidation
class Foo {
    String someField;

    @Valid
    Bar someBar;
}

the implementation for ConstraintValidator<SomeClassValidation, Foo> looks something like:

if(someField == someExpectedValue) {
    return someBar != null;
}

When the initial checks done with @SomeClassValidation pass, I also need to validate the fields inside the Bar class which looks something like:

class Bar {
    @NotEmpty
    @Size(min=2, max=2)
    String fieldA;

    @NotEmpty
    @Size(min=3, max=10)
    String fieldB;
}

Following the answer in another similar question, I figured I need to use validation groupings where BarValidationGroup is a marker interface I used for the fields in Bar class.

@SomeClassValidation
@GroupSequence({Foo.class, BarValidationGroup.class})
class Foo {
    String someField;

    @Valid
    Bar someBar;
}

class Bar {
    @NotEmpty
    @Size(min=2, max=2, groups = BarValidationGroup.class)
    String fieldA;

    @NotEmpty
    @Size(min=3, max=10, groups = BarValidationGroup.class)
    String fieldB;
}

The first level validation (@SomeClassValidation) triggers but the validations for Bar doesn't. What am I doing wrong?

niverg
  • 31
  • 2

0 Answers0