[Validation] Configuration and IgnoreNullsAttribute

Topics: Pre-release discussions, Validation Application Block
Mar 26, 2010 at 1:37 AM

Hi

I have a suggestion with regards to configuring the Validation Application Block.

If I configure a validator for a member of a type in a configuration file (e.g., the RegexValidator for the SocialSecurityNumber member of the Customer type) and the type's member is decorated with the IgnoreNullsAttribute, the attribute is ignored.

Is it possible that the IgnoreNullsAttribute can be checked when validators are created from configuration?

Regards,

Chris

Mar 26, 2010 at 10:48 AM

For what I know, if the scenario you have is if a single Property has both config validators and also an attribute validator each of them acts/behave independently. So it doesn't mean that whenever the IgnoreNullAttribute is met by the property it will also ignore the config validator. The 2 validators have no relationship and the config validator will just continue to do its validation regardless of what happen in the attribute config. HTH

~ginkapitan

Mar 29, 2010 at 1:20 AM

Hi ginkapitan

I should have expressed this a different way. :-)

If I have configured a validator for a member of a type in a configuration file and the member's value is null, then the validator isn't evaluated (as per the attribute model).

Rather than using an attribute to ignore nulls, maybe it can be expressed in the configuration file using an ignoreNulls attribute of the property element. For example:

<type name="MyNamespace.MyClass" assemblyName="MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" defaultRuleset="DefaultRuleset">
    <ruleset name="DefaultRuleset">
        <properties>
            <property name="MyProperty" ignoreNulls="true">
                <validator name="MyPropertyRegexValidator" type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.RegexValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=5.0.315.0, Culture=neutral, PublicKeyToken=null" pattern="\d{2}-\d{3}-d{3}-\d{3}" />
            </property>
        </properties>
    </ruleset>
</type>

Regards,

Chris

 

Mar 29, 2010 at 4:09 AM

Yeah the initial question was really a bit confusing :)

You really cannot explicitly add an ignoreNulls Attribute to a Property in your Validation configuration just like your sample. Which is really very cool if this can be done that easy.

Anyway, I do think there’s a lot of ways to achieve a desired validation by using custom validation or any other approach. Though, given that in the scenario at hand we want to have it fully in the configuration, here is something that works for me that I hope would also help you on your end :)

 My sample below also deals with a property containing a Regex validator at the same time Ignore it when the property value is null. I have used an Or Composite Validator to achieve this.

              <properties>
                    <property name="Property1">
                        <validator type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.OrCompositeValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=5.0.315.0, Culture=neutral,                    PublicKeyToken=31bf3856ad364e35"
                            name="Or Composite Validator">
                            <validator type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.RegexValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=5.0.315.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                                pattern="^\d\d\d-\d\d-\d\d\d\d$" name="Regular Expression Validator" />
                            <validator type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.NotNullValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=5.0.315.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                                negated="true" name="Not Null Validator" />
                        </validator>
                    </property>
                </properties>

HTH,

~ginkapitan