evaluateCondition
Evaluate complex condition trees against event data.
Signature
evaluateCondition(conditionTree, data)
Description
The evaluateCondition function evaluates a hierarchical condition tree against a data object. It supports complex boolean logic with AND, OR, and NOT operators, as well as a comprehensive set of comparison operators.
This function is typically used internally by the workflow engine but can also be called directly in custom scripts for advanced conditional logic.
Parameters
| Parameter | Type | Description |
|---|---|---|
conditionTree | Object | A nested condition tree structure (see below) |
data | Object | The data object to evaluate against |
Returns
boolean — Returns true if the condition tree evaluates to true, false otherwise.
Condition Tree Structure
The condition tree consists of two node types: rules and groups.
Rule Nodes
A rule represents a single comparison:
{
type: 'rule',
field: 'message.type', // Path to value (dot notation)
comparison: 'equals', // Comparison operator
value: 'touchdown' // Expected value
}
Group Nodes
A group combines multiple conditions with a logical operator:
{
type: 'group',
operator: 'AND', // AND, OR, or NOT
conditions: [ // Array of rules or nested groups
{ type: 'rule', ... },
{ type: 'group', ... }
]
}
Comparison Operators
| Operator | Description | Example |
|---|---|---|
equals | Strict equality (===) | value === expected |
not_equals | Strict inequality (!==) | value !== expected |
contains | String contains substring | "hello world".includes("world") |
starts_with | String starts with prefix | "hello".startsWith("he") |
ends_with | String ends with suffix | "hello".endsWith("lo") |
greater_than | Numeric greater than | value > expected |
less_than | Numeric less than | value < expected |
greater_or_equal | Numeric greater or equal | value >= expected |
less_or_equal | Numeric less or equal | value <= expected |
in | Value is in array | ['a','b'].includes(value) |
not_in | Value is not in array | !['a','b'].includes(value) |
exists | Value is defined and not null | value !== undefined && value !== null |
not_exists | Value is undefined or null | value === undefined || value === null |
empty | Value is empty (string, array, or falsy) | !value || value === '' |
not_empty | Value is not empty | value && value !== '' |
matches | Regular expression match | /pattern/.test(value) |
Group Operators
| Operator | Description |
|---|---|
AND | All conditions must be true |
OR | At least one condition must be true |
NOT | Negates the first condition |
Examples
Simple Rule
const rule = {
type: 'rule',
field: 'type',
comparison: 'equals',
value: 'touchdown'
};
const data = { type: 'touchdown', team: 'KC' };
const result = evaluateCondition(rule, data);
// result: true
AND Group
const condition = {
type: 'group',
operator: 'AND',
conditions: [
{ type: 'rule', field: 'type', comparison: 'equals', value: 'touchdown' },
{ type: 'rule', field: 'team', comparison: 'equals', value: 'KC' }
]
};
const data = { type: 'touchdown', team: 'KC' };
const result = evaluateCondition(condition, data);
// result: true (both conditions match)
OR Group
const condition = {
type: 'group',
operator: 'OR',
conditions: [
{ type: 'rule', field: 'type', comparison: 'equals', value: 'touchdown' },
{ type: 'rule', field: 'type', comparison: 'equals', value: 'field_goal' }
]
};
const data = { type: 'field_goal' };
const result = evaluateCondition(condition, data);
// result: true (second condition matches)
Nested Groups
const condition = {
type: 'group',
operator: 'AND',
conditions: [
{ type: 'rule', field: 'sport', comparison: 'equals', value: 'NFL' },
{
type: 'group',
operator: 'OR',
conditions: [
{ type: 'rule', field: 'type', comparison: 'equals', value: 'touchdown' },
{ type: 'rule', field: 'type', comparison: 'equals', value: 'field_goal' }
]
}
]
};
// Matches: NFL touchdown OR NFL field_goal
const data = { sport: 'NFL', type: 'touchdown' };
const result = evaluateCondition(condition, data);
// result: true
Using Regex Match
const condition = {
type: 'rule',
field: 'description',
comparison: 'matches',
value: '^[A-Z]\\. [A-Z][a-z]+ scores' // "P. Mahomes scores..."
};
const data = { description: 'P. Mahomes scores touchdown' };
const result = evaluateCondition(condition, data);
// result: true
Checking Array Membership
const condition = {
type: 'rule',
field: 'status',
comparison: 'in',
value: ['active', 'pending', 'processing']
};
const data = { status: 'pending' };
const result = evaluateCondition(condition, data);
// result: true
NOT Operator
const condition = {
type: 'group',
operator: 'NOT',
conditions: [
{ type: 'rule', field: 'status', comparison: 'equals', value: 'ignored' }
]
};
const data = { status: 'active' };
const result = evaluateCondition(condition, data);
// result: true (status is NOT 'ignored')
Field Path Syntax
Fields support dot notation for accessing nested properties:
// Access: data.player.name
{ field: 'player.name', comparison: 'equals', value: 'Mahomes' }
// Access: data.stats.passing.yards
{ field: 'stats.passing.yards', comparison: 'greater_than', value: 300 }
The message. prefix is automatically stripped from field paths. Both message.type and type will access the same value.
Error Handling
- Returns
falsefor unknown comparison operators (logs a warning) - Returns
falsefor unknown group operators (logs a warning) - Returns
falseif the condition tree is null or undefined - Safely handles missing nested properties (returns
undefined)