Skip to content

@qarar/sdk Reference

The @qarar/sdk package is the main entry point to the Qarar platform. It re-exports all essential functions, compilers, builders, and types with zero external runtime dependencies.

Terminal window
pnpm add @qarar/sdk

Exported Functions

FunctionSignatureDescription
rule(nameOrId?)(nameOrId?: string) => RuleBuilderStarts building a dynamic rule AST using fluent chainable methods.
field(path)(path: string) => ValueNodeCreates a dynamic field reference node for dynamic two-sided comparisons.
switchRule(nameOrId?)<T>(nameOrId?: string) => DecisionTableBuilder<T>Starts building a multi-branch decision table matrix.
checkRule(rule, data, opts?)(rule: Rule, data: unknown) => CheckRuleResultAll-in-one validator, evaluator, and human-readable explanation generator.
compileRule(rule)(rule: Rule) => (data: unknown) => booleanCompiles a rule into an optimized closure executing at ~6.2M ops/sec.
evaluate(rule, data)(rule: Rule, data: unknown) => EvaluationResultEvaluates a rule against data and returns the raw TraceEntry[] tree.
evaluateDecisionTable(table, data)<T>(table: DecisionTable<T>, data: unknown) => DecisionTableResult<T>Evaluates an input payload against a decision table matrix.
compileToSQL(rule, opts?)(rule: Rule, opts?: SqlCompileOptions) => SqlCompileResultCompiles a rule AST into a SQL WHERE clause at ~1.69M compiles/sec.
compileToMongo(rule)(rule: Rule) => MongoCompileResultCompiles a rule AST into a MongoDB query filter at ~4.22M compiles/sec.
explainFailures(trace)(trace: TraceEntry[]) => string[]Formats failed condition trace entries into human-readable strings.
validateRule(json)(input: unknown) => RuleValidates an untrusted JSON object against the strict Qarar rule schema (throws on invalid input).
safeValidateRule(json)(input: unknown) => ValidationResult<Rule>Safely validates an untrusted JSON object without throwing ({ success, data / error }).
createQarar(schema)<T>(schema: SchemaLike<T>) => QararSchemaInstance<T>Binds rule execution to a domain schema validator for runtime payload validation.

Core TypeScript Types

// Top-Level Rule Container
export interface Rule {
id: string;
name: string;
description?: string;
version: string;
root: RuleNode;
}
// Decision Table Container
export interface DecisionTable<T = unknown> {
id: string;
name: string;
version: string;
mode: "first" | "all";
defaultOutput: T;
branches: Array<{
id: string;
name: string;
condition: RuleNode;
output: T;
}>;
}
// Result of checkRule()
export interface CheckRuleResult {
result: boolean;
valid: boolean;
reasons: string[];
trace: TraceEntry[];
errors?: string[];
}
// Single Condition Execution Trace
export interface TraceEntry {
path?: string;
operator?: string;
expected?: unknown;
actual?: unknown;
passed: boolean;
message?: string;
nodeType: string;
}