Fluent Validation :

Il s'agit d'une petite bibliothèque de validation qui utilise une interface fluide et les expressions lambda pour construire des règles de validation pour vos objets métier.

Plateformes cibles :
  • .NET 4.0
  • ASP.NET MVC 4 (for MVC integration)
  • Silverlight 4 or 5 (for Silverlight integration)
  • Windows Phone 8 (for Windows Phone integration)


Ex :
Code C# : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 
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.Company).NotNull();
    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;