基于.NET的FluentValidation数据验证实现(2)

public class CustomerValidator : AbstractValidator<Customer> { public CustomerValidator() { RuleForEach(t => t.Address).SetValidator(new AdressValidator()); //RuleFor(x => x.Address).SetCollectionValidator(new AdressValidator());在8.0版本及以后弃用 } }

在编写验证规则时,可以通过 Where 关键字排除或者筛选不需要验证的对象。

public class CustomerValidator : AbstractValidator<Customer> { public CustomerValidator() { RuleForEach(t => t.Address).Where(t => !string.IsNullOrEmpty(t.City)).SetValidator(new AdressValidator()); //RuleFor(x => x.Address).SetCollectionValidator(new AdressValidator()).Where(t => !string.IsNullOrEmpty(t.City));在8.0版本及以后弃用 } }

从FluentValidation 8.5开始,您还可以使用以下ChildRules方法在线定义子集合元素的规则,从而不用再定义另一个验证器:

RuleForEach(t => t.Address).ChildRules(adderss => { adderss.RuleFor(t => t.City).NotEmpty(); adderss.RuleFor(t => t.Province).NotEmpty(); }).NotEmpty();

支持规则集

规则集允许您将验证规则分组在一起,这些规则可以作为一个组一起执行,而忽略其他规则:

我们可以把“姓”和“名”统一加在一个姓名规则集中。

public class CustomerValidator : AbstractValidator<Customer> { public CustomerValidator() { RuleSet("Name", () => { RuleFor(t => t.Surname).NotEmpty(); RuleFor(t => t.Forename).NotEmpty(); }); } }

规则集通过一般的 Validate 方法是不会执行验证的,需要用如下方法进行单独的验证,这将复杂的验证器定义分解为较小的部分进行验证,IncludeRuleSets 中可以传入多个规则集名称来执行多个规则集的验证:

Customer customer = new Customer(); CustomerValidator validationRules = new CustomerValidator(); ValidationResult validationResult = validationRules.Validate(customer, options => options.IncludeRuleSets("Name"));

还可以通过 IncludeRulesNotInRuleSet 方法或使用特殊名称“默认”(不区分大小写)来执行验证所有不属于规则集的规则:

ValidationResult validationResult = validationRules.Validate(customer, options =>{ options.IncludeRulesNotInRuleSet(); options.IncludeRuleSets("Name"); } );

可以通过调用强制执行所有规则,而不管它们是否在规则集中 IncludeAllRuleSets(这等效于using IncludeRuleSets("*"))。

同个类型的多个验证器

一个验证器可以包含多个其他的验证器,只要这些验证器都是验证统一类型的即可。这样就可以拆分验证器,通过不同的需求组合在一起:

public class CustomerValidator : AbstractValidator<Customer> { public CustomerValidator() { Include(new CustomerSurnameValidator()); Include(new CustomerForenameValidator()); } } public class CustomerSurnameValidator : AbstractValidator<Customer> { public CustomerSurnameValidator() { RuleFor(t => t.Surname).NotEmpty(); } } public class CustomerForenameValidator : AbstractValidator<Customer> { public CustomerForenameValidator() { RuleFor(t => t.Forename).NotEmpty(); } }

继承验证

从FluentValidation 9.2开始,如果您的对象属性作为其他类的基类或接口,则可以为各个子类/实现器设置特定的子验证器,来验证这个属性。我们的类设置如下:

public class Store { public People Peoples { get; set; } } public class Customer : People { public string Address { get; set; } } public class People { public string Name { get; set; } }

验证器如下:

public class StoreValidator : AbstractValidator<Store> { public StoreValidator() { RuleFor(t => t.Peoples).NotNull().SetInheritanceValidator(t => { t.Add<Customer>(new CustomerValidator()); }); } } public class CustomerValidator : AbstractValidator<Customer> { public CustomerValidator() { RuleFor(t => t.Address).NotEmpty(); } }

覆盖消息

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/d39ab55b6f956129eebb4104b5059fb9.html