ASP.NET Core Web API-如何使用Fluent验证执行条件相对验证
在ASP.NET Core-6 Web API中,我使用流利的验证有此验证器:
public CustomerValidator()
{
RuleFor(p => p.NotificationType)
.Cascade(CascadeMode.StopOnFirstFailure)
.NotEmpty().WithMessage("{PropertyName} should be not empty. NEVER!");
RuleFor(p => p.MobileNumber)
.Cascade(CascadeMode.StopOnFirstFailure)
.NotEmpty().WithMessage("{PropertyName} should be not empty. NEVER!");
RuleFor(p => p.Email)
.EmailAddress();
}
我想使用以下条件进行验证:
如果NotificationType ='电子邮件',则需要电子邮件,
如果NotificationType ='SMS',则应需要Mobilenumber
如果NotificationType ='两个',则应需要电子邮件和Mobilenumber,
该如何实现?
谢谢
In ASP.NET Core-6 Web API, I have this validator using Fluent Validation:
public CustomerValidator()
{
RuleFor(p => p.NotificationType)
.Cascade(CascadeMode.StopOnFirstFailure)
.NotEmpty().WithMessage("{PropertyName} should be not empty. NEVER!");
RuleFor(p => p.MobileNumber)
.Cascade(CascadeMode.StopOnFirstFailure)
.NotEmpty().WithMessage("{PropertyName} should be not empty. NEVER!");
RuleFor(p => p.Email)
.EmailAddress();
}
I want to validate using these conditions:
If NotificationType = 'Email', then Email should be required
If NotificationType = 'SMS', then MobileNumber should be required
If NotificationType = 'Both', then Email and MobileNumber should be required
How do I achive this?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 fluentvalidation时使用
。
https://docs.fluentvalidation.net/en/en/latest/latest/conditions.httions.html
You can use the
When()
extension on fluentvalidation.https://docs.fluentvalidation.net/en/latest/conditions.html