How can I prevent Value Validators from failing if a null value is passed?

Topics: Validation Application Block
May 14, 2010 at 6:32 PM
Edited May 14, 2010 at 6:33 PM

I want to have value validators fail validation only if a value is passed.  This is especially important in optional fields, where validation should only be performed if a value is passed.  Unfortunately, this is not configurable, as all of its implementations run the logic:

 

bool logError = false;
            bool isObjectToValidateNull = objectToValidate == null;

            logError = !isObjectToValidateNull && 

// do validation logic here

            if (isObjectToValidateNull || (logError != Negated))
            {
                LogValidationResult(validationResults,
                    GetMessage(objectToValidate, key),
                    currentTarget,
                    key);
            }

It would be nice if there was an option to have it only validate if object to validate is not null, and not to fail if that's the case.

 

Any recommendations on how to do this with Behavior Interception?

May 17, 2010 at 12:09 AM

If you're using attributes, you can use the IgnoreNulls attribute.  But if you're specifying your validation, you need to do it by using an OrCompositeValidator together with a negated NotNullValidator and the other validator that you want.

 

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

May 21, 2010 at 7:42 PM

I'm not sure that will correct the issue.  See this unanswered discussion for more details on this possible scenario/bug:  http://entlib.codeplex.com/Thread/View.aspx?ThreadId=211694

 

May 24, 2010 at 12:05 AM

The code in that thread didn't use the IgnoreNulls attribute or an OrCompositeValidator.  I've also verified that entlibrocks code does repro the scenario but if the case is on validating an object's property, the error message provided for the assigned validator is used as the validation result error. 

 

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

May 26, 2010 at 8:47 PM
AvanadeSupport wrote:

If you're using attributes, you can use the IgnoreNulls attribute.  But if you're specifying your validation, you need to do it by using an OrCompositeValidator together with a negated NotNullValidator and the other validator that you want.

 

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

Sarah, thanks for that workaround as far as using the OrCompositeValidator with the negated NotNullValidator.  This worked and I would have never come up with that idea! 

This is, however, a workaround.  It would be nice if in the configuration you could set IgnoreNulls.  Enterprise library 5.5? 6.0?

May 27, 2010 at 12:00 AM

The IgnoreNulls attribute actually maps to an Or-Composition of the validator you specified and a negated NotNullValidator.  But if you really want a separate validator for this in the config, you can log it in the Issue Tracker.  The more votes it has, the more chance it has to be included in the next versions.

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

May 27, 2010 at 4:42 PM

Ok, makes sense.  For now I'll use the Or-Composite validator.  Thanks again for the pointer.