actualbudget/actual · error · CompileError

Invalid order direction:

Error message

Invalid order direction: 

What it means

compileOrderBy validates the optional direction suffix of an order spec; it only accepts the exact strings 'desc' and 'asc'. Any other value (including empty-ish or misspelled strings) throws this CompileError.

Source

Thrown at packages/loot-core/src/server/aql/compiler.ts:975

    } else {
      const entries = Object.entries(expr);
      const entry = entries[0];

      // Check if this is a field reference
      if (entries.length === 1 && entry[0][0] !== '$') {
        dir = entry[1];
        compiled = compileExpr(state, '$' + entry[0]).value;
      } else {
        // Otherwise it's a function
        const { $dir, ...func } = expr;
        dir = $dir;
        compiled = compileFunction(state, func).value;
      }
    }

    if (dir != null) {
      if (dir !== 'desc' && dir !== 'asc') {
        throw new CompileError('Invalid order direction: ' + dir);
      }
      return `${compiled} ${dir}`;
    }
    return compiled;
  });

  return orderBy.join(', ');
});

const AGGREGATE_FUNCTIONS = ['$sum', '$count'];
function isAggregateFunction(expr) {
  if (typeof expr !== 'object' || Array.isArray(expr)) {
    return false;
  }

  const [name, originalArgExprs] = Object.entries(expr)[0];
  let argExprs = originalArgExprs;
  if (!Array.isArray(argExprs)) {

View on GitHub (pinned to d4334cb6e6)

Solutions

  1. Lowercase the direction string before passing: `dir.toLowerCase()` and ensure it is exactly 'asc' or 'desc'.
  2. Normalize user/config-driven sort order with a whitelist mapping to 'asc'/'desc'.
  3. Omit the direction argument entirely to use the default ascending order.

Example fix

// before
.orderBy('date', 'DESC')
// after
.orderBy('date', 'desc')
Defensive patterns

Strategy: validation

Validate before calling

function normalizeDir(dir) {
  if (dir == null) return undefined;
  const d = String(dir).toLowerCase();
  if (d !== 'asc' && d !== 'desc') throw new Error(`Invalid order direction: ${dir}`);
  return d;
}

Type guard

const isSortDir = (v) => v === 'asc' || v === 'desc';

Prevention

When it happens

Trigger: Calling `.orderBy('date', 'DESC')` (uppercase), `.orderBy('date', 'descending')`, or passing an invalid dynamic variable as the direction argument.

Common situations: Uppercase SQL-style direction keywords, user-supplied sort direction from UI state that wasn't normalized, or passing `null`/undefined strings from config.

Related errors


AI-assisted analysis of actualbudget/actual@d4334cb6e6 (2026-08-29). Data as JSON: /api/errors/4f76067733c6fbc6. Report an issue: GitHub.