多个依赖规则FluentValidation

时间:2017-12-19 08:29:04

标签: c# .net-core fluentvalidation

刚开始使用这个很棒的api,我遇到了多个DependentRules的问题。我有这样的规则

RuleFor(d => d.NotificationType).NotEmpty().WithMessage("Required");
When(d => d.NotificationType.ToUpper() == "EMAIL", () =>
{
    RuleFor(d => d.NotificationEmail).EmailAddress().WithMessage("Invalid Email Address");
    RuleFor(d => d.NotificationEmail).NotEmpty().WithMessage("Required");

});
When(d => d.NotificationType.ToUpper() == "SMS", () =>
{
    RuleFor(d => d.NotificationContactNo).NotEmpty().WithMessage("Required");
});

但是当NotificationTypeEmpty时,它会失败,它已经引发了Required错误。现在在这种情况下,这些其他规则是依赖规则,它们只应在NotificationType不为空时执行。为此,我将规则修改为:

RuleFor(d => d.NotificationType).NotEmpty().WithMessage("Required").DependentRules(k =>
    k.When(d => d.NotificationType.ToUpper() == "EMAIL", () =>
    {
        RuleFor(d => d.NotificationEmail).EmailAddress().WithMessage("Invalid Email Address");
        RuleFor(d => d.NotificationEmail).NotEmpty().WithMessage("Required");
    })
);
RuleFor(d => d.NotificationType).NotEmpty().WithMessage("Required").DependentRules(k =>
    When(d => d.NotificationType.ToUpper() == "SMS", () =>
    {
        RuleFor(d => d.NotificationContactNo).NotEmpty().WithMessage("Required");
    })
);

它正在运作,但我正在重复此规则d.NotificationType).NotEmpty(),我希望实现这样的目标,Multiple Dependent Rules under one Rule

RuleFor(d => d.NotificationType).NotEmpty().WithMessage("Required").DependentRules(k =>
    k.When(d => d.NotificationType.ToUpper() == "EMAIL", () =>
    {
        RuleFor(d => d.NotificationEmail).EmailAddress().WithMessage("Invalid Email Address");
        RuleFor(d => d.NotificationEmail).NotEmpty().WithMessage("Required");
    });
    k.When(d => d.NotificationType.ToUpper() == "SMS", () =>
    {
        RuleFor(d => d.NotificationContactNo).NotEmpty().WithMessage("Required");
    })
);

任何想法我怎么能实现这个目标?

4 个答案:

答案 0 :(得分:2)

您应该在第一条规则上设置CascadeMode,以便第一次失败时验证停止。

RuleFor(d => d.NotificationType)
  .Cascade(CascadeMode.StopOnFirstFailure)
  .NotEmpty()
  .WithMessage("Required");

答案 1 :(得分:0)

它不是那么优雅,但你可以通过修改你的条件来避免失败:

val x: Action<String> = Action { println(it) }

答案 2 :(得分:0)

你的解决方案帮我做了类似的事情

RuleFor(d => d.NotificationType).NotEmpty().WithMessage("Required")
.DependentRules(k =>
    k.When(d => d.NotificationType.ToUpper() == "EMAIL", () =>
    {
        RuleFor(d => d.NotificationEmail).EmailAddress().WithMessage("Invalid Email Address");
        RuleFor(d => d.NotificationEmail).NotEmpty().WithMessage("Required");
    })
).DependentRules(k =>
    k.When(d => d.NotificationType.ToUpper() == "SMS", () =>
    {
        RuleFor(d => d.NotificationContactNo).NotEmpty().WithMessage("Required");
    }));

答案 3 :(得分:0)

我认为你可以使用 When(...).Otherwise(...) 作为嵌套 When() 语句

RuleFor(d => d.NotificationType).NotEmpty().WithMessage("NotificationType Required").DependentRules(() => {
                When(d => d.NotificationType.ToUpper() == "EMAIL", () =>
                {
                    RuleFor(d => d.NotificationEmail).NotEmpty().WithMessage("Email Required")
                    .EmailAddress().WithMessage("Invalid Email Address");
                    //RuleFor(d => d.NotificationEmail).EmailAddress().WithMessage("Invalid Email Address");
                    //RuleFor(d => d.NotificationEmail).NotEmpty().WithMessage("Required");
                }).Otherwise(() => When(d => d.NotificationType.ToUpper() == "SMS", () =>
                {
                    RuleFor(d => d.NotificationContactNo).NotEmpty().WithMessage("ContactNo Required");

                }).Otherwise(() =>
                                {
                                    // Otherwise Validations
                                    //...
                                })
                   );
            });