Project Description
Validate is a collection of extension methods that let you add validations to any object and display validations in a user/developer friendly format. Its an extremely light weight validation framework with a zero learning curve.

Usage ( or go to Quickstart ):

    var person = new Person {Name = "Some Person", Age= 26, Sex = "Male"};
    // Validate this object
    var validator = person.Validate()
                                     .IsNotNull(p=> p.name)
                                     .IsGreaterThan(p=> p.Age, 18)
                                     .IsEqualTo(p => p.Sex, "Male");
    Assert.IsTrue(validator.IsValid);
    var person = new Person {Name = "Some Person", IncomePA = "16000" , DateOfBirth = (DateTime) "07/04/1993" , HomeAddress = new Address {Line1 = "Line 1"..... Country = "UK"}};
    // ValidationOptions allow you to control validation behaviour like whether to stop validating on first error, or whether to throw exceptions on validation errors.
    var validator = person.Validate(new ValidationOptions{StopOnFirstError = false})
                          .IsInBetween(p => p.IncomePA, 30000, 45000)
                          .IsOneOf(p => p.HomeAddress.Country, "USA", "Canada")
                          .IsLesserThan(p => p.DateOfBirth, (DateTime) "01/01/1992", "You should be born before 1992 to participate in this survey."); // Override the auto generated validation message
    Assert.IsFalse(validator.IsValid);
    Assert.AreEqual(validator.Errors[0].Message, "Person.IncomePA should be between 30000 and 40000.");
    Assert.AreEqual(validator.Errors[1].Message, "Address.Country should be one of { USA | Canada }.");
    Assert.AreEqual(validator.Errors[2].Message, "You should be born before 1992 to participate in this survey.");
    // The message property of the validation errors shows the validation failure message.
    Assert.AreEqual(validator.Errors[0].Cause, "{ Person.IncomePA with value 16000 is not between 30000 and 40000}");
    // The cause property of the validation error shows the cause of the error.
    // Instead of checking for validation errors, it is also possible to raise custom exceptions by setting the ThrowValidationExceptionOnValidationError property on ValidationOptions.
    // Automatic message generation templates can be overridden (through code) to generate custom validation messages automatically
     // Validation rules can be aggregated into validations. Such validations are called *Named Validations*.
     var validation = new Validation<Person>("My_Person_Validation")
                             .Setup(v => v.IsNotNull(p => p.Name, "Name is mandatory.")
                                          .IsGreaterThan(p => p.Age, 18, "Age must be nore than 18.")
                                   );
     // This validation can then be applied later
     var personToValidate = new Person {Name = "Person's Name", Age = 25};
     var validator = validation.RunAgainst(personToValidate);
     Assert.IsTrue(validator.IsValid);
     // It can also be applied using .ValidateUsing("VALIDATION_NAME") extension
     validator = personToValidate.ValidateUsing("My_Person_Validation");
     Assert.IsTrue(validator.IsValid);

The following validations are supported:
  • IsNotNull
  • IsNull
  • IsNullOrEmpty
  • IsNotNullOrEmpty
  • IsEqualTo
  • IsNotEqualTo
  • IsGreaterThan
  • IsLessThan
  • IsTrue
  • IsFalse
  • Or
  • And
  • IfThen
  • IfNotThen
  • IsBetween
  • IsNotBetween
  • RegexMatches
  • Contains
  • IsOneOf
  • PassesSavedValidation

Documentation


If you would like to see a validation added, please raise a feature request for it.

Please take a look at the included test project for usage, and the Project Roadmap.

Please ping me (ashish dot sharma dot ait at gmail dot com) if you want to join this project or have any suggestions.

A git clone is maintained at git://github.com/ashishs/Validate.git

Last edited Jan 12, 2011 at 10:36 AM by ashishsharmaait, version 30