JSR303 validation for collection of shared objects

Is it possible to check each item in the collection based on one or more delegate verification rules? For instance:

@EachElement({@Min(1), @Max(12)}) private Set<Integer> monthNumbers; 
+6
source share
2 answers

Take a look at validator-collection , using this library it’s very easy to use any Constraint annotation in a collection of simple types.

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

Also see fooobar.com/questions/153966 / ....

+3
source

Have a look at this answer: Hibernate Validating collections of primitives . This describes a solution that works for you, but it is rather complicated. A simpler solution would be to implement a wrapper class for Integer and declare @Min and @Max in this class. Than you can use

 @Valid private Set<MyIntegerWrapper> monthNumbers; 

class MyIntegerWrapper:

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

Here you will find documentation for @Valid : Object Graphs

0
source

Source: https://habr.com/ru/post/895837/


All Articles