Validate collection size in VAB

Topics: General discussion, Validation Application Block
Sep 28, 2010 at 8:57 AM

Hi guys, I've been using VAB since 4.1 and it's great, but I have the following problem.

 

I have an object A that has a property B (which is a List of elements of type C):

 

Object A

|---- Object B (List<C>) 

       |---- Object C

 

I want to validate that:

  • Collection is not null
  • Collection size is greater than 0
  • Validate each element of the List 

 

¿How can I check that collection size is greater than x? As far as I've read, the CollectionValidator only checks the collection type and invokes validation of each element of the List...

 

Thanks in advance!

Sep 28, 2010 at 9:14 AM

The easiest way would be to create another property of type integer returning the size of the collection and assigning a rangevalidator to it.

 

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Sep 28, 2010 at 9:27 AM

Isn't there a "native" way of checking it?

 

Object A is an input object in a webservice method and it would be a little "dirty" to fill two parameters (object B and size property) to check it... 

 

I suppose that that property could be read-only though...

Sep 28, 2010 at 9:32 AM

Sorry but the only other workaround I could think of is creating a custom validator.

 

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Sep 28, 2010 at 9:35 AM

Thank you Sarah.

Sep 28, 2010 at 8:54 PM

Or you can always write a self validation method.