Try our conversational search powered by Generative AI!

public IEnumerable<ValidationError> Validate() - IValidate<> How to return success for Validate method

Vote:
 

Hi

I've implemeted the interface IValidate. In my Validate method, I can return error like following:

yield return new ValidationError
{

..

}

I'm unable to return success. Following is the statement, like seen in many articles. It's not working.

return Enumerable.Empty();

Question:

How to return no error for Validate method??

#197958
Oct 17, 2018 21:01
Vote:
 

Hi @Martin,

Returning null or empty list should work and it means no error.

Could you please paste your complete code so can have a look.

Cheers

#197962
Oct 18, 2018 2:49
Vote:
 

Hi @Martin,

Returning null or empty list should work and it means no error.

Could you please paste your complete code so can have a look.

Cheers

#197963
Oct 18, 2018 2:49
Vote:
 

Hi Martin,

If you're using yield to return errors, you can return a success by using "yield break;" (assuming you haven't already added an error using yield) like the following:

        public IEnumerable<ValidationError> Validate(MyContentType instance)
        {
            if (string.IsNullOrEmpty(instance.MyProperty))
            {
                yield return new ValidationError
                {
                    ErrorMessage = "Fill in the property",
                    Severity = ValidationErrorSeverity.Error
                };
            }
            yield break;
        }
#197972
Oct 18, 2018 10:14
Vote:
 

Instead of using yield return, what can work is to create a List<ValidationError> and add errors if any. In the end when you return it, it either contains errors or nothing.

Because the collection should be very small so it's not different with List<ValidationError> or a yield return.

#197973
Oct 18, 2018 10:49
Vote:
 

it allocates! :)

#198020
Oct 19, 2018 4:50
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.