简体   繁体   English

多重依赖规则 FluentValidation

[英]Multiple Dependent Rules FluentValidation

Just started using this awesome api, I am facing some issue with multiple DependentRules .刚开始使用这个很棒的 api,我遇到了多个DependentRules问题。 I had rules like this我有这样的规则

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");
});

But this fails when NotificationType is Empty ,it already raised the Required error.但是当NotificationTypeEmpty时这会失败,它已经引发了Required错误。 Now in this case these other rules are dependent rules and they should only execute when NotificationType is not empty.现在在这种情况下,这些其他规则是依赖规则,它们应该只在NotificationType不为空时执行。 For this i have modified the rules as:为此,我将规则修改为:

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");
    })
);

It is working but i am repeating this rule d.NotificationType).NotEmpty() , I want to achieve something like this, Multiple Dependent Rules under one Rule .它正在工作,但我正在重复这条规则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");
    })
);

Any idea how can i achieve this ?知道我怎么能做到这一点吗?

You should set the CascadeMode on your first rule, so that the validation stops on the first failure.您应该在第一条规则上设置CascadeMode ,以便验证在第一次失败时停止。

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

It is not so elegant but you can avoid failure by modifying your conditions:它不是那么优雅,但您可以通过修改条件来避免失败:

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

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

});

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

Your solution helped me by doing something like this你的解决方案通过做这样的事情帮助了我

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");
    }));

I think you can use When(...).Otherwise(...) as nest When() statements我认为你可以使用 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
                                    //...
                                })
                   );
            });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM