Create Custom Validation Rules
Introduction
You can add validation rules to properties and configuration items (CIs) in synthetic.xml
or as Java annotations. Deploy comes with built-in rules (like regex
), but you can also create your own custom rules for advanced validation needs.
This XML snippet shows how to add a validation rule:
<type type="tc.WarModule" extends="ud.BaseDeployedArtifact" deployable-type="jee.War"
container-type="tc.Server">
<property name="changeTicketNumber" required="true">
<rule type="regex" pattern="^JIRA-[0-9]+$"
message="Ticket number should be of the form JIRA-[number]"/>
</property>
</type>
Validation will throw an error if tc.WarModule
is saved in Deploy with a value that is not in the form: JIRA-[number]
.
Prerequisites
Make sure you have one of the following tools installed:
You will also need a local Java development environment that matches the Java version used by your Digital.ai Deploy server.
Creating a Custom Validation Rule in Java
This section provides a complete, working example of how to create, package, and use a custom validation rule in Digital.ai Deploy.
Suppose you want a validation rule that ensures a property value exactly matches a specific string—for example, the value must be /tmp
. This is useful when enforcing environment-specific or platform-specific constraints.
1. Project Structure
custom-validation-rule/
├── src/
│ └── main/
│ └── java/
│ └── com/
│ └── example/
│ └── validation/
│ └── StaticContent.java
├── pom.xml or build.gradle
2. Implement the Java Validation Rule
StaticContent.java:
package com.example.validation;
import com.xebialabs.deployit.plugin.api.validation.Rule;
import com.xebialabs.deployit.plugin.api.validation.ValidationContext;
import com.xebialabs.deployit.plugin.api.validation.ApplicableTo;
import com.xebialabs.deployit.plugin.api.reflect.PropertyKind;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@ApplicableTo(PropertyKind.STRING)
@Retention(RetentionPolicy.RUNTIME)
@Rule(clazz = StaticContent.Validator.class, type = "static-content")
@Target(ElementType.FIELD)
public @interface StaticContent {
String content();
public static class Validator
implements com.xebialabs.deployit.plugin.api.validation.Validator<String> {
private String content;
@Override
public void validate(String value, ValidationContext context) {
if (value != null && !value.equals(content)) {
context.error("Value should be %s but was %s", content, value);
}
}
}
}
3. Configure the Build
You can use either Maven or Gradle to compile and package your rule.
Maven Example:
pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>custom-validation-rule</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<repositories>
<repository>
<id>xebialabs-public</id>
<url>https://dist.xebialabs.com/public/maven2/com/xebialabs/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.xebialabs.deployit</groupId>
<artifactId>udm-plugin-api</artifactId>
<version>LATEST_COMPATIBLE_VERSION</version>
</dependency>
<!-- Add other plugin modules here if needed -->
</dependencies>
</project>
To build the JAR with Maven, run:
mvn clean package
Gradle Example:
build.gradle:
plugins {
id 'java'
}
group = 'com.example'
version = '1.0.0'
repositories {
mavenCentral()
maven {
url 'https://dist.xebialabs.com/public/maven2/com/xebialabs/'
}
}
dependencies {
compileOnly 'com.xebialabs.deployit:udm-plugin-api:LATEST_COMPATIBLE_VERSION'
// You can add other deployit modules here as needed
}
To build the JAR with Gradle, run:
gradle build
If you are using dependencies hosted in the Digital.ai Public Maven repository, you must explicitly declare the repository url in your pom.xml
under the <repositories>
section. This ensures that Maven can resolve and download the required artifacts.
Always use the version of the module that matches your Deploy server version to ensure compatibility
4. JAR Structure
After the build completes, you will find a JAR file in:
-
target/ directory for Maven
-
build/libs/ directory for Gradle
5. Deploy the JAR to Digital.ai Deploy
- Copy the JAR file to the
plugins/_local_
directory of your Deploy server. - Restart the Deploy server.
6. Use the Custom Rule
You can now use your custom rule in Java CIs or in synthetic XML, as shown below:
In Java:
public class MyLinuxHost extends BaseContainer {
@Property
@StaticContent(content = "/tmp")
private String temporaryDirectory;
}
In synthetic.xml:
<type name="ext.MyLinuxHost" extends="udm.BaseContainer">
<property name="temporaryDirectory">
<rule type="static-content" content="/tmp"/>
</property>
</type>