FluentValidation

FluentValidation is a A .NET library for building strongly-typed validation rules.

The following platforms are supported:

For automatic validation with ASP.NET, FluentValidation supports ASP.NET Core 2.1 and 3.1.

If you’re new to using FluentValidation, check out the Creating your first validator page.

Note

FluentValidation is developed and supported by @JeremySkinner in his spare time. If you find FluentValidation useful, or if you use FluentValidation in a commercial environment, then please consider financially supporting the project on one of the following platforms, which will help keep the project going.

Example

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
  }
}

Localization