Skip to content

abhijeethaval/xVal.net

Repository files navigation

What is it?

xVal.net is a fluent validation library for .NET. It is designed to be flexible. It allows specifying the validation rules for the .NET objects using fluent API. There are three types of validation rules which can be created.

e1. ValidationRule : Simple validation rule with precondition, validation expression, error message with format parameter. 2. ChildValidationRule<TEntity, TChild> : Validation rule to validate child property. 3. CollectionChildValidationRule<TEntity, TChild> : Validation rule to validate IEnumerabl child property. 4. CompositeValidationRule : Validation rule to validate multiple validation rules on certain pre-condition with error message describing the pre-condition.

#How to use Following example shows usage of the framework r

  • Entities for which validation rules are built
    public class Employee
    {
        public int? Id { get; set; }
        public string Firstname { get; set; }
        public string Lastname { get; set; }

        public Address Address { get; set; }

        public IEnumerable<PhoneNumber> ContactNumbers { get; set; }
    }

    public class PhoneNumber
    {
        public int? Number { get; set; }

        public PhoneType? Type { get; set; }
    }

    public enum PhoneType
    {
        Mobile,
        Home,
        Office,
    }

    public class Address
    {
        public string Street { get; set; }
        public string City { get; set; }
    }
  • Example of simple ValidationRule
            var firstnameRule = ValidationRule.For<Employee>()
                 .Validate(e => e.Firstname != null)
                 .When(e => e.Id != null)
                 .Message("Firstname is mandatory. Employee Id = {0}", e => e.Id)
                 .Build();
  • There is overload of Message method which allows to use string interpolation in C# as below.
            var firstnameRule = ValidationRule.For<Employee>()
                 .Validate(e => e.Firstname != null)
                 .When(e => e.Id != null)
                 .Message(e => $"Firstname is mandatory. Employee Id = {e.Id}")
                 .Build();
  • Example of ChildValidationRule<TEntity, TChild> Here we are creating rule for validating that City property of an address of an employee is required field.
            var addressRule = ValidationRule.For<Address>()
                .Validate(a => a.City != null)
                .Message("City is required.")
                .Build();

            var propertyRule = ValidationRule.For<Employee>()
                .ForChild(e => e.Address)
                .Validate(addressRule)
                .When(e => e.Id != null)
                .Message("Employee Id = {0}", e => e.Id)
                .Build();
  • Example of CollectionChildValidationRule<TEntitiy, TChild> Here we are creating rule for validating that the Number property of each of multiple contacts is required.
            var contactNumberRule = ValidationRule.For<PhoneNumber>()
                .Validate(p => p.Number != null)
                .Message("Phone number is required.")
                .Build();

            var enumerableChildrenRule = ValidationRule.For<Employee>()
                .ForChildren(e => e.ContactNumbers)
                .Validate(contactNumberRule)
                .When(e => e.Id != null)
                .Message("some message")
                .Build();
  • Example of CompositeValidationRule Here we are creating rule that when Employee.Id is not null validate the two rules; firstnameRule created in example for simple validation rule and lastnameRule created here.
            var lastnameRule = ValidationRule.For<Employee>()
                .Validate(e => e.Lastname != null)
                 .When(e => e.Id != null)
                .Message("Lastname is mandatory. I.When(e => e.Id != null)d = {0}", e => e.Id)
                .Build();

            var compositeRule = ValidationRule.For<Employee>()
                .Validate(firstnameRule, lastnameRule)
                .When(e => e.Id != null)
                .Message("Validations failed. Id = {0}, Firstname = {1}", e => e.Id, e => e.Firstname);

About

Validation framework for .Net

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages