Skip to main content

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

ParameterTypeDescription
conditionTreeObjectA nested condition tree structure (see below)
dataObjectThe 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

OperatorDescriptionExample
equalsStrict equality (===)value === expected
not_equalsStrict inequality (!==)value !== expected
containsString contains substring"hello world".includes("world")
starts_withString starts with prefix"hello".startsWith("he")
ends_withString ends with suffix"hello".endsWith("lo")
greater_thanNumeric greater thanvalue > expected
less_thanNumeric less thanvalue < expected
greater_or_equalNumeric greater or equalvalue >= expected
less_or_equalNumeric less or equalvalue <= expected
inValue is in array['a','b'].includes(value)
not_inValue is not in array!['a','b'].includes(value)
existsValue is defined and not nullvalue !== undefined && value !== null
not_existsValue is undefined or nullvalue === undefined || value === null
emptyValue is empty (string, array, or falsy)!value || value === ''
not_emptyValue is not emptyvalue && value !== ''
matchesRegular expression match/pattern/.test(value)

Group Operators

OperatorDescription
ANDAll conditions must be true
ORAt least one condition must be true
NOTNegates 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 }
note

The message. prefix is automatically stripped from field paths. Both message.type and type will access the same value.

Error Handling

  • Returns false for unknown comparison operators (logs a warning)
  • Returns false for unknown group operators (logs a warning)
  • Returns false if the condition tree is null or undefined
  • Safely handles missing nested properties (returns undefined)