【发布时间】:2017-06-14 09:28:56
【问题描述】:
在父验证器中,我们有多个子验证器。像这样声明
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 的父级和子级。
我该怎么做才能回到“国家”错误?
【问题讨论】:
标签: c# fluentvalidation