Validation on null instance

Topics: Validation Application Block
Dec 1, 2010 at 8:07 AM

Hello.

 

we have a situation where we use the "ValidationBehaviour" (with WCF). one of the service method accepts an argument with "HasSelfValidation" attribute at class level. the value passed is null.

client validation fails with an error "The reference provided to the self validation method is either null or references an instance of a non-compatible type."

"IgnoreNull" attribute can only be specified on property level and not class level - leaving us with this undesired behaviour.

 

please advice

 

Dec 2, 2010 at 3:50 AM

That is the behavior by design.  A possible workaround would be to create a custom validator similar to what the self validation method does. 

 

Sarah Urmeneta
Global Technologies & Solutions
Avanade, Inc.
entlib.support@avanade.com

Dec 2, 2010 at 6:25 AM

I don't understand why, and could not find any reference about it in the documentation. if an object is null what's there to validate?

Even if i set a breakpoint in the [SelfValidation] method, it never gets hit.

 

ohh well, I'll appreciate additional info

Dec 2, 2010 at 6:45 AM

It wasn't mentioned in the documentation, I just looked at the source code and found that they deliberately log a validation failure if the object to validate is null.

Maybe the decision was due to the assumption that since you have validation rules set for that object type, then that rules needed to be follow and so passing an object that is null automatically violates the validation rules defined on the type. 

 

Sarah Urmeneta
Global Technologies & Solutions
Avanade, Inc.
entlib.support@avanade.com

Dec 2, 2010 at 6:50 AM

I can see where you going, but i can understand both sides here (mine more than yours :-)).

at least they can mention it in the documentation

 

thank you for the reply

 

Dec 2, 2010 at 6:53 AM

Personally, I would prefer if it's something that can be configured in the validator, like there's a property which will allow you to say to continue validating if null or not.  :)

 

Sarah Urmeneta
Global Technologies & Solutions
Avanade, Inc.
entlib.support@avanade.com

Dec 2, 2010 at 6:55 AM
Edited Dec 2, 2010 at 6:58 AM
That was my initial approach - setting [IgnoreNull] attribute at class level deceleration of my entity