.NET平台開源項目速覽(10)FluentValidation驗證組件深入使用(二)


    在上一篇文章:.NET平台開源項目速覽(6)FluentValidation驗證組件介紹與入門(一) 中,給大家初步介紹了一下FluentValidation驗證組件的使用情況。文章從構建間的驗證器開始,到最后的結果,以及復雜驗證等都做了比較深入的講解和使用。但其實一個完整的組件是麻雀雖小五臟俱全的,一篇文章還不能說明問題,對優秀的組件,我將分開盡量多一點的進行很完整的介紹。在查閱了官方提供的幫助文件中,還有一些組件內部以及個性化的東西,所以今天一起看看看這方面的內容。

    為了保持內容的完整性,大部分內容我都是參考FluentValidation提供的幫助文檔,自己經過翻譯和理解加工。更好的呈現給大家。

.NET開源目錄:【目錄】本博客其他.NET開源項目文章目錄

本文原文地址:.NET平台開源項目速覽(10)FluentValidation驗證組件深入使用(二) 

1.FluentValidation內置驗證器

    FluentValidation在使用的時候,我們也可以看到例子中,有一些驗證方法,例如NotNull等,其實是其內部已經實現的幾個常規的驗證。所以先來看看他們的使用以及作用。FluentValidation本身附帶了一個內置的驗證器,每個驗證器都可以輸出固定格式的錯誤信息。這些內置的驗證器是大家自定義驗證器的基礎。

1.1 空值驗證:NotNull,NotEmpty

1.NotNull:確保屬性的值不是Null,例如代碼:

RuleFor(customer => customer.Surname).NotNull();

2.NotEmpty:確保屬性值不是Null,不為空,或者空白字符串,相當於String的IsNullOrWhiteSpace方法

RuleFor(customer => customer.Surname).NotEmpty();

    上面應該是最簡單也是最常用的吧,畢竟驗證主要是空值相關的。內置驗證器有錯誤信息的格式,例如NotEmpty的信息就是這個樣子:

{PropertyName} = The name of the property being validated
{PropertyValue} = The current value of the property

1.2 相等驗證:NotEqual,Equal

1.NotEqual:不相等驗證,可以驗證固定字符串,也可以驗證和其他屬性相對,比如驗證密碼和用戶名不能相同:

RuleFor(customer => customer.Surname).NotEqual("Foo");
RuleFor(customer => customer.Surname).NotEqual(customer => customer.Forename);

2.Equal:去報指定的字段和其他固定信息或者屬性的值是相等的,例如:

RuleFor(customer => customer.Surname).Equal("Foo");
RuleFor(customer => customer.Password).Equal(customer => customer.PasswordConfirmation);

1.3 大小比較驗證:Length,Less Than,Greater Than等

1.Length:長度比較,主要是確定字符串的長度范圍,相當於Range,例如:

//Surname的長度要在1-250個字符之間
RuleFor(customer => customer.Surname).Length(1, 250);

2.Less Than:主要是進行值比較,確定值<某個給定值或者屬性,例如:

RuleFor(customer => customer.CreditLimit).LessThan(100);
RuleFor(customer => customer.CreditLimit).LessThan(customer => customer.MaxCreditLimit);

3.Less Than Or Equal:比較值小於或者等於某個值或者屬性,例如:

RuleFor(customer => customer.CreditLimit).LessThanOrEqual(100);
RuleFor(customer => customer.CreditLimit).LessThanOrEqual(customer => customer.MaxCreditLimit);

4.Greater Than:比較值大於某個值或者屬性,例如:

RuleFor(customer => customer.CreditLimit).GreaterThan(0);
RuleFor(customer => customer.CreditLimit).GreaterThan(customer => customer.MinimumCreditLimit);

5.Greater Than Or Equal:比較值大於或者等於某個值或者屬性,例如:

RuleFor(customer => customer.CreditLimit).GreaterThanOrEqual(1);
RuleFor(customer => customer.CreditLimit).GreaterThanOrEqual(customer => customer.MinimumCreditLimit);

1.4 其他驗證:正則驗證,Email

     同時FluentValidation也支持Email驗證以及正則表達式驗證。正則表達式是比較強大的,直接支持應該可以給不少人提供方便,雖然我不太懂這玩意。但了解了解也是好的。看看例子:

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

1.5 高級委托驗證:Predicate

     這個單詞不知道咋翻譯,就是這個驗證和其他的不一樣,可以允許使用委托來進行驗證。使用Must方法,也可以接受Lambda表達式,它的使用方法比較多,可以直接指定屬性,或者指定類型,或者只給指定的值去驗證都是可以的,例如下面這幾種用法:

1.單獨驗證屬性:

RuleFor(customer => customer.Identification).Must(BeUniqueId);
public bool BeUniqueId(Customer customer, CustomerId id) {  if (id == ...

2.單獨驗證類:

RuleFor(customer => customer).Must(HaveUniqueId);
public bool HaveUniqueId(Customer customer) { if (customer.Id == ...

3.Lambda表達式:

RuleFor(customer => customer.Id).Must(id => IsUniqueId(id));
public bool IsUniqueId(ID id) { if (id == ...

    這些內置的驗證器就介紹到這里。總的來說,用了這些方法,構建自定義的驗證器就更加方便快捷了。下面我們簡單介紹一下錯誤信息的配置以及本地化的內容。

2.驗證器錯誤信息配置

    在進行驗證的時候,如果不重載錯誤信息方法,會提示類似默認驗證器的一些驗證錯誤信息,但也可以進行自定義。例如,下面2個例子:

RuleFor(customer => customer.Surname).NotNull()
                                     .WithMessage("Please ensure that you have entered your Surname");

     當然也可以搞更復雜的,格式化的信息,例如:

RuleFor(customer => customer.Surname)
  .NotNull()
  .WithMesasge("This message references some other properties: Forename: {0} Discount: {1}", 
    customer => customer.Forename, 
    customer => customer.Discount
  );

    當然還有一些簡單的用法,大家可以參考官方文檔:

    https://github.com/JeremySkinner/FluentValidation/wiki/d.-Configuring-a-Validator

3.其他內容

    其他內容,例如一些特殊的使用方法,限於篇幅和復雜,暫時不翻譯了。這2篇文章已經足夠全面的介紹該組件的功能了。其他內容可以參考下面頁面:

https://github.com/JeremySkinner/FluentValidation/wiki/f.-Localization

https://github.com/JeremySkinner/FluentValidation/wiki/e.-Custom-Validators

4.幫助文檔

    鑒於官方沒有提供離線版的CHM文檔,所以還是老規矩,我自己制作了一個,沒有翻譯相關內容,但看了文章也很容易理解。

blob.png

    下載地址:FluentValidationy驗證幫助文檔.rar


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM