A .NET Core library that uses lambda expressions to build validations rules.
Nuget Package
Install-Package FluentValidator.Core
Example Usage
Initializing validations from a constructor
public class Person : Notifiable
{
public string Name { get; set; }
public string Email { get; set; }
public int Age { get; set; }
public Person(){}
public Person(string name, string email, int age)
{
this.Name = name;
this.Email = email;
this.Age = age;
new ValidationContract<Person>(this).IsRequired(x => x.Name);
new ValidationContract<Person>(this).IsRequired(x => x.Email);
new ValidationContract<Person>(this).IsGreaterThan(x => x.Age, 18);
new ValidationContract<Person>(this).IsEmail(x => x.Email);
}
}
Initializing validations from an existing object instance
public class Person : Notifiable
{
public string Name { get; set; }
public string Email { get; set; }
public int Age { get; set; }
public Person()
{
}
}
class Program
{
static void Main(string[] args)
{
Person newPerson = new Person();
newPerson.Name = "Milton";
newPerson.Email = "[email protected]";
newPerson.Age = 15;
new ValidationContract<Person>(newPerson).IsRequired(x => x.Name);
new ValidationContract<Person>(newPerson).IsRequired(x => x.Email);
new ValidationContract<Person>(newPerson).IsGreaterThan(x => x.Age, 18);
new ValidationContract<Person>(newPerson).IsEmail(x => x.Email);
Console.WriteLine(newPerson.IsValid());
foreach(var message in newPerson.Notifications)
{
Console.WriteLine(message.Message);
}
}
}