The amazing adventures of Doug Hughes

A couple of weeks ago, we introduced an open source project we have been working on at Alagad called Validat. If you missed the announcement, Validat is a data validation engine for ColdFusion applications. Validat is designed such that it can be dropped into any application with a minimal amount of customization, and perform any data validation needs for that application.

As the documentation is being updated for Validat, I wanted to make a few postings hitting on some of the big concepts behind Validat. First on that list is what is called data definitions.

Intro to Data Definitions

So, what is a data definition? Part of the problem with developing a validation engine that works for any application is that every application has different data in different formats (forms, beans, etc.) and validation rules to be applied to that data. In order to address this, the Validat data validation engine makes use of a data definition that maps various validation rules to specific data elements.

Data Definitions via XML

The Validat data validation engine allows you the developer to configure the data definitions in two ways – one of which is an XML configuration file. The Validat XML configuration file is broken into two parts – validation rules and data sets.

Validation Rules

The validation rules segment of the XML configuration defines the validation rules that can later be applied to various data elements. An example configuration snippet looks something like this:

     <!– validation rule definitions –>

        <rule name="alpha" validator="validateAlpha" />
        <rule name="alphaNumeric" validator="validateAlphaNumeric" />
        <rule name="length" validator="validateLength">
            <arg name="min" value="0" />
            <arg name="max" value="100" />

The root element is validationRules and contains one or more rule child elements. Each rule element has a name for reference to later on and a pointer to a validator bean. This validator bean name must match a bean that can be requested from the ColdSpring bean factory (by default).

In addition, default arguments may be passed to the validation rule. Default arguments could be things like a min and max value as shown above, a minimum age, or any number of other possibilities. These default values may also be overridden later on in the dataset definition as well.

Data Sets

The data sets segment of the XML configuration defines one or more data sets (who would have thought!). A data set is simply a collection of data elements. A data set might represent a collection of form fields that will be validated together or it might represent a bean object and its associated properties. By defining data elements in collections, you may apply validation rules not only to individual data elements, but also to the data set as a whole. An example of a data set definition is shown below:

     <!– data set definitions –>

        <dataSet name="user">

            <dataElement name="firstName" required="true" message="errors.validation.user.firstName.required" >
                <assert rule="length" >
                    <!– optionally, additional arguments can be provided to the validator function –>
                    <arg name="min" value="1" />
                    <arg name="max" value="100" />
                    <message name="invalid" value="errors.validation.user.firstName.invalidLength" />
            <dataElement name="middleName" required="false" >
                <assert rule="length" >
                    <arg name="min" value="1" />
                    <arg name="max" value="100" />
                    <message name="invalid" value="errors.validation.user.middleName.invalidLength" />
            <dataElement name="lastName" required="true" message="errors.validation.user.lastName.required" >
                <assert rule="length" >
                    <arg name="min" value="1" />
                    <arg name="max" value="100" />
                    <message name="invalid" value="errors.validation.user.lastName.invalidLength" />



Every data set in XML configuration file must have a uniquely defined name and contains a collection of child data elements. The name data element was chosen instead of data field or other term because of the flexible nature of a data element. A data element is not limited to a form field. Instead, if you are validating a bean, a data element could actually be a property of that bean. If you have a structure of data that needs validating, the data element could represent a key/value pair in that structure. Flexibility is the name of the game.

For every data element, a unique name is defined (within the data set) and a required value flag is set. If the required value flag is true, then a message attribute must also be provided. The message attribute value is what is returned in the event that the data element is required and does not contain a value. In the example above, the message attribute value is a resource bundle identifier for localization, but it could easily be any text string.

For every data element, there are one or more child assertion elements. An assertion is simply a validation rule being applied to that data element. The rule attribute of the assert element specifies the name of the validation rule mapping from the validation rules section of the XML configuration file above. In this example, we again show how arguments can be passed to the validation rule as well as a message to be returned in the event that the assertion fails. An assertion may run multiple tests on a given data element, so multiple messages may be specified, each with a unique name.

Up Next …. Programmatic Data Definitions

In some cases, having a hard coded data definition via XML is not the best route to go. For example, if a form is dynamically built, then it is possible that the data validation rules would be dynamically assigned as well. Validat takes this into consideration as well with a comprehensive API allowing the developer to programmatically create and update a data definition. In the next article, we will take a look at this API and what it offers the developer.

Tag Cloud

%d bloggers like this: