5

I have two classes where one is nested in the other. I'd like to deserialize and validate incoming JSON against this structure of mine. I have a javax Validator obtained by

Validator validator = Validation.buildDefaultValidatorFactory().getValidator();

And later I do:

Set<ConstraintViolation<Object>> validate = validator.validate(obj);

Where obj is my deserialized JSON. My nested classes are

class Foo {

  @Min(5)
  Integer a;

  List<Bar> bars;

  static class Bar {
    @Min(2)
    Integer b;
  }
}

I know that, unless I specify @Valid annotation on my List<Bar> bars field, the validator is not going to make a recursive validation. However, I'd like the default behavior of the validator to be recursive.

Is there a way to do that when getting the instance of the Validator?

LIvanov
  • 1,126
  • 12
  • 30

1 Answers1

0

You can achieve this with @Valid, which allows for "validation cascading" (see this example)

class Foo {

  @Min(5)
  Integer a;

  @Valid
  List<Bar> bars;

  static class Bar {
    @Min(2)
    Integer b;
  }
}
Joseph Cass
  • 119
  • 1
  • 3