I currently have to validate custom Field Objects for my application. Simply put, each Field object consists of information about the validation for the field, as well as the value of the field. I am validating fields in bulk, so currently, I have a validation class, that has a method for each validation. For required fields, it looks something like this:
private void RequiredFields()
{
foreach (Field field in allFields)
{
if ((field.Required == true) && (field.Value == string.Empty))
{
field.isValid = false;
}
}
}
Now my problem is that I feel like I should a layer of abstraction to the validation, so instead of saying:
if ((field.Required == true) && (field.Value == string.Empty))
... I would add a validation class, to accept the values and turn it into this:
if (!validater.RequiredFields(field.Required, field.Value))
If I were to do this, it would allow me to reuse the validation class without use of the field objects, and it would also allow better unit testing... However, it seems like and unnecessary layer of abstraction and also somewhat repetitive... Keep in mind, this is the most simple of all the validation.
Suggestions?