Optional Validation and Required Values

Oct 26, 2010 at 5:31 PM

I have been using your validation framework for a little while now and two of the biggest pains I have found so far:

1. Optionally validating a field. For example I have an email address that is optional. I wish there was a way to skip validation methods if there is no value provided. 

    Currently I have to do this:

        If (!string.IsNullOrWhitespace(email)) Validate.That(email, "Email Address").IsEmail().IsShorterThan(100);

    I would prefer to do something like this:

        Validate.That(email, "Email Address").Optionally().IsEmail().IsShorterThan(100);

    This way the validation is "short circuited" and does not test the value if there is no value to test.

2. Testing for required string values is not easy since .IsEmpty() does not account for whitespace. My preference would be to have an .IsNullOrWhitespace() method so I can do this:

    Validate.That(stringValue, "Some Value").Not().IsNullOrWhitespace();

    Or even better:

    Validate.That(stringValue, "Some Value").HasValue();