{"record":{"id":"b4468643c2b0ff8b","repo":"pinojs/pino","slug":"levels-comparison-should-be-one-of-asc-desc-o","errorCode":null,"errorMessage":"Levels comparison should be one of \"ASC\", \"DESC\" or \"function\" type","messagePattern":"Levels comparison should be one of \"ASC\", \"DESC\" or \"function\" type","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"lib/levels.js","lineNumber":226,"sourceCode":"}\n\n/**\n * Validates whether `levelComparison` is correct\n *\n * @throws Error\n * @param {SORTING_ORDER | Function} levelComparison - value to validate\n * @returns\n */\nfunction assertLevelComparison (levelComparison) {\n  if (typeof levelComparison === 'function') {\n    return\n  }\n\n  if (typeof levelComparison === 'string' && Object.values(SORTING_ORDER).includes(levelComparison)) {\n    return\n  }\n\n  throw new Error('Levels comparison should be one of \"ASC\", \"DESC\" or \"function\" type')\n}\n\nmodule.exports = {\n  initialLsCache,\n  genLsCache,\n  levelMethods,\n  getLevel,\n  setLevel,\n  isLevelEnabled,\n  mappings,\n  assertNoLevelCollisions,\n  assertDefaultLevelFound,\n  genLevelComparison,\n  assertLevelComparison\n}\n","sourceCodeStart":208,"sourceCodeEnd":242,"githubUrl":"https://github.com/pinojs/pino/blob/5aa62305c57709ebd6f6b7c328e8830eeb29b297/lib/levels.js#L208-L242","documentation":"pino's assertLevelComparison validates the levelComparison option used for sorting custom levels. It must be the string \"ASC\", \"DESC\", or a function; anything else (other strings, numbers, etc.) is rejected at logger creation time.","triggerScenarios":"Passing pino({ levelComparison: 'asc' }) (lowercase), levelComparison: true, levelComparison: 1, or any value not in SORTING_ORDER and not a function.","commonSituations":"Typos or wrong casing when configuring custom levels sort order; passing a comparison value read from config that is not normalized; misunderstanding that only exact \"ASC\"/\"DESC\" strings are accepted.","solutions":["Use the exact strings \"ASC\" or \"DESC\" (uppercase)","Pass a comparator function: levelComparison: (a, b) => a - b","Normalize config values: levelComparison: (raw || '').toUpperCase()"],"exampleFix":"// before\npino({ customLevels: { foo: 30 }, levelComparison: 'asc' });\n// after\npino({ customLevels: { foo: 30 }, levelComparison: 'ASC' });","handlingStrategy":"validation","validationCode":"const VALID = ['ASC', 'DESC'];\nfunction assertLevelComparison(lc) {\n  if (!(VALID.includes(lc) || typeof lc === 'function')) {\n    throw new TypeError('levelComparison must be \"ASC\", \"DESC\", or a function');\n  }\n}","typeGuard":"function isValidLevelComparison(v) { return v === 'ASC' || v === 'DESC' || typeof v === 'function'; }","tryCatchPattern":"try { const logger = pino(opts); } catch (e) { if (/Levels comparison/.test(e.message)) { opts.levelComparison = 'ASC'; } throw e; }","preventionTips":["Normalize case when levelComparison comes from config: String(raw).toUpperCase()","Use TypeScript union type: 'ASC' | 'DESC' | ((a: number, b: number) => number)","Only pass levelComparison when configuring custom levels"],"tags":["logging","configuration","pino","levels"],"backgroundTag":"invalid-enum-value","analyzedSha":"5aa62305c57709ebd6f6b7c328e8830eeb29b297","analyzedAt":"2026-09-02T22:05:21.418Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-10T02:17:09.455Z"}