.NET · · 1 min read
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!
About Tore
I'm Tore, a Microsoft .NET MVP and independent .NET consultant and trainer based in Sweden. I help development teams sharpen their .NET and ASP.NET Core skills through practical workshops and hands-on coaching, covering everything from C# fundamentals to advanced application architecture. Explore my workshops, presentations and services at tn-data.se and connect on LinkedIn.

Comments
Nothing yet. Say the first thing.
Sign in to join the conversation.