如何将两个或三个值传递给流利的验证必须功能?

how to pass two or three values to fluent validation Must function?

我的代码:

   public class MandatoryValidator : AbstractValidator<Entity.EigenSchema.AttributeSet>
{
    private string Keyvalue = string.Empty;
    public MandatoryValidator(string keyvalue)
    {
        Keyvalue = keyvalue;
        RuleFor(record => record.Mandatory).Must(Mandatory);
    }

    protected bool Mandatory(bool val)
    {
        if (val)
        {
            if(Keyvalue!=null || Keyvalue!="")
            {
                return true;
            }
            return false;
        }
        else
        {
            return true;
        }
        
    }
}

这将检查该字段是否为必填字段。

现在我需要一个带有多个参数的强制函数的函数,像这样..

      protected bool Mandatory(bool val, string strval, int val)
       {
            //strval = record.LocalUnique
            //val = record.Size
        }

你可以这样做

RuleFor(record => record.Mandatory).Must(mandatoryField => Mandatory(mandatoryField, Keyvalue, 012));

你也可以

RuleFor(record => record).Must(WholeObject=> Mandatory(WholeObject))
.WithName("Mandatory");
//.WithName(x => x.MandatoryFieldName) optional

//and now 
private bool MandatorChecker(MyRecordType obj)
{
     strval = obj.LocalUnique;
     val = obj.Size;
}

如果违反此规则,此人将使用您提供的名称。