Error is being thrown from EntLib Validation block for all nullable value types e.g. Decimal? after upgrading to EntLib 5.0 from EntLib 4.0 validation block

May 7, 2013 at 12:55 PM
"Value to validate is null but expected an instance of the non-reference type System.Nullable`1[[System.Decimal, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]."

In Class validator.Generic.cs One extra check is being introduced as below
// null values need to be avoided when checking for type compliance for value types

if (objectToValidate == null)
    {
        if (typeof(T).IsValueType)
        {
            string message
                = string.Format(
                    CultureInfo.CurrentCulture,
                    Resources.ExceptionValidatingNullOnValueType,
                    typeof(T).FullName);
            LogValidationResult(validationResults, message, currentTarget, key);
            return;
        }
    }
validations are defined in config level as below:
<property name="DonationPercentage">
        <validator messageTemplate="" messageTemplateResourceName="" messageTemplateResourceType=""
        tag="" type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.AndCompositeValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        name="And Composite Validator">

                        <validator lowerBoundType="Inclusive" upperBound="1000.00000" upperBoundType="Inclusive" lowerBound="-1000.00000"
                        messageTemplate="minMaxLengthValidatorMessage,DonationPercentage,Fund,-1000.00000,1000.00000"
                        type="BI.CMW.Validator.CustomValidators.RangeIgnoreNullValidator, BI.CMW.Validator,Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
                        name="Range Validator" />

                    <validator pattern="^[-+]?\d{0,4}(\.\d{1,5})?$" options="None"
                    messageTemplate="decimalValidatorMessage,DonationPercentage,Fund,9,5"
                    tag="" type="BI.CMW.Validator.CustomValidators.DecimalValidator,BI.CMW.Validator,Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
                    name="DecimalValidator" />
Since one can have nullable values, how to handle this scenario.
May 12, 2013 at 3:36 AM
It looks like you are using custom validation some I'm not sure what those validators are doing. For an out of the box approach to validate a Nullabe<Decimal> property using configuration then you could use something like this to validate a range while not allowing nulls:
    public class ItemProperties
    {
        public decimal? MeltingPoint
        {
            get;
            set;
        }
    }
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="validation" type="Microsoft.Practices.EnterpriseLibrary.Validation.Configuration.ValidationSettings, Microsoft.Practices.EnterpriseLibrary.Validation, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
    </configSections>
    <validation>
        <type name="ItemProperties"
            defaultRuleset="Validation Ruleset" assemblyName="MyValidation, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
            <ruleset name="Validation Ruleset">
                <properties>
                    <property name="MeltingPoint">
                        <validator type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.RangeValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                            culture="en-US" lowerBound="0.00" lowerBoundType="Inclusive"
                            upperBound="1000.00" upperBoundType="Inclusive" name="Range Validator" />
                    </property>
                </properties>
            </ruleset>
        </type>
    </validation>
</configuration>
If you wanted to allow nulls then you could use an Or Composite Validator:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="validation" type="Microsoft.Practices.EnterpriseLibrary.Validation.Configuration.ValidationSettings, Microsoft.Practices.EnterpriseLibrary.Validation, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
    </configSections>
    <validation>
        <type name="ItemProperties"
            defaultRuleset="Validation Ruleset" assemblyName="MyValidation, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
            <ruleset name="Validation Ruleset">
                <properties>
                    <property name="MeltingPoint">
                        <validator type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.OrCompositeValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                            name="Or Composite Validator">
                            <validator type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.NotNullValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                                negated="true" name="Not Null Validator" />
                            <validator type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.RangeValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                                culture="en-US" lowerBound="0.00" lowerBoundType="Inclusive"
                                upperBound="1000.00" upperBoundType="Inclusive" name="Range Validator" />
                        </validator>
                    </property>
                </properties>
            </ruleset>
        </type>
    </validation>
</configuration>

If you need to have other validators as well then you can nest composite Or Validators within an And Composite Validator as in this Stack Overflow answer: http://stackoverflow.com/questions/13937482/enterprise-library-validation-allow-nulls-but-verify-other-properties.

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to