Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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):
C#
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) { // }
    // etc..
}


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?
Posted
Updated 6-Jul-17 10:37am

1 solution

You can use different approaches for different validations. When defining the data table I'd suggest using constraints whenever possible. This would ensure that the data in the data table is never invalid what comes to the constraint logic.

However, only a few checks can be done using constraints, such as mandatory, foreign keys, range checks etc. This means that you also need to code different validations logic. To react to problems as soon as possible you can validate the data upon column or row change. Have a look at Validate data in datasets[^]

The third category is more complex, especially circular validation that cannot be enforced all the time. In the middle of a modification you may have an invalid situation but in the end you must prevent such data from being entered into the system. This would mean that you may also need to do a separate validation before saving/committing the changes. This kind of validation typically must be fired only when the changes are being persisted so it often requires information from outside the data table (or set). For example you may need to fire this validation only when save button is hit or in some similar situation.
 
Share this answer
 
Comments
hpjchobbes 7-Jul-17 15:09pm    
Based on the way the user is going to be using my software, it needs to be able to keep 'invalid' data in the datatable, but be able to highlight the invalid data so the user can have a chance to correct the information. The data will be saved when the user presses a save button, which will happen after the user supposedly has fixed any invalid data. There is also some options to auto correct some of the data (i.e. truncate stings that are too long). Because of this I will probably be staying away from using most constraints. The article you linked seems to be the best that I am going to get, but it's beginning to look like I'll need to manually create a typed dataset instead of letting the designer do this as I want to be able to create validation columns.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900