Kaizen
Browse modulesRulesrules/sharedFunctions

Function: matchRulesAgainstTables()

function matchRulesAgainstTables<TContext, TDomainConfig>(context: TContext, options: {
  conditionsTable: Record<string, Condition>;
  customConditions?: CustomConditionDef<unknown, TContext>[];
  domain: string;
  domainConfigSchema: ZodType<TDomainConfig>;
  organizationId: string;
  rulesTable: Record<string, Rule>;
  scopeMapping: Record<string, string>;
}): Promise<MatchResult<TDomainConfig>>;

Defined in: shared/rules/engine.ts:385

Pure rules-engine kernel. Match active rules against the given context using caller-supplied rulesTable and conditionsTable.

  • Loads active, current rules from rulesTable for the domain/org
  • Filters by scope matching
  • Evaluates each rule's condition_logic tree, dispatching built-in conditions via ts-pattern and custom conditions via customConditions
  • Parses each matched rule's domain_config through domainConfigSchema, typing the result as TDomainConfig

Most consumers should use the higher-level matchRules() from @the-kaizen-labs/taproot/rules/server, which loads tables from Postgres and delegates here. This kernel is exposed for tests, static authoring tools, and any caller that wants to manage rule data itself.

Custom conditions take precedence over built-ins — a conditionType matching a built-in replaces it entirely.

Time semantics: each time-aware condition handler manages its own evaluation date (defaulting to DateTime.now()). The engine does not thread a clock — tests pin time via vi.setSystemTime(...), and production replays anchor to context fields via per-condition config (e.g. RelativeDateCheckConfig.evaluation_date_field).

Type Parameters

Type Parameter
TContext
TDomainConfig

Parameters

ParameterType
contextTContext
options{ conditionsTable: Record<string, Condition>; customConditions?: CustomConditionDef<unknown, TContext>[]; domain: string; domainConfigSchema: ZodType<TDomainConfig>; organizationId: string; rulesTable: Record<string, Rule>; scopeMapping: Record<string, string>; }
options.conditionsTableRecord<string, Condition>
options.customConditions?CustomConditionDef<unknown, TContext>[]
options.domainstring
options.domainConfigSchemaZodType<TDomainConfig>
options.organizationIdstring
options.rulesTableRecord<string, Rule>
options.scopeMappingRecord<string, string>

Returns

Promise<MatchResult<TDomainConfig>>

On this page