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
- Lowercase the direction string before passing: `dir.toLowerCase()` and ensure it is exactly 'asc' or 'desc'.
- Normalize user/config-driven sort order with a whitelist mapping to 'asc'/'desc'.
- 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
- Normalize UI/config sort input with toLowerCase() before passing.
- Type sort direction as the union 'asc' | 'desc' end to end.
- Never pass user-supplied strings directly as order direction.
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
- Table "${tableName}" does not exist in the schema
- Can't cast ${expr.type} to date
- Can't cast ${expr.type} to date-month
- Can't cast ${expr.type} to date-year
- Can't convert ${expr.type} to ${type}
AI-assisted analysis of actualbudget/actual@d4334cb6e6 (2026-08-29).
Data as JSON: /api/errors/4f76067733c6fbc6.
Report an issue: GitHub.