简体   繁体   English

FluentValidation MustAsync子节点丢失父属性值

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

相关问题 FluentValidation:在子集合规则中使用父属性值 - FluentValidation: Using a parent property value in a child collection rule FluentValidation,如何在子验证程序异常消息中包括父属性名称 - FluentValidation, how to include parent property name in child validator exception message 方法 MustAsync() 与 WhenAsync() FluentValidation 之间的差异 - Differences between methods MustAsync() vs WhenAsync() FluentValidation WebAPI-FluentValidation-根据父模型值验证子模型属性 - WebAPI - FluentValidation - Validate Child model properties based on parent model value 为什么我在 FluentValidation MustAsync 中收到 CS1061 错误 - Why am I receiving CS1061 error in FluentValidation MustAsync 测试该属性是否具有子验证器 FluentValidation - Test that property has child validator FluentValidation FluentValidation-以嵌套结构将上下文从父级带入子级的最佳方法 - FluentValidation - Best approach to take context from the parent into the child in a nested structure 在验证上下文中获取子属性中的父对象属性值 - Get the parent object property value in child property in Validation Context 将子属性与父属性进行比较? - Compare child property with parent property? FluentValidation 子验证器中的 PropertyName - PropertyName in FluentValidation child validators
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM