Skip to content

A lightweight and high performance entity validation framework.

Notifications You must be signed in to change notification settings

yangzhyo/validation-lite

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

validation-lite

  • A lightweight and high performance entity validation framework.
  • Based on generic type and expression tree, it's very efficient.
  • Flexible and reusable.

How to use

For Single Object

Person john = repo.GetPerson();

var v = new ValidateFor<Person>()
    .Field(p => p.Name).ShouldNotEmpty().Length(1, 10)
    .Field(p => p.Age).ShouldGreaterThan(0)
    .Field(p => p.Height).ShouldGreaterThanOrEqualTo(1.8m)
    .Field(p => p.Weight).ShouldLessThan(100m)
    .Field(p => p.Debt).ShouldLessThanOrEqualTo(0)
    .Field(p => p.Money).ShouldEqualTo(1000000)
    .Field(p => p.Company).ShouldNotNull().ValidateWith(
        new ValidateFor<Company>()
        .Field(c => c.Address).ShouldNotEmpty()
        .Field(c => c.EmployeeCount).ShouldGreaterThan(0)
    )
    .EnumerableField<List<Book>, Book>(p => p.FavoriteBooks).ShouldHaveData().ValidateWith(
        new ValidateFor<Book>()
        .Field(b => b.Name).ShouldNotEmpty()
        .Field(b => b.PageCount).ShouldGreaterThan(0)
    )
    .Entity().ShouldPassCustomCheck(CustomCheck)
    .Build();
	
var r = v.Validate(john);
if (!r.IsValid)
{
    //Show r.ErrorMessages;
}

For Enumerable Object

List<Person> persons = new List<Person>()
{
    new Person() {Name = "John"}
};

var v = new ValidateForEnumerable<List<Person>, Person>()
    .ShouldHaveData()
    .ShouldPassCustomCheck(ps => ValidationResult.Valid);
    .ValidateElementWith(
        new ValidateFor<Person>()
            .Field(p => p.Name).ShouldNotEmpty());

var r = v.Validate(persons);

Reusable Validator

public class PersonValidator : ValidateFor<Person>
{
    public PersonValidator()
    {
        this.Field(p => p.Age).ShouldGreaterThan(0)
            .Field(p => p.Company).ShouldNotNull().ValidateWith(new CompanyValidator());
    }
}

public class CompanyValidator : ValidateFor<Company>
{
    public CompanyValidator()
    {
        this.Field(c => c.Address).ShouldNotEmpty().ShouldPassCustomCheck(CheckAddress);
    }

    public ValidationResult CheckAddress(string address)
    {
        return ValidationResult.Valid;
    }
}

Person john = new Person();
PersonValidator validator = new PersonValidator();
ValidationResult result = validator.Validate(john);

About

A lightweight and high performance entity validation framework.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages