[英]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.