Skip to content

mjamro/FluentValidation

 
 

Repository files navigation

FluentValidation

Full Documentation

A validation library for .NET that uses a fluent interface and lambda expressions for building strongly-typed validation rules.


Supporting the project

If you use FluentValidation in a commercial project, please sponsor the project financially. FluentValidation is developed and supported by @JeremySkinner for free in his spare time and financial sponsorship helps keep the project going. You can sponsor the project via either GitHub sponsors or OpenCollective.


Get Started

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

Build Status

FluentValidation NuGet Nuget
FluentValidation.AspNetCore NuGet Nuget

Example

using FluentValidation;

public class CustomerValidator: AbstractValidator<Customer> {
  public CustomerValidator() {
    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");
  }

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

var customer = new Customer();
var validator = new CustomerValidator();

// Execute the validator
ValidationResult results = validator.Validate(customer);

// Inspect any validation failures.
bool success = results.IsValid;
List<ValidationFailure> failures = results.Errors;

Documentation

Documentation can be found on the project site.

License, Copyright etc

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-2021 .NET Foundation, Jeremy Skinner and other contributors and is licensed under the Apache2 license.

Sponsors

This project is sponsored by the following organisations whose support help keep this project going:

  • JetBrains who both provide financial sponsorship as well as access to their developer tools free of charge.
  • Dotnetos

.NET Foundation

This project is part of the .NET Foundation.

About

A popular .NET validation for building strongly-typed validation rules.

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 99.7%
  • Other 0.3%