I have a requirement for a validator class to run a series of validation methods which each return a error code. Depending on the severity of the error, I may want to abort the rest of the validations. I could check the returned error code after each call and not perform the remaining validations but this would add a lot of duplicated clutter to the main ‘driving’ routine.
So, I wrote the following helper routine that handles this for me…
public IList<Error> ValidateSet(InputType input,
params Func<InputType, IList<Error>>[] validations)
{
var errors = new List<Error>();
foreach (var validation in validations)
{
errors.AddRange(validation(input));
if (AbortValidations(errors))
break;
}
return errors;
}
This is called as per below and works great…
var errors = new List<Error>();
errors.AddRange(
ValidateSet(input,
ValidateX,
ValidateY,
ValidateZ)
);
The problem I have now is that some validations require additional parameters other than one parameter of InputType.
Is there any way I could keep this same useful structure? Note that I can’t simply pass in the results of each validation as I don’t want to run the remaining validations if an earlier one returns an ‘abort’ error code (the validations load data so this is a performance requirement).
Thanks,
John
If you know the additional parameters at the time of calling
ValidateSet()then you can do something like the following