多个属性的FluentValidation规则 [英] FluentValidation rule for multiple properties
本文介绍了多个属性的FluentValidation规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个FluentValidator,它具有zip和county等多个属性.我想创建一个规则,该规则具有两个属性,就像RuleFor构造
I have a FluentValidator that has multiple properties like zip and county etc. I want to create a rule that takes two properties just like a RuleFor construct
public class FooArgs
{
public string Zip { get; set; }
public System.Guid CountyId { get; set; }
}
public class FooValidator : AbstractValidator<FooArgs>
{
RuleFor(m => m.CountyId).Must(ValidZipCounty).WithMessage("wrong Zip County");
}
这可行,但我想将邮编和县名都传递到路e以进行验证.达到此目的的最佳方法是什么?
This works but I want to pass both Zip and county to the rue in order to validate. What is the best method to achieve this?
推荐答案
There is a Must
overload that also provides you with the FooArgs
object documented here. It allows you to easily pass both arguments into your method like this:
RuleFor(m => m.CountyId).Must((fooArgs, countyId) =>
ValidZipCounty(fooArgs.Zip, countyId))
.WithMessage("wrong Zip County");
这篇关于多个属性的FluentValidation规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文