2017-05-25 81 views
1

我更新了一個項目,以流利的驗證的最新版本,我得到一個警告:自定義已經過時了

'AbstractValidator<AccountSignInModel>.Custom(Func<AccountSignInModel, ValidationFailure>)' 
is obsolete: 'Use model-level RuleFor(x => x) instead' 

當我使用下面的代碼:

When(x => !String.IsNullOrEmpty(x.Password) && !String.IsNullOrEmpty(x.Username),() => { 

    Custom(x => { 

     Boolean valid = service.ValidateCredentials(x.Username, x.Password)); 

     if (!valid) 
     return new ValidationFailure("Credentials", "Authentication failed"); 

     return null; 

    }); 

    }); 

我不知道如何將其轉換爲RuleFor(x => x)

或者是否有另一種替代定製?

回答

0

我們決定最近在我們的應用程序上使用Fluent Validation。所以我對此很新,在我們前進的道路上找出一些東西。

在搜索其他問題時遇到了您的問題。這方面的資源並不多。以爲我會分享我的想法,如果它可以幫助你。 這是我會做的。

public NewCustomValidator(Interface int) 
    { 
     CascadeMode = CascadeMode.Continue; // you could use StopOnFirstFailure or Continue 

     RuleFor(x=> x.UserName).NotEmpty(); // Will return an error if null or empty 
     RuleFor(x=> x.Password).NotEmpty(); 

     RuleSet("SomeNameHere",() => 
     { 
      CascadeMode = CascadeMode.StopOnFirstFailure; 
      var isValid = false; 
      RuleFor(x=> new { x.UserName, x.Password }) 
      .Must((u , p) => 
      { 
       valid = ValidateCredentials(u, p); 
       return valid; 
      }) 
      .WithMessage("You can show any message if you want"); 

      RuleFor(x => x.UserName).Must((s) => isValid); 
      RuleFor(x => x.Password).Must((s) => isValid); 
     }); 


    } 

所以,我基本上使用你的方法來定義規則集。您可能需要添加一些代碼來驗證規則集。

var result = validator.Validate(NewCustom, ruleSet: "SomeNameHere"); 

聲明:此代碼可能無法編譯。但它會給你一個關於如何解決問題的想法。如果您有更好的想法或者如果您可以使代碼正常工作,請發佈答案。這將幫助我獲得更多的知識。謝謝。

相關問題