MrKWatkins

Fluent Argument Validation Part 5 - Version One

So that's it for fluent argument validation for the time being. I've got enough done to spit out version one, which is more than enough for my current needs. Afraid you don't get the source just yet... I plan to host the source on CodePlex eventually but I might just wait for version two for that...

"Version two?" I hear you say. "But what is wrong with version one?" Nothing, really. It suits me quite well. However whilst writing these articles (I'd actually written the library several months before...) I've thought of several ways to improve the thing...

Improve Number Chains

I touched on this one in part three. Currently the following is perfectly acceptible:

public static void SomeMethod(int number)
{
    Validate.Argument(number, "number")
        .IsGreaterThan(5)
        .IsGreaterThan(3)
        .IsGreaterThan(3);

Now are you ever going to validate a number is greater than another number twice? Surely you'd just validate it against the higher of the two. Therefore should I allow more than one call to IsGreaterThan in a validation chain?

Okay, you might be validating against variables and therefore have to validate against two variables to check they are greater than both. But in this case I think we need a different method anyways because one would want the exception method to express that the argument has to be greater than the variable rather than the current value of the variable. For example the following code:

public int SomeProperty
{
    get
    {
        return 5;
    }
}

public static void SomeMethod(int number)
{
    Validate.Argument(number, "number")
        .IsGreaterThan(SomeProperty);

would give the error message "Value must be greater than 5." A more useful message would be "Value must be greater than SomeProperty.". So perhaps we need another set of methods that allows you to specify something (other than a constant) to validate against?

Improve The Fluent 'Sentences'

The English made by reading the Fluent validation could be a bit better. For example the following:

Validate.Argument(value, "value")
    .IsNotNull()
    .IsNotEmpty()
    .StartsWith("Something");

would sound better as:

Validate.Argument(value, "value")
    .IsNotNull()
    .OrEmpty()
    .AndStartsWith("Something");

Default Tolerance

Unless you specify a tolerance then currently the validation methods for imprecise numbers don't use a tolerance which basically means they're not very useful; in general they will fail for some valid cases. NUnit's constraint model, which is a great example of a good fluent interface, has a default tolerance to use in imprecise comparisons. I think my library should do that too...

More Than Arguments

I'd like to add more validation than just arguments, e.g. type parameter validation, IO validation, XML validation, etc..

Anyways...

I'm sure I'll think of lots more for version two. But in the meantime version one is finished. Have a look at parts one, two, three and four if you haven't already. Feel free to download the library and use it in your own projects. If you do find the library useful then please ping me an email with details of the project you used it on and any ideas you have for improving it. And if you use it to make loads of cash then feel free to donate. No, in fact, feel compelled.