如何在 fluentvalidation 的 WithMessage 函数中传递对象值

how to pass object values in WithMessage function in fluentvalidation

我有以下代码:

  RuleFor(record => record)
             .Must(WholeObject => Mandatory(WholeObject, keyValuePairs))
            .WithName( X => X.KeyName).WithMessage("KeyValue is mandatory but some values are missing")

         //Here X.KeyValue contains the value.
         //I want to pass this value on error

 private bool Mandatory(Object recObj, Object keyValuePairs)
    {
       //return true or false depeneds on the logic
        
    }

如何在 WithMessage 中传递 X.KeyValue?,如果出现错误 returns.WithMessage("KeyValue is mandatory but some values are missing") 但如何传递实际价值 ? X 包含 X.KeyName 和 X.KeyValue

注:

X.KeyValue 不是字符串。

      public class KeyValue
       {
          public List<string> Val { get; set; }
       }

    RuleFor(record => record)
  .Must(WholeObject => SizeOF(WholeObject, keyValuePairs))
            .WithName(X => X.KeyName).WithMessage(x => $"{x.KeyValue.Val[0]} is not in range(min, max) as defined");

不幸的是,这只打印了第一个值。是否可以只包含错误值?

我用过这个

 .WithName(X => X.KeyName).WithMessage(x => $" 
{x.KeyValue.Val.ToList().ForEach(s => s)} is not in range(min, max) as 
 defined");

但这没有用。

   private bool SizeOF(Entity.EigenData.Record recObj, IDictionary<string, Entity.EigenSchema.AttributeSet> keyValuePairs)
    {
        string strval = recObj.KeyName;
        Entity.EigenSchema.AttributeSet obj = keyValuePairs[recObj.KeyName];
        //if the size falls inbetween min and max
       
        
            return recObj.KeyValue.ISSizeWithinRange(obj);

       
      
        //string val = obj.KeyValue.ToString();

    }

     

    public static bool ISSizeWithinRange(this Validation.Entity.EigenData.KeyValue kv, Validation.Entity.EigenSchema.AttributeSet Obj)
    {

        try
        {
            if (kv.Val.Count > 0) //only if List<val> is available go inside the loop
            {
                foreach (string s in kv.Val)
                {
                    //foreach val check if its empty or null, if its empty or null then return false
                    bool True = String.IsNullOrEmpty(s);
                    if (True)
                    {
                        return true;

                    }
                    else
                    {
                        bool False = (Enumerable.Range(Obj.Size.Min, Obj.Size.Max).Contains(s.Length));
                        // if it contains within range then do nothing, return true at the end
                        //if it doesnot fall with in range then return false immediately. No need to check the entire set of values
                        if(!False)
                        {
                            return false;
                        }
                           
                    }

                }
                //if it contains some value then return true
                return true;
            }
            else
            {
                //List<val> count is zero
                return false;
            }
        }
        catch
        {
            return false;
        }
    }
   

像下面这样更改您的代码:

RuleFor(record => record)
         .Must(WholeObject => Mandatory(WholeObject,keyValuePairs))
        .WithName(X => X.KeyName).WithMessage(x => $"{x.KeyName} is mandatory but some values are missing");

完整代码:

public class Status
{
    public string Name { get; set; }
}
public class CustomValidator : AbstractValidator<Status>
{

    public CustomValidator ()
    {
        RuleFor(record => record)
         .Must(WholeObject => Mandatory(WholeObject,keyValuePairs))
        .WithName(X => X.Name).WithMessage(x => $"{x.Name} is mandatory but some values are missing");
        
    }
    private bool Mandatory(Object recObj, Object keyValuePairs)
    {
        //return true or false depeneds on the logic
        return false;
    }
}