Skip to content

Fluent Properties API

crmckenzie edited this page Mar 30, 2012 · 3 revisions

Simple.Validation provides the Fluent Properties API to simplify the construction of the major out-of-the-box Validators. This API can be very powerful when used in conjunction with the CompositeValidator to create simple property-level validators for your classes.

The Fluent Properties API can be accessed by through the Properties<T> static class where T is the type of object you are validating.

Employee Sample

    public class SaveEmployeeValidator : CompositeValidator<Employee>
    {
        public override bool AppliesTo(string rulesSet)
        {
            return rulesSet == RulesSets.Crud.Save;
        }

        protected override IEnumerable<IValidator<Employee>> GetInternalValidators()
        {
            // [[StringPropertyValidator]]
            yield return Properties<Employee>
                .For(e => e.FirstName)
                .Length(3, 50)
                .Required()
                .IgnoreWhiteSpace();

            // [[StringPropertyValidator]]
            yield return Properties<Employee>
                .For(e => e.LastName)
                .Length(3, 50)
                .Required()
                .IgnoreWhiteSpace()
                ;

            // [[RangePropertyValidator]]
            yield return Properties<Employee>
                .For(e => e.Age)
                .MinValue(18)
                .MaxValue(65)
                ;

            // [[ReferencePropertyValidator]]
            yield return Properties<Employee>
                .For(e => e.Address)
                .Required()
                .Cascade("Save")
                ;

            // [[EnumerablePropertyValidator]]
            yield return Properties<Employee>
                .For(e => e.ContactInfo)
                .Required()
                .Count(1)
                .Unique(c => c.Type)
                .Cascade("Save");
        }
    }

Address Sample

    public class SaveAddressValidator : CompositeValidator
{ public override bool AppliesTo(string rulesSet) { return rulesSet == RulesSets.Crud.Save; } protected override IEnumerable<IValidator<Address>> GetInternalValidators() { // [[StringPropertyValidator]] yield return Properties<Address>. For(a => a.Line1) .Length(0, 50) .Required() .Message("Address Line 1 is required."); // [[StringPropertyValidator]] yield return Properties<Address> .For(a => a.Line2) .Length(0, 50) .NotRequired() .Message("Line 2 must be between 0 and 50 characters in length."); // [[StringPropertyValidator]] yield return Properties<Address> .For(a => a.Line3) .Length(0, 50) .NotRequired() .Message("Line 3 must be between 0 and 50 characters in length."); // [[StringPropertyValidator]] yield return Properties<Address> .For(a => a.PostalCode) .Length(0, 20) .NotRequired() .Message("Postal Code must be between 0 and 20 characters in length."); // [[StringPropertyValidator]] yield return Properties<Address> .For(a => a.Country) .Length(0, 3) .NotRequired() .Message("Country must be between 0 and 3 characters in length."); // [[StringPropertyValidator]] yield return Properties<Address> .For(a => a.StateOrProvince) .Length(0, 50) .NotRequired() .Message("StateOrProvince must be between 0 and 50 characters in length."); } }