Skip Navigation Links.

Introduction

What is NValidate?

NValidate is a framework designed to streamline and standardize the code you write to validate method parameters. Its purpose is to make your code clearer and to help you reduce defects by making it easier to write parameter validation code so that you are more likely to do it consistently.

NValidate consists of a single .NET assembly (NValidate.DLL). All of the classes within the NValidate framework execute at runtime with your application.

Why Use It?

Parameter validation tends to be a hit-and-miss thing in software development. Given the tight schedules that many software projects are under, corners are often cut, and code is hastily written. Given that parameter validation code tends to be verbose, it’s often quickly discarded in favor of getting the logic written.

The problem is that this means that the bugs due to invalid parameters often aren’t found until the product reaches the testing team or the customer. Validating parameters to methods can significantly reduce those types of defects when it is done consistently; but you have to provide a way to do that that is easy and doesn’t significantly impact the application’s performance or memory consumption.

For example, assume that the following code sample expects that it’s string argument contains a ZIP code, that must contain a +4 component. The function then converts the +4 code to an integer. In its worst implementation, it might be written like this:

protected int getPlus4(string zipCode)
{
   return int.Parse(zipCode.Substring(6, 4));
}

This method will fail if any of the following conditions is true:

  • zipCode is null
  • zipCode is less than 7 characters in length
  • The string starting at position 6 can’t be parsed into an integer

The method’s correctness is improved by adding validation code as follows:

protected int getPlus4(string zipCode)
{
   if (zipCode == null)
      throw new ArgumentNullException("zipCode");
   if (!((new Regex("\\d[5]-\\d[4]")).IsMatch(zipCode)))
      throw new ArgumentNullException("Not
          a valid ZIP+4 value.", "zipCode");

   return int.Parse(zipCode.Substring(6, 4));
}

However, as you can see, the validation code is verbose, and is not particularly human-readable. NValidate takes that validation code and reduces it to this:

protected int getPlus4(string zipCode)
{
   Demand.That(zipCode, "zipCode").IsNotNull().Matches("\\d[5]-\\d[4]");
   return int.Parse(zipCode.Substring(6, 4));
}

This code is easier to read (although it would be easier to read if the regular expression were replaced with a constant). Further, it takes fewer lines of code to validate the parameter.

NValidate is designed to exploit IntelliSense. The Demand class is a factory, and the That method is heavily overloaded to return strongly-typed parameter validators based on the type of the parameters passed to it. This allows IntelliSense to provide you with a list of all the tests available to the parameter validator for the parameter’s data type. You can browse the tests available to you, which helps facilitate on-the-fly review of the anticipated tests for the parameter.

Another advantage of NValidate is that the number of validation tests supported by the framework is extensive. NValidate supports each of the primitive data types (Boolean, Byte, Char, Decimal, Double, Int16, Int32, Int64, Object, SByte, Single, UInt, Uint32, and UInt64), as well as the DateTime, Enum, and String types. The initial release also supports some commonly used types including Array, List, IDbConnection, and IDbTransaction. Support for other types will be added in the future. For each type that NValidate supports, you do not have to manually write the code that performs the test and throws the exception.