Samurai Validation Framework

Samurai Validation Framework is very easy to use and extend.

Do you enjoy writing validation logic using a fluent API? Samurai Validation Framework has an easy to use fluent interface.

Do you like to define your validation rules through the use of attributes on your objects? We support that.

Do you prefer to define your rules in a more encapsulated, modular manner? We support that through the creation of custom validators that associate your rules to your domain objects through a simple API.

What about validation in WPF? It's been standard to implement IDataErrorInfo on your model objects and allow single property validation so WPF can call upon the validation automatically. Many developers enjoy some of the automation offered by this mechanism, but feel it "dirties" their domain objects somewhat. We attempt to alleviate some of this for you by offloading the application of rules to the aforementioned custom validators. Additionally, you can not only validate an entire domain object in one shot, but you can specifically validate individual properties for use in this scenario.

==How about a few examples?==

Example 1: Applying validation rules in a custom validator using the new Fluent API
-This encapsulates all of your validation logic for a given type
in one class, avoiding the scattering of rule applications by attribute.


public class CustomerValidator : Validator<Customer>
  public CustomerValidator(Customer targetObject) : base(targetObject)
    For(x => c.EmailAddress).Ensure().IsEmailAddress();
    For(x => c.Nickname).Ensure().IsLongerThan(3);
    For(x => c.FirstName).Ensure().IsShorterThan(30);
    For(x => c.WebSite).Ensure().IsUrl();
    For(x => c.BirthDate).Ensure().IsEarlierThan(new DateTime(1980,1,1));


Example 2: Applying rules through Attributes


public class Customer
    [StringLengthRule(MaximumAllowed = "20", InvalidMessage = "First name exceeds 20 characters")]
    public string FirstName { get; set; }

    [StringLengthRule(MaximumAllowed = "30", InvalidMessage = "Last name exceeds 30 characters")]
    public string LastName { get; set; }

    [MustIncludeTextRule(SearchText = "Tony", InvalidMessage = "Nickname cannot contain curse words")]
    public string Nickname { get; set; }

    [IntegerRangeRule(MinimumAllowed = "5", MaximumAllowed = "150")]
    public int Age { get; set; }

    [Int64RangeRule(MinimumAllowed = "10000000", MaximumAllowed = "200000000000")]
    public long SomeLongProperty { get; set; }

    [DateTimeRangeRule(MaximumAllowed = "1/1/2010", MinimumAllowed = "1/1/1900")]
    public DateTime BirthDate { get; set; }

    public string[] FavoriteColors { get; set; }


Example 3: Applying rules through code


var customer = new Customer();
var validator = Validator<Customer>.Create(customer);

//Define rules
var firstNameRequiredRule = new RequiredRule();
var firstNameLengthRule = new StringLengthRule {MinimumAllowed = "1", MaximumAllowed = "20"};

//Apply rules

//Validate only a single property
ValidationResult firstNameResult = validator.Validate("FirstName");

//Validate entire object, executing all rules applied to all properties
ValidationResult overallResult = validator.Validate();


Example 4: Creating a custom rule


public class CustomRule : Rule
    /// <summary>
    /// Gets or sets the custom rule parameter. This parameter is what gets 
    /// set either as an attribute parameter or a rule property when defining 
    /// rules through runtime code.
    /// </summary>
    /// <value>The custom rule parameter.</value>
    public string CustomRuleParameter { get; set; }

    public override void Validate()
        //Ensures this rule has been bound to a target property

        //Enforce rule logic
        if(PropertyUnderValidation.GetValue<int>() < 0)
            IsValid = false;
            InvalidMessage = "Value cannot be less than zero";


==Out of the box Validation Rules==

* DateTimeConversionRule
* DateTimeRangeRule

* EnumConversionRule

* EnumerableDuplicateRule
* EnumerableNotEmptyRule

* Int64RangeRule

* IntegerRangeRule
* IntegerConversionRule

* MustIncludeTextRule
* CannotIncludeTextRule
* RegexMatchRule
* StringLengthRule
* StringRequiredRule
* DirectoryPathExistsRule

==Features Coming Soon==

Code generator that will allow you to automatically implement IDataErrorInfo on your objects and wire up the specified rules to the IDataErrorInfo indexer call.

Last edited Aug 10, 2010 at 1:05 AM by akilhoffer, version 1


No comments yet.