[英]How to organize FluentValidation rules so that they may be reused in multiple validators?
[英]Multiple Dependent Rules 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");
});
但是當NotificationType
為Empty
時這會失敗,它已經引發了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");
})
);
知道我怎么能做到這一點嗎?
您應該在第一條規則上設置CascadeMode ,以便驗證在第一次失敗時停止。
RuleFor(d => d.NotificationType)
.Cascade(CascadeMode.StopOnFirstFailure)
.NotEmpty()
.WithMessage("Required");
它不是那么優雅,但您可以通過修改條件來避免失敗:
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");
});
你的解決方案通過做這樣的事情幫助了我
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");
}));
我認為你可以使用 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.