[英]FluentValidation: Using a parent property value in a child collection rule
[英]FluentValidation MustAsync child loses parent property value
在父驗證器中,我們有多個子驗證。 宣布像這樣
RuleFor(x => x.Country)
.Cascade(CascadeMode.StopOnFirstFailure)
.NotEmpty().WithMessage(ValidationErrorMessageCodes.CountryRequired)
.SetValidator(new CountryValidator(countryService)).WithMessage(ValidationErrorMessageCodes.CountryDoesNotExist);
在CountryValidator中我們設置了這個
RuleFor(c => c)
.MustAsync(async candidateCountryCode =>
(await countryService.GetAll())
.Any(x => string.Equals(x.Code, candidateCountryCode, StringComparison.CurrentCultureIgnoreCase)))
.WithMessage(ValidationErrorMessageCodes.CountryDoesNotExist);
當我們習慣使用Validate和Must時,這一切都運行正常,但現在我們使用ValidateAsync和MustAsync來滿足使用異步任務的Api中更深層次的調用。
由於以這種方式更改它,我們收到以下錯誤消息
無法為表達式c => c自動確定屬性名稱。 請通過調用'WithName'指定自定義屬性名稱。
如果我將.WithName(“Country”)添加到子Validator,那么返回的parameterName是“Country.Country”,它曾經只是“Country”。
兩者都從AbstractValidator繼承了具有RequestModel的父級和子級。
我該怎么做才能回到“國家/地區”錯誤?
升級到最新版本的FluentValidation解決了這個問題
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.