Validators

Validators are used to check whether database model matches customs rules, like naming rules. A rule a class implementing the MDValidator interface.

Configuration

Validator configuration can be expressed in two formats: XML and properties.

Each validation rule is an instance of a validator class. Each instance of validator must have a unique name to identify it. Validator properties can be injected with values. There can be multiple instances of the same validator class (with different properties values)

XML Format

<?xml version="1.0" encoding="UTF-8"?>
<dbtb:validators xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:dbtb="http://dbtoolbox.sourceforge.net/xml/validator">
    <dbtb:validator name="ValidatorName" class="net.sourceforge.dbtoolbox.validator.ValidatorClass">
        <dbtb:property name="validatorProperty">Validator Property Value</dbtb:property>
        <dbtb:property name="anotherValidatorProperty">Another Validator Property Value</dbtb:property>
    </dbtb:validator>
    <dbtb:validator name="AnotherValidatorName" class="net.sourceforge.dbtoolbox.validator.AnotherValidatorClass"/>
</dbtb:validators>

Properties Format

validator.ValidatorName.class=net.sourceforge.dbtoolbox.validator.ValidatorClass
validator.ValidatorName.validatorProperty=Validator Property Value
validator.ValidatorName.anotherValidatorProperty=Another Validator Property Value

validator.AnotherValidatorName.class=net.sourceforge.dbtoolbox.validator.AnotherValidatorClass

Basic validators

DB Toolbox comes with some basic validation rules

Validation rules hierarchy

net.sourceforge.dbtoolbox.validator.NamePatternElementMDValidator

Checks that element name matchers regular expression.

elementClass Target model class (subclass of ElementMD)
Required.
elementMatcher Groovy expression to include/exclude elements from validation
nameRegEx Java Regular expression
Required.

net.sourceforge.dbtoolbox.validator.NameLengthElementMDValidator

Checks that element name length is between min and max. This can be done with a regular expression (see above) as well.

elementClass Target model class (subclass of ElementMD)
Required.
elementMatcher Groovy expression to include/exclude elements from validation
min,max Minimum and maximum name length

net.sourceforge.dbtoolbox.validator.ForbiddenNameElementMDValidator

Checks that element name is not in a list of forbidden names (avoid usage of reserved keywords).

elementClass Target model class (subclass of ElementMD)
Required.
names Coma separated, list of forbidden words
elementMatcher Groovy expression to include/exclude elements from validation

net.sourceforge.dbtoolbox.validator.ColumnCountElementMDValidator

Checks that column count is between min and max.

elementClass Target model class (subclass of ElementMD and ColumnSet: TableMD, PrimaryKeyMD...)
Required.
elementMatcher Groovy expression to include/exclude elements from validation
min,max Minimum and maximum value

net.sourceforge.dbtoolbox.validator.PrimaryKeyTableMDValidator

Checks that table has primary key.

elementMatcher Groovy expression to include/exclude elements from validation

net.sourceforge.dbtoolbox.validator.PrimaryKeyTableMDValidator

Checks that table has primary key.

net.sourceforge.dbtoolbox.validator.IndexPrimaryKeyMDValidator

Checks that primary key matches an index.

elementMatcher Groovy expression to include/exclude elements from validation

net.sourceforge.dbtoolbox.validator.ReferencesPrimaryKeyForeignKeyMDValidator

Checks that foreign key references a primary key.

elementMatcher Groovy expression to include/exclude elements from validation

net.sourceforge.dbtoolbox.validator.ReferencesIndexForeignKeyMDValidator

Checks that foreign key references an index.

elementMatcher Groovy expression to include/exclude elements from validation

net.sourceforge.dbtoolbox.validator.IsolatedTableMDValidator

Checks that table has at least one relation to another table (avoid abandonned tables).

elementMatcher Groovy expression to include/exclude elements from validation

net.sourceforge.dbtoolbox.validator.GroovyElementMDValidator

Checks that element validates against a Groovy script.

elementClass Target model class (subclass of ElementMD and ColumnSet: TableMD, PrimaryKeyMD...)
Required.
elementMatcher Groovy expression to include/exclude elements from validation
scriptFile Groovy script file containing validator code
bindingName Name of the script variable corresponding to element being validated. Defaults to 'object'.

Sample Groovy script:

if (!object.name.toUpperCase().startsWith("ABC")) {
    validatorMessages.add(validator.createMessage(table,"Name is not prefixed by ABC"));
}

Three Groovy variables are pre initialised:

  • object (can be changed using bindingName): The element being validated (table, column...)
  • validator: The validator being run
  • validatorMessages: The collection of messages raised by validators.