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.
If you find FluentValidation useful, please consider making a donation
Install-Package FluentValidation
For ASP.NET MVC integration:
Install-Package FluentValidation.MVC5
usingFluentValidation;publicclassCustomerValidator:AbstractValidator<Customer>{publicCustomerValidator(){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");}privateboolBeAValidPostcode(stringpostcode){// custom postcode validating logic goes here}}Customercustomer=newCustomer();CustomerValidatorvalidator=newCustomerValidator();ValidationResultresults=validator.Validate(customer);boolvalidationSucceeded=results.IsValid;IList<ValidationFailure>failures=results.Errors;
