- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
亲爱的我正在尝试使用 SetCollectionValidator 验证对象列表并且列表计数可能有 0 个对象或更多对象所以验证返回错误直到列表没有这样的项目
public class SCRequest
{
public List<Attachment> Attachments { get; set; }
}
public class Attachment
{
public int AttachmentId { get; set; }
public string Name { get; set; }
public string FileType { get; set; }
public string FilePath { get; set; }
public string FileUrl { get; set; }
}
现在为了验证 ScRequest
,我执行以下操作
public SCRequestValidator()
{
RuleFor(request => request.Attachments)
.SetCollectionValidator(new AttachmentValidator());
}
为了验证附件,我执行以下操作
public AttachmentValidator()
{
RuleFor(x => x.FileUrl)
.NotNull()
.WithMessage(ErrorMessage.B0001)
.NotEmpty()
.WithMessage("Not Allowed Empty");
}
当附件列表有 0 个对象时,我得到错误不是 Not Allowed Empty
,我的问题是我只想在列表有值时验证它。
我该如何解决?
最佳答案
您可以使用 When() 将规则/验证器设置为仅在某些情况下被调用。在您的示例中,代码将类似于:
public SCRequestValidator()
{
When(request => request.Attachments.Any(), () =>
{
RuleFor(request => request.Attachments)
.SetCollectionValidator(new AttachmentValidator());
});
}
因此,如果没有附件,则不会设置 CollectionValidator。
关于c# - 流利的验证 SetCollectionValidator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37408461/
我正在尝试将fluentd与elasticsearch连接起来,并且在启动td-agent服务时遇到此错误。 td-agent.log: 无法与Elasticsearch通信,重置连接并重试。连接被拒
所以我有一个案例,布局已经变得更加复杂。有一些常见的东西,比如 @section styleIncludes{ ... } ,然后是其他部分,这些部分定义了每个页面可以选择(但几乎总是)指定的各种内容
我刚刚看到一个巨大的 Java 正则表达式,它让我对一般正则表达式的可维护性有所思考。我相信大多数人——除了一些糟糕的 perl 贩子——都会同意正则表达式很难维护。 我在考虑如何解决这种情况。到目前
我有一个 12 秒长的 audio.mp3 文件video.mp4 的长度为 60 秒。 我需要在视频的第 40 秒插入audio.mp3。 如何使用 Node-Fluent-ffmpeg 做到这一点
我正在使用 NHibernate + Fluent 来处理我的应用程序中的数据库。到目前为止,我一直在使用 SessionSource 来创建我的 ISession 对象。我现在对来自 NHibern
我在 Java 控制台应用程序中使用 Apache HttpClient 4.5(具有流畅的界面)。我注意到,它的默认超时值似乎是无限的,但我必须为我发送的请求使用非无限的超时值。我想对所有请求使用相
这是我的路线: router.get("answers","delete", Int.parameter) { req -> Future in let answerID = try
我有随机“ session 已关闭!” Autofac 和 Fluent nHibernate 的以下配置错误: 全局.asax.cs: builder.Register(x => new NHibe
我是一名优秀的程序员,十分优秀!