{"record":{"id":"63f279b75c7564c1","repo":"n8n-io/n8n","slug":"selectquerybuilder-addorderby-order-can-accept-o","errorCode":null,"errorMessage":"SelectQueryBuilder.addOrderBy \"order\" can accept only \"ASC\" and \"DESC\" values.","messagePattern":"SelectQueryBuilder\\.addOrderBy \"order\" can accept only \"ASC\" and \"DESC\" values\\.","errorType":"exception","errorClass":"TypeORMError","httpStatus":null,"severity":"error","filePath":"packages/@n8n/typeorm/src/query-builder/SelectQueryBuilder.ts","lineNumber":1273,"sourceCode":"\t/**\n\t * Sets ORDER BY condition in the query builder.\n\t * If you had previously ORDER BY expression defined,\n\t * calling this function will override previously set ORDER BY conditions.\n\t */\n\torderBy(order: OrderByCondition): this;\n\n\t/**\n\t * Sets ORDER BY condition in the query builder.\n\t * If you had previously ORDER BY expression defined,\n\t * calling this function will override previously set ORDER BY conditions.\n\t */\n\torderBy(\n\t\tsort?: string | OrderByCondition,\n\t\torder: 'ASC' | 'DESC' = 'ASC',\n\t\tnulls?: 'NULLS FIRST' | 'NULLS LAST',\n\t): this {\n\t\tif (order !== undefined && order !== 'ASC' && order !== 'DESC')\n\t\t\tthrow new TypeORMError(\n\t\t\t\t`SelectQueryBuilder.addOrderBy \"order\" can accept only \"ASC\" and \"DESC\" values.`,\n\t\t\t);\n\t\tif (nulls !== undefined && nulls !== 'NULLS FIRST' && nulls !== 'NULLS LAST')\n\t\t\tthrow new TypeORMError(\n\t\t\t\t`SelectQueryBuilder.addOrderBy \"nulls\" can accept only \"NULLS FIRST\" and \"NULLS LAST\" values.`,\n\t\t\t);\n\n\t\tif (sort) {\n\t\t\tif (typeof sort === 'object') {\n\t\t\t\tthis.expressionMap.orderBys = sort as OrderByCondition;\n\t\t\t} else {\n\t\t\t\tif (nulls) {\n\t\t\t\t\tthis.expressionMap.orderBys = {\n\t\t\t\t\t\t[sort as string]: { order, nulls },\n\t\t\t\t\t};\n\t\t\t\t} else {\n\t\t\t\t\tthis.expressionMap.orderBys = { [sort as string]: order };\n\t\t\t\t}","sourceCodeStart":1255,"sourceCodeEnd":1291,"githubUrl":"https://github.com/n8n-io/n8n/blob/5ac6606e81f67bb9534255570cd4e86fd8101eee/packages/@n8n/typeorm/src/query-builder/SelectQueryBuilder.ts#L1255-L1291","documentation":"SelectQueryBuilder.orderBy validates the order argument and throws TypeORMError if it is neither 'ASC' nor 'DESC' (and not undefined). The signature is typed as the union, but a runtime value (often from user input) that violates it is caught here rather than producing malformed SQL.","triggerScenarios":"qb.orderBy('user.name', req.query.sort as string) where req.query.sort is 'asc'/'Asc'/'ascending'/''; passing a lowercased or localized sort string from an HTTP API.","commonSituations":"Accepting sort direction from query params without normalization; copy-pasting 'ascending' from a UI dropdown value; locale-specific strings.","solutions":["Normalize user input: const dir = String(req.query.sort).toUpperCase() === 'DESC' ? 'DESC' : 'ASC'; then qb.orderBy(field, dir).","Whitelist the value before passing: if (!['ASC','DESC'].includes(input)) throw new UserError('invalid sort').","Use zod/joi schema validation on the query string with an enum of ['asc','desc']."],"exampleFix":"// before\nqb.orderBy('user.name', req.query.dir as 'ASC'|'DESC');\n// after\nconst dir = req.query.dir === 'desc' ? 'DESC' : 'ASC';\nqb.orderBy('user.name', dir);","handlingStrategy":"validation","validationCode":"const SORT_DIR = ['ASC', 'DESC'] as const;\ntype SortDir = typeof SORT_DIR[number];\nfunction normalizeDir(input: unknown): SortDir {\n  return String(input).toUpperCase() === 'DESC' ? 'DESC' : 'ASC';\n}\n// qb.orderBy(field, normalizeDir(req.query.dir));","typeGuard":"function isSortDir(value: unknown): value is 'ASC' | 'DESC' {\n  return value === 'ASC' || value === 'DESC';\n}","tryCatchPattern":null,"preventionTips":["Whitelist and upper-case sort direction at the controller boundary.","Validate the query string with a zod enum(['asc','desc']) schema.","Never cast user input to the union type without normalization."],"tags":["typeorm","select-query-builder","order-by","validation"],"backgroundTag":null,"analyzedSha":"5ac6606e81f67bb9534255570cd4e86fd8101eee","analyzedAt":"2026-08-12T05:26:35.080Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}