In part one of this article, we looked at the data definition xml configuration option for the Validat data validation engine. In some cases, hard coding the data definitions is not the ideal choice. For example, your system might allow for dynamically build forms and you might want to dynamically assign validation rules to the form fields. One of the core concepts behind the Validat data validation engine is flexibility and the ability to fit into any scenario. To address this issue, Validat offers a complete API for creating and managing the data definition in addition to using the configuration XML.
Overview
The Validat API is designed with a series of functions for creating, updating, deleting, or listing validation rule mappings, data sets, data elements, and assertions. You can use the API to perform actions on an existing data set that was created via the configuration XML file discussed in part one, or you can start from scratch.
Lets break down the different capabilities available through the API.
Validation Rules
In the context of the data definition, validation rules are a mapping between the actual validation rule object and a rule name that can be referenced for each data element. As will become obvious as we progress through the API, there are a standard set of functions for managing validation rules as well as the other elements of the Validat data definition.
addRule(ruleName, validator, ruleArgs, ruleMsgs)
As the name suggests, this method simply adds a new validation rule to the data definition. It accepts all of the attributes of a validation rule – name, validator, arguments, and messages – and overwrites any existing validation rules with the same name.
addRuleXML(ruleXML)
This method adds a new rule, but does so via an XML snippet. This function is actually used when parsing the XML data definition, but can also be used directly, passing just the XML snipped for a validation rule.
getAllRules()
This method returns a structure, keyed by the validation rule name, with all validation rules in the data definition.
getRule(ruleName)
This method returns a structure, keyed by the validation rule attribute, with all of the data regarding the validation rule with the specified rule name.
remRule(ruleName)
This method removes a validation rule, based upon its rule name, from the data definition.
ruleExists(ruleName)
This method simply tests to see if a validation rule is defined in the data definition with the specified rule name.
Data Sets
As mentioned in the first part of this article, data sets are simply collections of data elements later to be connected with a given form, bean, or other collection of data to be validated. The following API functions are available for manipulating data sets programmatically.
addDataSet(dataSetName, transformer, overwrite)
This method simply adds a new data set to the data definition. It accepts all of the attributes of a data set – name and transformer – along with an optional overwrite argument. With data sets, the developer can decide not to overwrite an existing data set with the same name.
addDataSetXML(dataSetXML, overwrite)
This method adds a new data set, but does so via an XML snippet. This function is used when parsing the XML data definition, but can also be used directly, passing just the XML snipped for a data set. Additional, it also accepts an optional overwrite argument allowing the developer to decide not to overwrite an existing data set with the same name.
getAllDataSets()
This method returns a structure, keyed by the data set name, with all data sets in the data definition.
getDataSet(dataSetName)
This method returns a structure, keyed by the data set attribute, with all of the data regarding the data set with the specified data set name.
remDataSet(dataSetName)
This method removes a data set, based upon its data set name, from the data definition.
dataSetExists(dataSetName)
This method simply tests to see if a data set is defined in the data definition with the specified data set name.
Data Elements
Data elements represent individual pieces of data that are to be validated. Data elements may have one or more validation rules applied and are grouped together into data sets. The following API functions are available for manipulating data elements programmatically.
addDataElement(dataSetName, dataElementName, dataSetConnectionName, required, message, overwrite)
This method simply adds a new data element to the data definition. It accepts all of the attributes of a data element – parent data set name, data element name, required, message, and overwrite. With data elements, the developer can decide not to overwrite an existing data set with the same name. The dataSetConnectionName argument is optional and allows for chaining data sets and data elements. For example, if you were validating a person bean object, that person bean might have a property called homeAddress which was a pointer to an address bean object. By using this data set / element chaining, you can have a data element called address which actually points to an entire other data set containing all of the data elements that make up an address.
addDataElementXML(dataSetName, dataElementXML, overwrite)
This method adds a new data element to the specified data set, but does so via an XML snippet. This function is used when parsing the XML data definition, but can also be used directly, passing just the XML snipped for a data element. Additional, it also accepts an optional overwrite argument allowing the developer to decide not to overwrite an existing data element with the same name.
getAllDataElements(dataSetName)
This method returns a structure, keyed by the data element name, with all data elements in the specified data set.
getDataElement(dataSetName, dataElementName)
This method returns a structure, keyed by the data element attribute, with all of the data regarding the data element with the specified data element name in the specified data set.
remDataElement(dataSetName, dataElementName)
This method removes a data element, based upon its data set and data element name, from the data definition.
dataElementExists(dataSetName, dataElementName)
This method simply tests to see if a data element is defined in the specified data set with the specified data element name.
Data Assertion
Data assertions represent the link between a data element and a validation rule – in other words asserting that the validation rule evaluate to true for the data element. Data assertions may also apply at the data set level if they test multiple data elements. The following API functions are available for manipulating data assertions programmatically.
addAssert(dataSetName, dataElementName, rule, continueOnFail, args, dependencies, messages)
This method simply adds a new data assertion to the data definition. It accepts all of the attributes of a data assertion – applicable data set name, applicable data element name (optional), validation rule name, continue on fail, and optional collections of arguments, dependencies, and error messages. If an data set level assertion exists with the same name, it will not be overwritten – data set level assertions must be removed manually. If a data element level assertion exists with the same name, it will be overwritten.
The dependencies collection is for data set level assertions and maps one or more data elements to this assertion. For example, you have the need to test if a user account already exists in the system, but that test involves the user’s first name, last name, and email address. A single data assertion could be written with dependencies on all three of these data elements, in which case the value of all three data elements would be passed to the associated validation rule.
addAssertXML(dataSetName, dataElementName, assertXML)
This method adds a new data assertion to the specified data set and/or data element, but does so via an XML snippet. This function is used when parsing the XML data definition, but can also be used directly, passing just the XML snipped for a data assertion. If the data element name is specified, it is assumed the assertion is being added directly to that data element. If not, the assertion will be added at the data set level.
getAllAsserts(dataSetName, dataElementName)
This method returns an array containing all of the data assertions in the specified data set and/or data element.
getAssert(dataSetName, dataElementName, assertId)
This method returns a structure, keyed by the data assertion attribute, with all of the data regarding the data assertion with the specified assertion id in the specified data set and/or data element.
remAssert(dataSetName, dataElementName, assertId)
This method removes a data assertion, based upon its assertion id and the specified data set and/or data element from the data definition.
Additional Information
More information on Validat will be available on the Validat project site at http://trac.alagad.com/Validat as Validat gets closer to its first beta release. In the mean time, check out the Google groups site (http://groups.google.com/group/Validat) to ask any questions.
In the next set of Validat 101 topics, we will take a closer look at the validation rule and transformer objects that help make Validat so extensible. Until then …..