Monday, July 16, 2007

Announcing NValidate

Yeah, so I bit the bullet. I decided to go ahead and port the parameter validation code to C# and make it open source. And I settled on a name. After an extensive search on the web to make sure the name wasn't taken, I chose the apt name of NValidate.

Yes, it's a play on words.

So I've registered the domain name (www.nvalidate.org) and will soon have the web site up for it. First thing I'll get out the door is the user documentation so that you can see what's coming.

The initial release will provide basic parameter validation tests for each of the following types in the Framework:

  • Boolean
  • Byte
  • Char
  • DateTime
  • Decimal
  • Double
  • IDbConnection
  • IDbTransaction
  • Int16 (Short)
  • Int32 (Integer)
  • Int64 (Long)
  • Object
  • Single
  • String

For those who haven't seen my earlier post, NValidate provides a way to streamline the code that tests method parameters for validity. It turns this:

protected int getPlus4(string zipCode)
{
const string ZipFormat = "\\d[5]-\\d[4]";
if (zipCode == null)
throw new ArgumentNullException("zipCode");
if (!((new Regex(ZipFormat)).IsMatch(zipCode)))
throw new ArgumentNullException("zipCode");

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


into this:

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


To ensure that the learning curve is small, the "interface" to NValidate is modeled after that of NUnit; that is, Validate.That is similar to NUnit's Assert.That. The difference is that Validate.That always throws an exception when the test fails, and the exception is always an ArgumentException (or one of its derivatives).


The tests supported by NValidate will be pretty exhaustive. I've got plans for a full suite of tests already planned. They're shown at the bottom of this post. If you can think of one that you do very frequently and it's not on the list, let me know, and I'll add support for it. This list of tests is based on the tests that I normally perform myself in software, plus a few that I gleaned from NUnit itself. I plan to expand the library down the road, adding more tests and more type support as demand for it increases. (That supposes, of course, that said demand actually exists.)


NValidate will support .NET 1.1 and 2.0; it will ship with compiled binaries and the full source code. Additionally, the license I select will permit its use for any reason, free of charge. If you find a bug in it, I would hope that you'll let me know so that I can fix it.



Proposed Tests for Initial Release of NValidate



  • Contains: String
  • DoesNotContain: String
  • DoesNotMatch: String
  • EndsWith: String
  • HasDay: DateTime
  • HasHour: DateTime
  • HasLength: String
  • HasMinute: DateTime
  • HasSecond: HasValidConnection
  • HasYear: DateTIme
  • IsBoolean: String
  • IsClosed: IDbConnection
  • IsEqualTo: All
  • IsFalse: Boolean
  • IsGreaterThan: Byte, Char, DateTime, Decimal, Double, Int16, Int32, INt64, Single, String
  • IsGreaterThanOrEqualTo: Byte, Char, DateTime, Decimal, Double, Int16, Int32, INt64, Single, String
  • IsInRange: Byte, Char, DateTime, Decimal, Double, Int16, Int32, INt64, Single, String
  • IsLessThan: Byte, Char, DateTime, Decimal, Double, Int16, Int32, INt64, Single, String
  • IsLessThanOrEqualTo: Byte, Char, DateTime, Decimal, Double, Int16, Int32, INt64, Single, String
  • IsNegative: Byte, Char, Decimal, Double, Int16, Int32, INt64, Single
  • IsGreaterThan: Byte, Char, Decimal, Double, Int16, Int32, INt64, Single
  • IsNotEqualTo: All
  • IsNotInRange: Byte, Char, DateTime, Decimal, Double, Int16, Int32, INt64, Single, String
  • IsNotNull: IDbConnection, IDbTransaction, Obect, String
  • IsNotOneOf: All
  • IsNull: IDbConnection, IDbTransaction, Object, String
  • IsNumeric: String
  • IsOneof: All except Boolean
  • IsOpen: IDbConnection
  • IsPositive: Byte, Char, Decimal, Double, Int16, Int32, Int64, Single
  • IsTrue: Boolean
  • IsValid: All
  • IsZero: Byte, Char, Decimal, Double, Int16, Int32, Int64, Single
  • Matches: String
  • StartsWith: String

3 comments:

Unknown said...

I've been using such a validation strategy for years! Would be very nice to have that in a framework. Would make a nice CodePlex project!

Unknown said...

After some thinking... It's more interresting to extend the Enterprise Library Validation Application Block. I'm using their validators individually to do parameter validation. Works very well :-)

Anonymous said...

When will NValidate be released?