流利的验证器:如何验证字符串在另一个字段是从枚举的特定值时不为空的
我需要仅在消息
type 是
我尝试使用必须
必须
时,并且验证在类型为其他
,但是当类型为第一个
时,我接收“未满足指定条件的'消息'。”,
enum Types
{
First, Second, Other
}
class MyRequest
{
public string Message { get; set; }
public Types Type { get; set; }
}
public MyRequestValidator()
{
RuleFor(x => x.Type)
.IsInEnum();
RuleFor(x => x.Message)
.Must((model, field) =>
{
if (model.Type == Types.Other)
{
return true;
}
return false;
})
.NotEmpty()
.WithMessage("Message is required when type is Other")
.MaximumLength(10);
}
任何想法如何当另一个道具是枚举的特定值时,验证字符串?
I need to add validation for Message
only when Type
is Other
I tried to use Must
, and validation works when type is Other
, but when type is First
I receive "The specified condition was not met for 'message'.",
enum Types
{
First, Second, Other
}
class MyRequest
{
public string Message { get; set; }
public Types Type { get; set; }
}
public MyRequestValidator()
{
RuleFor(x => x.Type)
.IsInEnum();
RuleFor(x => x.Message)
.Must((model, field) =>
{
if (model.Type == Types.Other)
{
return true;
}
return false;
})
.NotEmpty()
.WithMessage("Message is required when type is Other")
.MaximumLength(10);
}
Any ideas how to validate a string when another prop is a specific value from an enum?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来当
SaeedEsmaeelinejad
提出时解决了我的问题looks like When proposed by
SaeedEsmaeelinejad
soled my issue