A small validation library for .NET that uses a fluent interface and lambda expressions for building validation rules.
FluentValidation can be installed using the Nuget package manager or the dotnet CLI.
dotnet add package FluentValidation
For ASP.NET Core integration:
dotnet add package FluentValidation.AspNetCore
FluentValidation | ||
FluentValidation.AspNetCore | ||
FluentValidation.Mvc5 | ||
FluentValidation.WebApi |
usingFluentValidation;publicclassCustomerValidator:AbstractValidator<Customer>{publicCustomerValidator(){RuleFor(x =>x.Surname).NotEmpty();RuleFor(x =>x.Forename).NotEmpty().WithMessage("Please specify a first name");RuleFor(x =>x.Discount).NotEqual(0).When(x =>x.HasDiscount);RuleFor(x =>x.Address).Length(20,250);RuleFor(x =>x.Postcode).Must(BeAValidPostcode).WithMessage("Please specify a valid postcode");}privateboolBeAValidPostcode(stringpostcode){// custom postcode validating logic goes here}}varcustomer=newCustomer();varvalidator=newCustomerValidator();ValidationResultresults=validator.Validate(customer);boolsuccess=results.IsValid;IList<ValidationFailure>failures=results.Errors;Documentation can be found on the project site.
FluentValidation has adopted the code of conduct defined by the Contributor Covenant to clarify expected behavior in our community. For more information see the .NET Foundation Code of Conduct.
FluentValidation is copyright © 2008-2020 .NET Foundation, Jeremy Skinner and other contributors and is licensed under the Apache2 license.
This project is supported by the .NET Foundation.
