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

我的代码:

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
        }

回答1

你可以这样做

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;
}

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

相似文章

c - c 程序处理 multithreading

我的c程序需要帮助,我已经完成了大部分,但还有一些问题。该计划是关于**探索进程和线程之间的同步。**在一个程序中给定了三(3)个流程,这些流程协同工作以解决生产者消费者问题:2个过程是“生产者”,每...

随机推荐

最新文章