Q: How to effectively output all the error messages after checking all the rules? Recently, I use C# to develop a unity editor window.
There is one of feature as follow:
When user input the data, the system will check the data by the rules which are defined by programmer.
There are two methods I am using now, as following :
The method 1 :
if(the data doesn't meet the rule 1 && the data meets the rule 2) {  
    // Output error message  
    Debug.log("The data doesn't meet the rule 1");  
}  
if(the data meets the rule 1 && the data doesn't meet the rule 2) {  
    // Output error message  
    Debug.log("The data doesn't meet the rule 2");  
}  
if(the data doesn't meet the rule 1 && the data doesn't meet the rule 2) {  
    // Output error message  
    Debug.log("The data doesn't meet the rule 1 and rule 2");  
}  

The method 2 :
String errorMessage = "The data doesn't meet";

if(the data doesn't meet the rule 1) {
    // save the error message
    errorMessage += " rule 1";
}
if(the data doesn't meet the rule 2) {
    // save the error message
    errorMessage += " rule 2";
}

Debug.log(errorMessage) // errorMessage = "The data doesn't meet rule 1 rule 2"

I think the method 2 is better than method 1.
Now, I define a lot of rules. (e.g. over 50 rules.)
If I use method 1 or method 2, I need to write a lot of if-else statement.
I want to know if method 2 is a good method?
And, is there any other method would be the best?
Thanks in advance!

A: Create an interface and give it a method like ValidationResult Validate(TWhatever objectAsInput), create a validator for each validation, register them in a collection somewhere, then loop over the collection of validators and concatenate their output.
Something like this:
var validationResults = new List<ValidationResult>();

foreach (var validator in validators)
{
    validationResults.Add(validator.Validate(yourObject));
}

var errorMessages = validationResults.Where(v => !v.Success).Select(v => v.ResultMessage);
var validationResult = string.Join(", ", errorMessages);

This makes it extremely trivial to add or remove validators and change the validation logic without having to scroll through 50 if statements to find the validation you want to alter.
