Our internal validation library TNValidate is now available as a project on GitHub. We released this as open source because we wanted to let others take part in our internal libraries. The library is written in C# and uses a “Fluent Interface” to define the validation rules.
By using TNValidate, we have achieved a uniform way to write our validation logic, which in turn has reduced the number of bugs and made our code more readable.
Using TNValidate is very simple:
// Instantiate validator.
var Validate = new Validator(ValidationLanguageEnum.English);
// Basic validation.
Validate.That(Email, "Email address").IsEmail();
// Chaining a couple of rules.
Validate.That(Name, "Name").IsLongerThan(3).IsShorterThan(100);
// Supply a custom error message.
Validate.That(Age, "Age").IsGreaterThanOrEqual(13,
"You must be older than 13 to sign up");
// Check if validation succeeded.
if (Validate.HasErrors())
{
// Show errors (Validate.ValidatorResults can be data-bound).
foreach (var Error in Validate.ValidatorResults)
Console.WriteLine(Error.ValidationMessage);
}
else
{
// Was fine, continue...
// ...
}
Head over to GitHub and give it a try!