6

Is it possible to validate each element of a collection, based one or more delegate validation rules? For example:

@EachElement({@Min(1), @Max(12)})
private Set<Integer> monthNumbers;
Jeroen
  • 527
  • 2
  • 7
  • 19

2 Answers2

3

Take a look at validator-collection, with this library is very easy to use any Constraint Annotation on a collection of simple types.

@EachMin(1)
@EachMax(12)
private Set<Integer> monthNumbers;

Also see https://stackoverflow.com/a/16023061/2217862.

Community
  • 1
  • 1
Jakub Jirutka
  • 10,269
  • 4
  • 42
  • 35
0

Have a look at this answer: Hibernate Validation of Collections of Primitives. That describes a solution which work for you but it is pretty complex. A simpler solution might be to implement a wrapper class for your Integer and declare @Min and @Max in that class. Than you can use

@Valid
private Set<MyIntegerWrapper> monthNumbers;

class MyIntegerWrapper:

class MyIntegerWrapper
{
   @Min(1)
   @Max(12)
   Integer myInteger;
}

Here you find some documentation for @Valid: Object graphs

Community
  • 1
  • 1
Kai
  • 38,985
  • 14
  • 88
  • 103