相互依赖的链接规则
本文关键字:链接 规则 依赖 | 更新日期: 2023-09-27 18:14:54
我在我的项目中使用Fluent Validation。
在我的ViewModel中,我有一个字符串类型的属性,有效值只能是表示正整数的字符串。
因此,我创建了一个简单的IntegerValidator
,用于检查字符串是否可以解析为整数。这工作。
问题是,如何加上一个规则,它必须是一个正整数?我想使用现有的大于验证器,但将其链接到我的字符串属性的规则将其作为string
进行比较,而不是作为已解析的int
。如何做到这一点?
我想做的示例(注意ToInt()
):
RuleFor(x => x.BatchNumber).SetValidator(new IntegerValidator())
.ToInt().GreaterThan(0);
您可以使用自定义方法…
RuleFor(x=>x.BatchNumber).Must(BeAPositiveIntegerString);
private bool BeAPositiveIntegerString(string batchNumber)
{
// check both parse ability and greater than (once parsed)
}