要求至少有一个集合不为空
使用 FluentValidation,我需要要求 3 个集合中至少有 1 个不为空。这是我目前能想到的最好方法:
RuleFor(x => x).Must(x => x.Required.Any() || x.Ignored.Any() || x.Preferred.Any());
正在验证的类型定义为:
public record ReleaseProfileData(
string Name,
[JsonProperty("trash_id")] string TrashId,
bool IncludePreferredWhenRenaming,
IReadOnlyCollection<TermData> Required,
IReadOnlyCollection<TermData> Ignored,
IReadOnlyCollection<PreferredTermData> Preferred
);
是否有更“流畅”(惯用)的方式来表达这种类型的规则?如果是这样,您能举个例子吗?
Using FluentValidation, I need to require that at least 1 collection out of 3 is not empty. This is the best way I can think to do this at the moment:
RuleFor(x => x).Must(x => x.Required.Any() || x.Ignored.Any() || x.Preferred.Any());
The type being validated is defined as:
public record ReleaseProfileData(
string Name,
[JsonProperty("trash_id")] string TrashId,
bool IncludePreferredWhenRenaming,
IReadOnlyCollection<TermData> Required,
IReadOnlyCollection<TermData> Ignored,
IReadOnlyCollection<PreferredTermData> Preferred
);
Is there a more "fluent" (idiomatic) way of expressing this type of rule? If so, could you provide an example?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论