[英]FluentValidation MustAsync child loses parent property value
In the parent validator, we have multiple child validations. 在父验证器中,我们有多个子验证。 Declared like this
宣布像这样
RuleFor(x => x.Country)
.Cascade(CascadeMode.StopOnFirstFailure)
.NotEmpty().WithMessage(ValidationErrorMessageCodes.CountryRequired)
.SetValidator(new CountryValidator(countryService)).WithMessage(ValidationErrorMessageCodes.CountryDoesNotExist);
In the CountryValidator we set this 在CountryValidator中我们设置了这个
RuleFor(c => c)
.MustAsync(async candidateCountryCode =>
(await countryService.GetAll())
.Any(x => string.Equals(x.Code, candidateCountryCode, StringComparison.CurrentCultureIgnoreCase)))
.WithMessage(ValidationErrorMessageCodes.CountryDoesNotExist);
This all worked fine when we used to use Validate and Must, but now we are using ValidateAsync and MustAsync in order to satisfy calls deeper in the Api all using async Tasks. 当我们习惯使用Validate和Must时,这一切都运行正常,但现在我们使用ValidateAsync和MustAsync来满足使用异步任务的Api中更深层次的调用。
Since changing it this way we get the following error message 由于以这种方式更改它,我们收到以下错误消息
Property name could not be automatically determined for expression c => c.
无法为表达式c => c自动确定属性名称。 Please specify either a custom property name by calling 'WithName'.
请通过调用'WithName'指定自定义属性名称。
If I add .WithName("Country") to the child Validator, then the parameterName returned is "Country.Country" where it used to be just "Country". 如果我将.WithName(“Country”)添加到子Validator,那么返回的parameterName是“Country.Country”,它曾经只是“Country”。
Both inherit from AbstractValidator the parent having a RequestModel and the child just being . 两者都从AbstractValidator继承了具有RequestModel的父级和子级。
What can I do to just get back to the "Country" error ? 我该怎么做才能回到“国家/地区”错误?
升级到最新版本的FluentValidation解决了这个问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.