Creating Custom Validators with NValidate
Important: The CustomValidatorBase class mentioned in this article
was added after the initial alpha release (v0.0.0.10177). The class will be available
in the next release.
The topic is remains relevant however: the base class only provides a base implementation
of the ErrorMessage property in its current state. To benefit from the article during
alpha testing, simply implement the IValidatable interface directly.
Introduction
The initial release of NValidate supports custom validators through the use
of the IValidatable interface. In this article, we'll look at how to write
your own tests and have NValidate execute them.
A custom validator must implement the IValidatable interface. To simplify
your work, NValidate provides the CustomValidatorBase class, which provides
the base implementation of the interface for you. All you should have to do is implement
the constructor and Validate method.
The sample below shows a basic validator that ensures that a string is not null,
not empty, and matches a ZIP+4 pattern:
namespace Validation
{
public class ZipPlus4Validator
: CustomValidatorBase
{
public ZipPlus4Validator (string value)
: base()
{
m_value = value;
}
public bool> Validate()
{
// Clear the error message; set it if a failure occurs.
// If the error message is anything but an empty string
// by the time we reach the bottom, the test has failed.
m_errorMessage = string.Empty;
if(null == m_value)
m_errorMessage = "Null values not permitted.";
else if(string.Empty == m_value)
m_errorMessage = "Empty strings not permitted.";
else if(!(new Regex(expression).IsMatch("\\d[5]-\\d[4]")))
m_errorMessage = "Not a valid ZIP+4 value.";
return (string.Empty = m_errorMessage);
}
private string m_value = string.Empty;
private string m_errorMessage = string.Empty;
}
}
Note that this is a class prepared for demonstration purposes only; you'll want
to do something far more robust in your own code.)
Using Your Custom Test
Once you've created your custom test class, all you have to do is pass it
to NValidate's
Demand.That method. For this example, we'll assume you're validating
a ZIP code from Visual Basic code:
Public Sub InsertZipCode(ByVal zipCode As String)
Demand.That(New ZipPlus4Validator(zipCode), "zipCode").IsValid()
' Continue processing
End Sub
That's it. When your InsertZipCode method is called, Demand.That
will create a new custom validator instance and pass the zipCode parameter
to it. InsertZipCode will then invoke the IsValid
method on it, which will then invoke the custom validator's Validate
method. If any test within Validate fails, Validate
sets the ErrorMessage propety and returns False;
otherwise, it simply returns True.
When the Validate method on your custom validator has finished
processing, IsValid examines the return value. If the return value
is False, an ArgumentException is thrown, using
the ErrorMessage property and the ParameterName
(in this case, "zipCode").
An Important Note
Note that if a custom validator fails, NValidate can only throw a generic ArgumentException,
but the error message is determined by your custom validator class. For this reason,
it's imperitive that your class correctly set the ErrorMessage property at runtime
and return the appropriate boolean value.
|