2015-07-10 59 views
1

我有一個驗證規則,對於許多我希望集中到DRY的屬性是常見的,但NotEmpty()規則等工作正常,Matches(...)和其他字符串驗證程序規則不要編譯。FluentValidation集中驗證器擴展中的正則表達式驗證程序

沒有問題:

public static IRuleBuilderOptions<T, TProperty> MustNotContainHtml<T, TProperty>(this IRuleBuilderOptions<T, TProperty> ruleBuilder)  
    { 
     return ruleBuilder.NotEmpty().WithMessage("Some message."); 
    } 

理解不能編譯,因爲使用Matches(...)這是字符串只:

public static IRuleBuilderOptions<T, TProperty> MustNotContainHtml<T, TProperty>(this IRuleBuilderOptions<T, TProperty> ruleBuilder) 
    { 
     return ruleBuilder.Matches("<[a-z!/?]|&#").WithMessage("'{PropertyName}' contains special HTML characters which is not allowed."); 
    } 

什麼規則生成的簽名是否有可用的字符串,唯一的選擇?

回答

0

的解決方案是重用實際RegularExpressionValidator

public static IRuleBuilderOptions<T, string> MustNotContainHtml<T>(this IRuleBuilder<T, string> ruleBuilder) 
    { 
     return ruleBuilder.SetValidator(new RegularExpressionValidator("<[a-z!/?]|&#")).WithMessage("Some custom message."); 
    } 
+0

您可以接受你自己的答案爲好,這有助於我們確定需要幫助:) –

+0

我知道的問題,但仍需要之前要等到明天允許;-) – Ted