Skip to Content
DocsLanguageRules and Conditions as Data

Rules and Conditions as Data

Since v0.2

LODE has no expression language. Conditions such as “all of these, none of those” are written as data: a variant type whose alternatives are predicates, and whose all, any and not alternatives hold more of the same type. The schema author decides which predicates exist; the program that reads the value decides what each predicate means.

{ "schema": "lode", "version": 1, "name": "RuleTable", "types": { "Regex": { "kind": "record", "fields": [ { "id": 1, "name": "source", "type": "string" }, { "id": 2, "name": "flags", "type": "string" } ] }, "Predicate": { "kind": "variant", "fields": [ { "id": 1, "name": "anyKeyword", "type": "string[]" }, { "id": 2, "name": "regex", "type": "Regex" }, { "id": 3, "name": "all", "type": "list<Predicate>" }, { "id": 4, "name": "any", "type": "list<Predicate>" }, { "id": 5, "name": "not", "type": "Predicate" } ] }, "Rule": { "kind": "record", "fields": [ { "id": 1, "name": "id", "type": "string" }, { "id": 2, "name": "when", "type": "Predicate" }, { "id": 3, "name": "decision", "type": "string" }, { "id": 4, "name": "weightBp", "type": "int", "min": 0, "max": 10000 } ] } }, "fields": [{ "id": 1, "name": "rules", "type": "list<Rule>" }] }

A rule then reads:

{ "id": "build_app", "when": { "all": [{ "anyKeyword": ["build", "make"] }, { "not": { "anyKeyword": ["explain"] } }] }, "decision": "build", "weightBp": 9000 }

Why data and not expressions

  • LODE stays declarative. A schema describes the shape of values; it never runs. Adding an evaluator would make LODE a programming language, with questions about termination, side effects and versioned semantics that a serialization format should not answer.
  • Every reader can evaluate it. A TypeScript service and a Rust service read the same JSON and apply the same small evaluator. An expression syntax would need a parser in every language, and parsers drift.
  • The fingerprint covers the logic. The predicate tree is part of the canonical form, so changing a keyword, a regex, a weight or the order of two rules changes the fingerprint. Rule tables become versioned, reviewable, diffable artefacts.
  • The schema limits what a rule can do. A reader only supports the predicates its schema declares. A new predicate kind is a schema change (a new variant field), not a new string that some reader might not handle.
  • Finite by construction. Values are trees with at most 64 levels; there are no loops or references to evaluate.

Conventions for rule tables

  • Put rules in list<Rule>: evaluation order is part of the meaning, and list<T> keeps order in the fingerprint. Use string[] only where order does not matter (keyword sets).
  • Write weights, thresholds and probabilities as integer basis points (int with "min": 0, "max": 10000).
  • Write regular expressions as { "source", "flags" } strings. Which regex dialect a reader supports is the reader’s contract (for example, Rust’s regex crate has no look-around); document it next to the schema.
  • Give every rule a stable id, so logs and audits can say which rule fired.

See Routing Rules for a complete example.

Last updated on