I am creating some typed DataTables that I will need to perform validation on the rows and individual cells and have been trying to figure out the best way to design this that will provide flexibility for new validation methods. Some of the fields/columns can have more than one validation methods that needs to be checked.
String field is less than or equal to X (x can change depending on the data column)
String field contains ':' and each string between the colon is less than or equal to X and the entire string is less than Y
String field can not start with a number
Combination of three fields length in a data row can not be greater than X characters.
Date field must be between X and Y dates
Double field maximum of X decimal places
So for example, an EmployeeRow may have string fields of FirstName, MiddleInitial, LastName with maximum length of 25, 5, and 25, but with a total combined of 40 characters and a HireDate field that must be between 01/01/1901 and 12/31/2099.
What I have tried:
I was thinking of setting up a static class for each of the validation rules that returns a struct that has the validation results (boolean) and string message (why validation failed):
public static class ValidationRules
{
public static ValidationResult MaxLengthValidation(this string value, int maxLength){
public static ValidationResult MultiPartMaxLengthValidation(this string value, int maxPartLength, int maxTotalLength){
public static ValidationResult DateRangeValidation(this DateTime value, DateTime minDate, DateTime maxDate) {
}
I could then setup my DataRow to have a Validate function that would test each of the columns, as well as implement any multi column validation. Somehow this doesn't feel like it's the best approach, though. Are there any better designs or ways to set this concept up?