包介绍 - Fluent Validation (用于验证)

Install-Package FluentValidation

 

如果你使用MVC5 可以使用下面的包

Install-Package FluentValidation.MVC5

 

例子:

public class CustomerValidator : AbstractValidator<Customer>
    {
        public CustomerValidator()
        {
            //不能为空
            RuleFor(customer => customer.Surname).NotEmpty();
            
            //自定义提示语
            RuleFor(customer => customer.Forename).NotEmpty().WithMessage("Pls 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("Pls specify a valid postCode");

        }

        /// <summary>
        /// 自定义验证
        /// </summary>
        /// <param name="arg"></param>
        /// <returns></returns>
        private bool BeAValidPostCode(string arg)
        {
            throw new NotImplementedException();
        }
    }
Customer customer = new Customer();
CustomerValidator validator = new CustomerValidator();
ValidationResult results = validator.Validate(customer);

bool validationSucceeded = results.IsValid;
IList<ValidationFailure> failures = results.Errors;

  

在一个属性上应用链式验证

public class CustomerValidator : AbstractValidator<Customer> {
  public CustomerValidator {
    RuleFor(customer => customer.Surname).NotNull().NotEqual("foo");
  }
}

 

抛出例外

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

validator.ValidateAndThrow(customer);

 

在复杂属性里面使用验证

public class Customer {
  public string Name { get; set; }
  public Address Address { get; set; }
}

public class Address {
  public string Line1 { get; set; }
  public string Line2 { get; set; }
  public string Town { get; set; }
  public string County { get; set; }
  public string Postcode { get; set; }
}

 

public class AddressValidator : AbstractValidator<Address> {
  public AddressValidator() {
    RuleFor(address => address.Postcode).NotNull();
    //etc
  }
}

 

public class CustomerValidator : AbstractValidator<Customer> {
  public CustomerValidator() {
    RuleFor(customer => customer.Name).NotNull();
    RuleFor(customer => customer.Address).SetValidator(new AddressValidator())
  }
} 

 

在集合属性中使用Validator

public class OrderValidator : AbstractValidator<Order> {
    public OrderValidator() {
        RuleFor(x => x.ProductName).NotNull();
        RuleFor(x => x.Cost).GreaterThan(0);
    }
}

 

public class CustomerValidator : AbstractValidator<Customer> {
    public CustomerValidator() {
        RuleFor(x => x.Orders).SetCollectionValidator(new OrderValidator());
    }
}

var validator = new CustomerValidator();
var results = validator.Validate(customer);

集合验证的错误信息如下

foreach(var result in results.Errors) {
   Console.WriteLine("Property name: " + result.PropertyName);
   Console.WriteLine("Error: " + result.ErrorMessage);
   Console.WriteLine("");
}

 

Property name: Orders[0].Cost
Error: 'Cost' must be greater than '0'.

Property name: Orders[1].ProductName
Error: 'Product Name' must not be empty.

 

验证集合  

RuleSet能让你选择性的验证某些验证组 忽略某些验证组

 public class PersonValidator : AbstractValidator<Person> {
  public PersonValidator() {
     RuleSet("Names", () => {
        RuleFor(x => x.Surname).NotNull();
        RuleFor(x => x.Forename).NotNull();
     });
 
     RuleFor(x => x.Id).NotEqual(0);
  }
}

下面的代码我们只验证Person的Surname和ForeName两个属性

var validator = new PersonValidator();
var person = new Person();
var result = validator.Validate(person, ruleSet: "Names");

 

一次验证多个集合

validator.Validate(person, ruleSet: "Names,MyRuleSet,SomeOtherRuleSet")

 

同样你可以验证那些没有被包含到RuleSet里面的规则  这些验证是一个特殊的ruleset 名为"default"

validator.Validate(person, ruleSet: "default,MyRuleSet")

 

正则表达式验证器

RuleFor(customer => customer.Surname).Matches("some regex here");

 

Email验证器

RuleFor(customer => customer.Email).EmailAddress();

 

覆盖默认的属性名

RuleFor(customer => customer.Surname).NotNull().WithName("Last name");

或者

public class Person {
  [Display(Name="Last name")]
  public string Surname { get; set; }
}

 

设置验证条件

When(customer => customer.IsPreferred, () => {
   RuleFor(customer => customer.CustomerDiscount).GreaterThan(0);
   RuleFor(customer => customer.CreditCardNumber).NotNull();
});

 

写一个自定义属性验证器

public class ListMustContainFewerThanTenItemsValidator<T> : PropertyValidator {

	public ListMustContainFewerThanTenItemsValidator() 
		: base("Property {PropertyName} contains more than 10 items!") {
		
	}

	protected override bool IsValid(PropertyValidatorContext context) {
		var list = context.PropertyValue as IList<T>;

		if(list != null && list.Count >= 10) {
			return false;
		}

		return true;
	}
}

 

public class PersonValidator : AbstractValidator<Person> {
    public PersonValidator() {
       RuleFor(person => person.Pets).SetValidator(new ListMustContainFewerThanTenItemsValidator<Pet>());
    }
}

 

 

与MVC集成

1. 

protected void Application_Start() {
    AreaRegistration.RegisterAllAreas();

    RegisterGlobalFilters(GlobalFilters.Filters);
    RegisterRoutes(RouteTable.Routes);

    FluentValidationModelValidatorProvider.Configure();
}

2.

[Validator(typeof(PersonValidator))]
public class Person {
	public int Id { get; set; }
	public string Name { get; set; }
	public string Email { get; set; }
	public int Age { get; set; }
}
 
public class PersonValidator : AbstractValidator<Person> {
	public PersonValidator() {
		RuleFor(x => x.Id).NotNull();
		RuleFor(x => x.Name).Length(0, 10);
		RuleFor(x => x.Email).EmailAddress();
		RuleFor(x => x.Age).InclusiveBetween(18, 60);
	}
}

3.

[HttpPost]
	public ActionResult Create(Person person) {
 
		if(! ModelState.IsValid) { // re-render the view when validation failed.
			return View("Create", person);
		}
 
		TempData["notice"] = "Person successfully created";
		return RedirectToAction("Index");
 
	}

或者只验证指定的RuleSet

public ActionResult Save([CustomizeValidator(RuleSet="MyRuleset")] Customer cust) {
  // ...
}

或者只验证指定的属性

public ActionResult Save([CustomizeValidator(Properties="Surname,Forename")] Customer cust) {
  // ...
}

另外在验证的权限还有一个钩子 可以通过实现IValidatorInterceptor在验证的前后做一些工作

public interface IValidatorInterceptor {
    ValidationContext BeforeMvcValidation(ControllerContext controllerContext, ValidationContext validationContext);
 
    ValidationResult AfterMvcValidation(ControllerContext controllerContext, ValidationContext validationContext, ValidationResult result);
}

  

posted @ 2015-03-02 13:49  irocker  阅读(1970)  评论(0编辑  收藏  举报