Project Description

NValid is a light-weight C# validation library for .NET. Easily extended with custom validation logic for your project!

Moved To:

http://validity.codeplex.com

High-Level Features

Rules

NValid supports 20 standard "rules" out of the box and is extendable to support countless more!
  • Any -> MustHave, MustNotHave, IsInDomain, IsNotInDomain
  • Object -> IsNotNull, IsSameAs
  • IComparer -> IsLessThan, IsLessThanOrEqualTo, IsEqualTo, IsNotEqualTo, IsGreaterThan, IsGreaterThanOrEqualTo (Supports Nullable<T> where T : IComparer)
  • String -> StartsWith, Contains, EndsWith, IsNotEmpty, IsNotWhiteSpace, MatchesPattern, HasMinimumLenghOf, HasMaximumLengthOf

Contexts

NValid chains together rule contexts to help simplify defining rules through IntelleSense (no invalid .If(~).If(~) chains)
  • Message -> ReplacingMethodWith
  • Conditional -> If, Unless
  • Comparable-> WhenUsingComparer

Example

Sample Business Object
  public class Account
  {
    public String Name { get; set; }
    public Int64 Number { get; set; }
    public DateTime Opened { get; set; }
    public DateTime? Closed { get; set; }
  }


Sample Validator
    public class AccountValidator
      : ValidatorBase<Account>
    {
      public AccountValidator()
      {
        DefineRulesFor(acct => acct.Name)
          .IsNotWhiteSpace()
          .HasMaximumLengthOf(30)
          .IsNotEqualTo("Reserved").WhenUsingComparer(StringComparer.OrdinalIgnoreCase);
        DefineRulesFor(acct => acct.Number)
          .IsGreaterThan(0);
        DefineRulesFor(acct => acct.Opened)
          .IsLessThanOrEqualTo(() => DateTime.Today)
          .IsGreaterThanOrEqualTo(acct => acct.Closed.Value).If(accnt => accnt.Closed.HasValue).ReplacingMessageWith("Some Custom Message");
        DefineRulesFor(acct => acct.Closed)
          .IsGreaterThanOrEqualTo(acct => acct.Opened).If(acct => acct.Closed.HasValue);
      }
    }


Sample Rule Constraint Extension
  public static class IsExactlyConstraintExtension
  {
    public static IValidationContext<TObject, String> IsExactly<TObject>(this IValidationContext<TObject, String> context, Int32 length)
    {
      return context.MustHave((obj, val) => (val ?? String.Empty).Length == length);
    }
  }

What's Next

  • NDoc
  • RuleViolation Message Formatters
  • Control Binders

Last edited Aug 1, 2010 at 2:56 PM by NoRemorse, version 13