Enterprise Library Validation - If NOT NULL validation failed then should not check other validations

Topics: Enterprise Library Core, Exception Handling Application Block, General discussion
Jun 30, 2014 at 6:14 PM
Validation - If NOT NULL validation failed then should not check other validations for that property.

e.g. Property name X and having 2 validations for this property say, NOT NULL and Length validations. If property is coming as NULL and should not execute Length validation for this science its already NULL.

Can handle this scenario in code... but should have some property by fault.

Thanks.
Jul 2, 2014 at 7:32 AM
Edited Jul 6, 2014 at 7:05 PM
I think you want to use XML configuration. The validation should return a single validation result if the property is null. Also, the validation should return a single validation result if the property is not null but triggers a length validation. Correct me if I've misinterpreted.

You can do the above using composite validators in configuration:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="validation" type="Microsoft.Practices.EnterpriseLibrary.Validation.Configuration.ValidationSettings, Microsoft.Practices.EnterpriseLibrary.Validation, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
  </configSections>
  <validation>
    <type name="Validation.Person" defaultRuleset="Validation Ruleset"
      assemblyName="Validation, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
      <ruleset name="Validation Ruleset">
        <properties>
          <property name="Name">
            <validator type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.AndCompositeValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
              name="And Composite Validator">
              <validator type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.NotNullValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                name="Not Null Validator" />
              <validator type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.OrCompositeValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                messageTemplate="Name must be less than 10 characters." name="Or Composite Validator">
                <validator type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.NotNullValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                  negated="true" name="Not Null Validator" />
                <validator type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.StringLengthValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                  upperBound="10" name="String Length Validator" />
              </validator>
            </validator>
          </property>
        </properties>
      </ruleset>
    </type>
  </validation>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
</configuration>
    public class Person
    {
        public string Name { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            ValidationFactory.SetDefaultConfigurationValidatorFactory(
              new SystemConfigurationSource(false));

            Validator<Person> validator = ValidationFactory.CreateValidator<Person>();
            ValidationResults vrs = validator.Validate(new Person() { Name = "12345678901234567890" });

            foreach(var vr in vrs)
            {
                Console.WriteLine(vr.Message);
            }
        }
    }

The approach is to use an And validator for the first check (not null). Then the second check uses an Or validator in combination with a negated version of the first validator (not null) and the second validator (string length). It is messy and could get quite cumbersome very quickly but it does work. Also, set the message on the Or validator to avoid checking nested validation results.

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to
Marked as answer by randylevy on 7/6/2014 at 11:05 AM
Jul 2, 2014 at 4:58 PM
Thanks Randy... this is what which I was looking for. Thanks for all your efforts.