Skip to content

Validation Rules

Rules validate field values in taxonomy and payload definitions.

Add rules to field definitions:

opentp.yaml
spec:
events:
taxonomy:
area:
type: string
rules:
max-length: 50
regex: "^[a-z_]+$"
not-empty: true

Rules can also be applied in event payload schemas:

events/auth/login.yaml
payload:
platforms:
all:
history:
1.0.0:
schema:
user_id:
type: string
rules:
min-length: 1
max-length: 100

Maximum string length.

rules:
max-length: 50
ValueParamsResult
hello50Valid
hello... (60 chars)50Invalid

Minimum string length.

rules:
min-length: 3
ValueParamsResult
hello3Valid
hi3Invalid

Match a regular expression.

rules:
regex: "^[a-z_]+$"
ValuePatternResult
hello_world^[a-z_]+$Valid
Hello World^[a-z_]+$Invalid

Value must not be empty.

rules:
not-empty: true
ValueResult
helloValid
Invalid
Invalid (whitespace only)

Value must start with a prefix.

rules:
starts-with: "app_"
ValueParamsResult
app_loginapp_Valid
loginapp_Invalid

Value must end with a suffix.

rules:
ends-with: "_event"
ValueParamsResult
login_event_eventValid
login_eventInvalid

Value must contain a substring.

rules:
contains: "_"
ValueParamsResult
hello_world_Valid
helloworld_Invalid

Validate against an external API.

rules:
webhook:
url: https://api.company.com/validate
headers:
Authorization: "Bearer ${API_KEY}"
timeout: 5000
retries: 2
ParamTypeRequiredDescription
urlstringYesValidation endpoint URL
headersobjectNoHTTP headers
timeoutnumberNoTimeout in ms (default: 5000)
retriesnumberNoRetry attempts (default: 0)

Use ${VAR_NAME} to reference environment variables:

webhook:
url: ${VALIDATOR_URL}/check
headers:
Authorization: "Bearer ${API_KEY}"

The webhook should return JSON:

// Valid
{ "valid": true }
// Invalid
{ "valid": false, "error": "Company ID not found" }

Multiple rules are evaluated in order:

rules:
not-empty: true
min-length: 3
max-length: 50
regex: "^[a-z_]+$"

All rules must pass for the value to be valid.

Create custom validation rules in JavaScript:

my-rules/company-id/index.js
module.exports = {
name: 'company-id',
validate: (value, params, context) => {
if (!value.startsWith('COMP-')) {
return {
valid: false,
error: 'Must start with COMP-'
};
}
if (value.length !== 12) {
return {
valid: false,
error: 'Must be exactly 12 characters'
};
}
return { valid: true };
}
};

Use in your tracking plan:

taxonomy:
company:
type: string
rules:
company-id: true

Load with CLI:

Terminal window
opentp validate --external-rules ./my-rules

Or in config:

opentp.yaml
spec:
external:
rules: ./my-rules

Custom rules receive a context object:

validate: (value, params, context) => {
// context.field - field name
// context.path - full path (e.g., "taxonomy.area")
// context.event - current event object
// context.config - opentp.yaml config
}