Extend SiteValidator with custom rules
SiteValidator is a tool that can help you to maintain high quality of the code both during development and after delivery. I will show how to create your own tests to test exactly what you think is important.
SiteValidator has an API that enables partners and customers to add additional tests to the product. To create and add a new test you should start with creating a new class, the class should implement a specific interface and report errors of the correct type. The interfaces and the error class that should be used can be found in the assembly named Meridium.Web.Accessibility.
When our new test reports an error it should create a new object of the type ExternalValidationError. It is important that new tests is using this class for error reporting, there are others but those are meant for use internally in SiteValidator.
There are two interfaces that you can choose from when you implement a test class, IGeneralValidationRule and IXPathValidationRule.
IGeneralValidationRule
This interface should you choose if you want create a test based on the page as raw text. The interface contains one method and one property:
List Validate(string html, string inputNamespace);
Dictionary Settings { get; set; }
When it is time to evaluate the rule for a given page, then this method will be called with the entire html source code as a parameter and the namespace of the page (if it is a valid XHTML page, this is likely http://www.w3.org/1999/xhtml). The property will contain the keys that have been set for the rule in the web.config.
The method should return a list populated with ExternalValidationError objects containing the errors detected. If no error is found, the method may either return null or an empty list.
IXPathValidationRule
This interface should you choose if you want create a test based on the page as an XPathDocument. You will receive an already created XPathDocument that can be shared between several different tests, thus saving performance. The interface contains one method and one property:
List Validate(XPathDocument document, System.Xml.XmlNamespaceManager ns);
Dictionary Settings { get; set; }
In this case the method will be called with the parameter set to a document XPathDocument created from the whole page source.
So let's create a simple test to ensure that we did not use any inline css in our templates. (note that this is a demo and the code is untested and may contain errors)
public class InlineCssRule: IXPathValidationRule {
public Dictionary Settings { get; set; }
public List Validate(XPathDocument document, XmlNamespaceManager ns) {
var result = new List();
var navigator = document.CreateNavigator();
var iterator = XPathCache.Select("//*[@style]", navigator);
if (iterator.Count > 0) {
var error = new ExternalValidationError {
ExternalErrorName = "Inline-CSS",
ExternalErrorDescription = string.Format("The document uses the {0} style-attributes. Use external style sheets or style elements instead.", iterator.Count),
ExternalErrorTypeId = 10001
};
result.Add(error);
}
return result;
}
}
ExternalErrorTypeId property is used to identify a particular type of error. For example, all errors of type "inline-CSS" has the same error type id. It is important that every error type has a unique id. It must also be greater than 10 000 (the first numbers are reserved for internal use).
Complile your code and register the new test in web.config inside the rule node of the SiteValidator configuration section and you are good to go.
For EPiServer partners SiteValidator is free to use when developing a new website for a customer so feel free to download a copy and take it for a test run.



