Creating custom validation rules for Visio 2010


In our last post, we discussed Diagram Validation—a new Visio 2010 feature which allows users to check their diagrams for common errors and allows companies to ensure that employees are following certain diagramming standards. In this post, we’ll talk more about how validation rules are stored and how to create custom validation rules.

With Visio 2010, we provide diagram validation support for Basic Flowchart, Cross Functional Flowchart, Microsoft SharePoint Workflow and Business Process Modeling Notation diagrams. We also provide an extensive validation API for creating custom validation rules. The supported functionality includes adding and deleting rules and rule sets, directly manipulating validation issues, triggering validation, and detecting the validation event. This support allows companies to develop custom validation rules to meet their own needs.

At a high level, validation rules belong to rule sets, such as the Flowchart and BPMN rule sets. To see which rule sets are available in your current diagram, click the Check Diagram pull-down menu on the Process tab, and then click Rules to Check. Rule sets with a checkmark beside them are active—rules from these sets will be evaluated during validation. Clicking on a rule set will toggle it between active and inactive.

RulestoCheck

There are two main approaches to creating custom validation rules and rule sets.

1. You can write validation rules and validation logic that will be stored in a custom Visio template. These rules are automatically available to users that create diagrams based on this template. When a user clicks Check Diagram on the Process tab, Visio uses the validation logic provided to determine whether the diagram has issues. In Visio 2010, this technique is used for the Basic Flowchart, Cross Functional Flowchart and Business Process Modeling Notation templates.

2. You can also write validation logic in code and deploy this logic as part of a Visio solution. For this approach, the solution code listens for the appropriate RuleSetValidated event and uses its own logic to determine the list of issues to add to the document. In Visio 2010, this technique is used for the Microsoft SharePoint Workflow template.

In both cases, the validation rules and corresponding rule sets are stored inside your Visio file. To see how these rules are stored, you can save a Visio file with validation rules as an XML Drawing (*.vdx) and open the XML Drawing in an XML editor. A search using the keyword “validation” will lead you to the XML related to validation. In the image below, a section of the XML for the Flowchart rule set is shown.

ValidationXML

Notice that each rule in the Flowchart rule set contains a RuleFilter and RuleTest property. These properties define the logic that Visio uses to determine whether a rule applies to a particular Visio object, and whether the rule is satisfied by the object. The syntax for each logic expression is the same as a ShapeSheet expression, but the allowable functions differ slightly. You can see many examples of valid RuleFilter and RuleTest expressions in our Flowchart and BPMN rule sets.

If you save a Microsoft SharePoint Workflow diagram as an XML Drawing (*.vdx) and open the XML Drawing in an XML editor, you will see that there is no logic in the RuleFilter and RuleTest expressions. In this case, the solution contains code with the validation logic. The SharePoint Workflow solution listens for the SharePoint Workflow RuleSetValidated event, and, when this event occurs, it scans the diagram to determine if there are issues. When an issue is found, it uses the validation API to add the issue to the document. Visio automatically displays these issues in the Issues Window.

ValidationXML-2

Although you can modify the validation properties directly in the XML, we suggest that developers use the validation API to manipulate rules programmatically. You can use Visual Basic for Applications, accessible through the Developer tab, and its Object Browser to view the validation API. The key objects, methods and properties for validation are shown below.

ValidationAPI

There is a lot more that can be said about creating custom validation rules, but hopefully this post gives you a good sense of the options available for using Diagram Validation. The following list summarizes these options.

1. End-users and companies can use the default rules provide in Visio 2010 to check their diagrams for issues and ensure diagram consistency. Out-of-the box support is included for Basic Flowchart, Cross Functional Flowchart, Microsoft SharePoint Workflow and Business Process Modeling Notation diagrams.

2. Developers can extend the out-of-box support by creating custom templates with validation rules. These rules are available to users who create diagrams from these templates.

3. Developers can create Visio solutions that listen for the validation event. When the Check Diagram button is clicked, the solution can run code to validate complex logic and use the validation API to add issues to the list of issues displayed in the Issues Window.

4. Developers can create Visio solutions that trigger validation from within their code. This allows solutions to make use of validation without relying on a click of the Check Diagram button. For example, the Microsoft SharePoint Workflow template triggers validation when a user clicks the SharePoint Workflow button Export on the Process tab.

We’re interested to hear your feedback, so use the Send a Smile feedback tool or comment on this post to let us know what you think.