A .NET Attribute based validation framework for Model and Domain classes
Required:BOOL, checks if the field is required (not empty or null).MaxSize:INT, checks the maximum length of the field as string.MinSize:INT, checks the minimum length of the field as string.MaxValue:INT, checks the maximum integer value allowed in the field.MinValue:INT, checks the minimum integer value allowed in the field.AllowedInputType:ENUM, checks for the data format as string.
- Numeric, Only numbers.
- Alphanumeric, Letters, numbers, and symbols.
- Alphabetic, Only letters.
- Email, Email pattern.
- URL, Web URL patter.
- Any, Any character or pattern is accepted.
The project was developed and tested using Visual Studio 2013 and .NET Framework 4.5.
public class TestModel
{
[Validation(Required = true, MaxSize = 100, MinSize = 0, AllowedInputType = InputType.Any)]
public string Field1 { get; set;}
[Validation(Required = true, MaxValue = 10, MinValue = 1, AllowedInputType = InputType.Numeric)]
public double Field2 { get; set;}
[Validation(AllowedInputType = InputType.Email)]
public string Field3 { get; set;}
}
// After loading the data into the Model in your Controller or ViewModel class:
static void Main(string[] args)
{
var engine = new ValidationEngine();
var testModelInstance = LoadTestModelWithData();
engine.Validate(testModelInstance);
foreach (var prop in engine.Error)
foreach (var msg in prop.Value)
Console.WriteLine(string.Format("{0} {1}", prop.Key.Name, msg));
Console.ReadKey();
}