2017-06-14 26 views
1

在父驗證程序中,我們有多個子驗證。聲明如下FluentValidation MustAsync子項失去父項屬性值

  RuleFor(x => x.Country) 
      .Cascade(CascadeMode.StopOnFirstFailure) 
      .NotEmpty().WithMessage(ValidationErrorMessageCodes.CountryRequired) 
      .SetValidator(new CountryValidator(countryService)).WithMessage(ValidationErrorMessageCodes.CountryDoesNotExist); 

在我們設置這個

  RuleFor(c => c) 
      .MustAsync(async candidateCountryCode => 
       (await countryService.GetAll()) 
       .Any(x => string.Equals(x.Code, candidateCountryCode, StringComparison.CurrentCultureIgnoreCase))) 
      .WithMessage(ValidationErrorMessageCodes.CountryDoesNotExist); 

的CountryValidator這一切的時候,我們曾經使用驗證工作得很好,而且必須,但現在我們爲了滿足呼叫更深的使用ValidateAsync和MustAsync在Api中都使用異步任務。

由於改變這種方式,我們得到了以下錯誤消息

屬性名稱不能爲表達式c => C是自動確定。請通過調用'WithName'來指定自定義屬性名稱。

如果我將.WithName(「Country」)添加到子Validator中,那麼返回的parameterName就是「Country.Country」,它曾經是「Country」。

兩者都從AbstractValidator繼承了具有RequestModel且剛剛存在的子級的父級。

我該怎麼做才能回到「國家」錯誤?

回答

0

升級到最新版本的FluentValidation解決了這個問題