Skip to content

elide:cel

The elide:cel module: compile and evaluate Common Expression Language expressions.

Read offlineelide help api-cel

On this page

elide:cel compiles and evaluates CEL expressions. Expressions have no side effects and evaluation is bounded, so they suit user-supplied rules, filters and policies. There is nothing to install.

js
import { compile } from "elide:cel";

const rule = compile("user.age >= 18 && 'admin' in user.roles");
rule.variables; // ["user"]
rule.test({ user: { age: 30, roles: ["admin"] } }); // true
compile("xs.filter(x, x % 2 == 0)").evaluate({ xs: [1, 2, 3, 4] }); // [2, 4]
utf-8 · js · Lines: 6

Compile and evaluate

  • compile(expression) parses and type-checks once and returns a reusable program. Free identifiers become variables, typed dyn. Invalid expressions throw an Error with the CEL diagnostic.
  • program.evaluate(variables?) returns the result; program.test(variables?) requires a boolean. Evaluation failures throw an Error.
  • program.source is the expression text and program.variables its free identifiers.
  • The standard library and macros (has, all, exists, exists_one, map, filter) are available, with optional values, two-variable macros, and string, list, math and network extensions. See the extension reference.
js
// rules.mjs
import { compile } from "elide:cel";

const canEdit = compile("doc.owner == user.name || 'editor' in user.roles");
const doc = { owner: "ada" };
for (const user of [{ name: "ada", roles: [] }, { name: "lin", roles: ["viewer"] }]) {
  console.log(user.name, canEdit.test({ doc, user }));
}
utf-8 · js · Lines: 8

elide run rules.mjs prints:

text
ada true
lin false
utf-8 · text · Lines: 2

Values

JavaScript CEL
integral number, bigint within int64 int
other number double
string, boolean, null string, bool, null
Uint8Array, Buffer bytes
array, plain object list, map

Results convert back the same way. An int beyond Number.MAX_SAFE_INTEGER returns as a bigint; a timestamp or duration returns as its string form. Comparisons mix int and double freely, arithmetic does not: write double(x) / 2.0 when x may be integral.

TypeScript

@elide-dev/types declares the module: compile() returns a CelProgram, and values crossing into or out of CEL are CelValues.

ts
import { type CelValue, compile } from "elide:cel";

const limit = compile("size(items) <= max");
const ok: boolean = limit.test({ items: [1, 2, 3], max: 5 });
const result: CelValue = compile("items.map(i, i * 2)").evaluate({ items: [1, 2] });
utf-8 · ts · Lines: 5

Samples

Was this page helpful?