如何链接FluentApi验证



我对FluentPiValidation有问题。

示例:

RuleFor(x => x.IdentificationNumber)
.Matches("^\d{8}$").When(x => x.IsLegalType).WithMessage("This validation occurs only if this row is present, but not if other rows below are presented.")
.Matches("^\d{13}$").When(x => !x.IsLegalType).WithMessage("This validation always works")
.Must((model, identificationNumber) => BeUniqueForIdentificationNumber(model)).WithMessage("This Validation Always works")
.NotNull().WithMessage("This validation always works!");

标志IsLegalType工作正常,但由于某种原因,当我将其他链放在后面时,第一个链块验证不起作用。

如果我评论下面所有的链,并只放:

RuleFor(x => x.IdentificationNumber)
.Matches("^\d{8}$").When(x => x.IsLegalType).WithMessage("This validation occurs only if this row is present, but not if other rows below are presented.");

我认为我需要以更好的方式改变链接。有什么提示吗?

您希望为每个验证场景创建一个新的Rule

RuleFor(x => x.IdentificationNumber)
.Matches("^\d{8}$").When(x => x.IsLegalType).WithMessage("This validation occurs only if this row is present, but not if other rows below are presented.");
RuleFor(x => x.IdentificationNumber)
.Matches("^\d{13}$").When(x => !x.IsLegalType).WithMessage("This validation always works");
RuleFor(x => x.IdentificationNumber)
.Must((model, identificationNumber) => BeUniqueForIdentificationNumber(model)).WithMessage("This Validation Always works");
RuleFor(x => x.IdentificationNumber)
.NotNull().WithMessage("This validation always works!");

相关内容

  • 没有找到相关文章

最新更新