Skip to content

Latest commit

 

History

History
54 lines (41 loc) · 1.7 KB

README.md

File metadata and controls

54 lines (41 loc) · 1.7 KB

FluentValidation

Full Documentation

A small validation library for .NET that uses a fluent interface and lambda expressions for building validation rules. Written by Jeremy Skinner (http://www.jeremyskinner.co.uk) and licensed under Apache 2.

NuGet Packages

Install-Package FluentValidation

For ASP.NET Core integration:

Install-Package FluentValidation.AspNetCore

For legacy ASP.NET MVC/WebApi integration:

Install-Package FluentValidation.MVC5
Install-Package FluentValidation.WebApi

Example

using FluentValidation;

public class CustomerValidator: AbstractValidator<Customer> {
  public CustomerValidator() {
    RuleFor(customer => customer.Surname).NotEmpty();
    RuleFor(customer => customer.Forename).NotEmpty().WithMessage("Please specify a first name");
    RuleFor(customer => customer.Discount).NotEqual(0).When(customer => customer.HasDiscount);
    RuleFor(customer => customer.Address).Length(20, 250);
    RuleFor(customer => customer.Postcode).Must(BeAValidPostcode).WithMessage("Please specify a valid postcode");
  }

  private bool BeAValidPostcode(string postcode) {
    // custom postcode validating logic goes here
  }
}

Customer customer = new Customer();
CustomerValidator validator = new CustomerValidator();
ValidationResult results = validator.Validate(customer);

bool validationSucceeded = results.IsValid;
IList<ValidationFailure> failures = results.Errors;

Further Documentation

Documentation can be found here.