【问题标题】:Validation Rule that depends on success of group of independent rules using Fluent Validation依赖于使用 Fluent Validation 的独立规则组的成功的验证规则
【发布时间】:2022-11-02 18:49:11
【问题描述】:

我正在尝试为这种情况提出正确的语法:

规则
规则B
以上都是独立的

规则 C- 只有在 RuleS 和 Rule 都通过验证时才会运行。

例子:
用户 ID 存在 (RuleS)
订单 ID 存在(规则)
OrderId 属于 UserId - 依赖于上述两个规则成功的规则

代码示例(即使 UserIdExists 或 OrderIdExists 之一未能通过验证,也无法调用 OrderBelongsToUser):

RuleFor(request => request).NotNull().DependentRules(() =>
        {
            RuleFor(request => request).CustomAsync(UserIdExists)

            RuleFor(request => request).CustomAsync(OrderIdExists)

        }).CustomAsync(OrderBelongsToUser);

【问题讨论】:

    标签: fluentvalidation


    【解决方案1】:

    您的“UserIdExists”和“OrderIdExists”规则现在仅取决于请求不为空。如果您只想在用户和订单 ID 存在的情况下执行规则“OrderBelongsToUser”,您可以执行以下操作:

    RuleFor(request => request)
        .ChildRules(child =>
        {
            child.RuleFor(request => request)
                .CustomAsync(UserIdExists)
                .CustomAsync(OrderIdExists)
        })
        .DependentRules(() =>
        {
            RuleFor(request => request).CustomAsync(OrderBelongsToUser);
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-19
      • 1970-01-01
      • 2011-10-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多