0

I have the following code :

  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
        
    }

How do I pass the X.KeyValue in WithMessage?, If there is an error it returns .WithMessage("KeyValue is mandatory but some values are missing") but how do I pass the actual value ? X contains X.KeyName and X.KeyValue

Note:

X.KeyValue is not a string.

      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");

unfortunately this prints only the first value. Is it a way to include only the error value?

I used this

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

but this didnot work.

   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;
        }
    }
   
3
  • Hi, any update?If my answer help you resolve your issue, could you please accept as answer? If not, could you please follow up to let me know? Thanks. Commented May 25, 2022 at 1:12
  • I have edited the question again. Please see to it Commented May 25, 2022 at 3:39
  • I suggest you can share the whole model and custom AbstractValidator. Commented May 25, 2022 at 5:20

1 Answer 1

2

Change your code like below:

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

Whole code:

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;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.