Adding Custom Rule using Configuration File.

Topics: Developer Forum, User Forum
Sep 23, 2008 at 8:24 AM
Edited Sep 23, 2008 at 10:05 AM
Hi,
       I need to include the method that will be used to validate my object in the XML configuration file since I am using the XML configuration file for manageability.Below is something similar
to my case.Please tell me how to include the service I am using to validate the object in the XML File.(Check below code).(Please note that there are 2 dlls in use)

Thanks in Advance.

Sample.BusinessObject .dll

******************************************************************
Person.cs
******************************************************************
namespace Sample.BusinessObject
{            
public class Person
{
public enum CardType
{
Visa,
MasterCard,
Amex
}
        
public CardType? CardType
{
get;
set;
}
        
public string CreditCardNumber
{
get;
set;
}
}
}
******************************************************************

Sample.BusinessObject .ValidationServices.dll

******************************************************************
PersonValidations.cs
******************************************************************
namespace Sample.BusinessObject.ValidationServices
{
    public class PersonValidations
    
       public static void CheckValidCreditCard(object sender, CustomValidationEventArgs e)
        {
            Person person = (Person)e.TargetObjectValue;
            CardType? cardType = person.CardType;
            string cardNumber = person.CreditCardNumber;
            if (cardNumber == null || cardType == null)
            {
                e.IsValid = false;
            }
            else
            {
                switch (cardType.Value)
                {
                    case ExampleLibraryCSharp.CardType.Visa:
                    case ExampleLibraryCSharp.CardType.MasterCard:
                        {
                            e.IsValid = (cardNumber.Length == 16);
                            break;
                        }
                    case ExampleLibraryCSharp.CardType.Amex:
                        {
                            e.IsValid = (cardNumber.Length == 15);
                            break;
                        }
                    default:
                        {
                            throw new ArgumentException("Invalid CardType");
                        }
                }
            }
        }

}
}
******************************************************************

******************************************************************
Person.validation.xml
******************************************************************
<?xml version="1.0" encoding="utf-8" ?>
<validationMapping xmlns="urn:validationFramework-validationDefinition-1.5">
  <class typeName="Sample.BusinessObject.Person,Sample.BusinessObject">    
   
   <property name="CreditCardNumber">     
      <rule errorMessage="Please enter a CreditCardNumber."
            typeName="RequiredStringRule"
            ruleSet="default-form"
            />           
    </property>

    ???????????????????
    
  </class>
</validationMapping>

Sep 23, 2008 at 11:26 AM
There is an example usage here
http://www.codeplex.com/ValidationFramework/SourceControl/FileView.aspx?itemId=432003&changeSetId=40466

but more specifically here is some xml that will work for your example.

<?xml version="1.0" encoding="utf-8" ?>
<validationMapping xmlns="urn:validationFramework-validationDefinition-1.5">
  <class typeName="Sample.BusinessObject.Person,Sample.BusinessObject">

    <property name="CreditCardNumber">
      <rule errorMessage="Please enter a CreditCardNumber."
            typeName="RequiredStringRule"
            ruleSet="default-form"
            />
      <rule errorMessage="Credit card is invalid."
            typeName="CustomRule"
            ruleSet="default-form"
            ruleInterpretation="Validate Credit card."
                    validationMethod="CheckValidCreditCard"
                    validationTypeName="Sample.BusinessObject.PersonValidations, Sample.BusinessObject"
            />
    </property>
  </class>
</validationMapping>

and here is a unit test

using NUnit.Framework;
using ValidationFramework;
using ValidationFramework.Configuration;

namespace Sample.BusinessObject
{
    [TestFixture]
    public class ExampleUsage
    {

        [Test]
        public void RunCustomRuleConfigReader()
        {
            ConfigurationService.AddAssembly(GetType().Assembly);
            var person = new Person();
            var propertyValidationManager = new PropertyValidationManager(person) {RuleSet = "default-form"};
            propertyValidationManager.ValidateAll();
            Assert.IsFalse(propertyValidationManager.IsValid);
            person.CreditCardNumber = "123456789012345";
            person.CardType = CardType.Amex;
            propertyValidationManager.ValidateAll();
            Assert.IsTrue(propertyValidationManager.IsValid);
        }
    }
}

for this to work Person.validation.xml will need to be an embedded resource.

Hope this helps

Sep 24, 2008 at 6:49 AM
Thanks a lot for you reply and follow up,I really appreciate it.I will try the above code you mention and check.

Thanks again.