Project Description

Generic Validation.NET is a flexible lightweight validation library for .NET, that can be used by any .NET project: ASP.NET Web Forms, ASP.NET MVC, but also Windows Forms.
It is entity centric, with tagging, fluent syntax, and maximum flexibility of the error messages.
  • It works with ASP.NET Web Forms and MVC
  • It works with Windows Forms
  • It works with LINQ to SQL
  • It works with Entity Framework
  • It works with TypedDataSets
  • It works with anything.
  • It doesn't require any modifications of the generated code.

Key Features

  • It is outragously easy to define object validations.
  • Tagging.
  • It is very flexible, easily customizable and extended with various features, so any other developer would customize/enrich it to meet his needs.
  • Real support for Globalization and Localization through Resources.
  • Real support for validation inheritance, following class inheritance.
  • Maximum flexibility for Validation Messages, with the option to include validated entity members (values) into validation messages.

How does it work?

The GenericValidation framework is entity centric, therefore you can only use it to validate complex objects (classes).

To validate a complex object, just inherit from the Validation<T> class and use the constructor to register the validation rules:
public class UserValidation : Validation<User>
{
    public UserValidation()
    {
        Member(p => p.Age)
            .IsLowerThan(10)
            .Always(); //tagging with "Always"

        Member(p => p.UserName)
            .IsRequired()
            .OnInsert(); //tagging with "Insert"

        Member(p => p.UserName)
            .IsLengthLowerThan(20)
            .Always(); //tagging with "Always"

        IsValid(p => p.Age != null)
            .Always(); // tagging with "Always"

        IsValid(p => p.UserName != p.FullName) //custom validation
            //setting an error message that contains object members, evaluated when performing the validation
            .Message("User Name {0} and Full Name {1} must be different.", p => p.UserName, p => p.UserName)
            .Always(); //tagging with "Always"
    }
}

having the User class:
    public class User
    {
        public string UserName { get; set; }
        public string FullName { get; set; }
        public bool IsEnabled { get; set; }
        public string Email { get; set; }
        public int Age { get; set; }
    }

How to Validate?

//the ValidationFactory - it resolves the Validation<T> definitions
var factory = new ValidationFactory();

//loading Validation definitions from the current Assembly (or any other assembly)
factory.LoadFromAssembly(Assembly.GetExecutingAssembly());

//the ValidationManager - used to perform object validation
var manager = new ValidationManager(factory);

//validating the object, against a set of tags (ex: "Insert", "Always")
var result = manager.Validate(employee, ValidationSettings.InsertTag, ValidationSettings.AlwaysTag);

//displaying the error messages
result.ErrorInfo.ToList().ForEach(p => Console.WriteLine(p));

Last edited Jun 24, 2010 at 8:16 AM by adyi04, version 10