【问题标题】:FluentValidation MustAsync child loses parent property valueFluentValidation MustAsync 子级丢失父级属性值
【发布时间】: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


    【解决方案1】:

    升级到最新版本的 FluentValidation 解决了这个问题

    【讨论】:

      猜你喜欢
      • 2011-11-06
      • 2021-11-05
      • 1970-01-01
      • 1970-01-01
      • 2013-07-28
      • 1970-01-01
      • 1970-01-01
      • 2021-12-18
      • 2012-05-03
      相关资源
      最近更新 更多