验证组件——FluentValidation

时间:2022-12-23 18:42:02

      FluentValidation

  FluentValidation是与ASP.NET DataAnnotataion Attribute验证实体不同的数据验证组件,提供了将实体与验证分离开来的验证方式,同时FluentValidation还提供了表达式      链式语法。

地址:

https://github.com/JeremySkinner/FluentValidation

     static void Main(string[] args)
{
//非空验证
TestValidator validator = new TestValidator(v => v.RuleFor(x => x.Surname).NotNull());
var result= validator.Validate(new Person { Surname = "Foo" });//true
Console.WriteLine(result.IsValid);
result = validator.Validate(new Person { Surname = null});//false
Console.WriteLine(result.IsValid); validator = new TestValidator {
v => v.RuleFor(x => x.Surname).NotEmpty()
};
result = validator.Validate(new Person { Surname = null });
Console.WriteLine(result.IsValid); }
 public class Person
{
public string NameField;
public int Id { get; set; }
public string Surname { get; set; }
public string Forename { get; set; } }

源码有很多测试示例