Java – JSR 303 – validate multiple internal lists

I am using JSR 303 and have written many comments, so I am familiar with the process of obtaining custom constraints

I've just had a problem. I'm not sure if I can solve it gracefully The items here are for illustration! So I have a grandparent object with a list of children Each child has its own list of children (apparently grandsons of grandparents) You can use @ size to constrain the size of this list But I need to limit the total number of grandparents' children so that if I test my grandparents' example, their children will be limited to 50 grandchildren Strange examples, I know: -)

I started this method by creating a method in my grandparents, which only calculates all elements in the sublist of the grandparent sublist and adds a simple @ size annotation to it It feels a little ugly - this method is added just for some validation Isn't that the part that JSR should solve?

Or – create a custom comment like this:

@CustomListSize(listName="children.children",min=0,max=50)
Grandparent
    --> List<Child> children
        @Size(min=0,max=5)
        -->List<Child> children
    --> List<Object> list1
        -->List<Object> list2

I can obviously use reflection / propertydescriptor to get it and check the minimum / maximum value This sounds good, or I have a better method in JSR 303, which I missed This works, but requires more code, but is still not ideal

Finally, what if I want to do the same check for the number of entries in List2? I can't repeat customlistsize, so how do I do this - have an internal list?

thank you

Chris

Solution

I think a custom validator for your grandparents will be a good way

@InnerListSize(lists = {"list1",list2"},max=50)
class GrandParent{
   List list1;
   List list2;

}

Reflection can be used to get a list Once we have them, it's easy to calculate the overall size The validator class is not particularly complex

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>