{"record":{"id":"0429134d2d56f672","repo":"affaan-m/ECC","slug":"label-must-be-a-positive-integer","errorCode":null,"errorMessage":"${label} must be a positive integer","messagePattern":"(.+?) must be a positive integer","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"scripts/consult.js","lineNumber":187,"sourceCode":"function tokenize(value) {\n  const normalized = normalizeToken(value);\n  if (!normalized) {\n    return [];\n  }\n\n  const tokens = [];\n  for (const token of normalized.split(/\\s+/)) {\n    if (!token || STOP_WORDS.has(token)) {\n      continue;\n    }\n    tokens.push(...expandToken(token));\n  }\n  return [...new Set(tokens)];\n}\n\nfunction parsePositiveInteger(value, label) {\n  if (!/^[1-9]\\d*$/.test(String(value || ''))) {\n    throw new Error(`${label} must be a positive integer`);\n  }\n  return Number(value);\n}\n\nfunction parseArgs(argv) {\n  const args = argv.slice(2);\n  const parsed = {\n    queryParts: [],\n    target: DEFAULT_TARGET,\n    limit: DEFAULT_LIMIT,\n    json: false,\n    help: false,\n  };\n\n  if (args.includes('--help') || args.includes('-h')) {\n    parsed.help = true;\n    return parsed;\n  }","sourceCodeStart":169,"sourceCodeEnd":205,"githubUrl":"https://github.com/affaan-m/ECC/blob/01e15490f04e29cfefe3896951f43db46994d8ee/scripts/consult.js#L169-L205","documentation":"The parsePositiveInteger() function in consult.js validates that a value matches the regex ^[1-9]\\d*$ before accepting it. This means the value must start with a digit 1-9 and contain only digits afterward. Zero, negative numbers, decimals, scientific notation, and non-numeric strings all fail. It is used to validate the --limit flag.","triggerScenarios":"Passing --limit 0 (zero results requested), --limit -5 (negative), --limit 3.5 (decimal), --limit abc (non-numeric), or --limit 010 (leading zero fails the regex).","commonSituations":"Scripts that derive --limit from an environment variable or calculation that can produce 0 or a non-integer; users assuming --limit 0 means 'no limit'; passing a float when an integer is expected.","solutions":["Provide a positive integer starting with 1-9, e.g. --limit 5","If deriving from a variable, validate it before passing: only pass --limit if the value is >= 1","Remember the script also enforces a MAX_LIMIT of 20 internally"],"exampleFix":"// before\nnode scripts/consult.js --limit 0 security\nnode scripts/consult.js --limit 3.5 security\n// after\nnode scripts/consult.js --limit 5 security","handlingStrategy":"validation","validationCode":"// Validate the limit value before passing it to consult.js\nfunction isValidPositiveInteger(value) {\n  return /^[1-9]\\d*$/.test(String(value || ''));\n}\n\nconst limit = process.env.CONSULT_LIMIT || '5';\nif (!isValidPositiveInteger(limit)) {\n  console.error(`Invalid limit: ${limit}. Must be a positive integer (1 or greater).`);\n  process.exit(1);\n}\n// Now safe to use: node scripts/consult.js --limit ${limit} ...","typeGuard":"// Type guard for a positive integer string\nfunction isPositiveIntegerString(value) {\n  return typeof value === 'string' && /^[1-9]\\d*$/.test(value);\n}","tryCatchPattern":"try {\n  const options = parseArgs(process.argv);\n} catch (error) {\n  if (error.message.includes('must be a positive integer')) {\n    console.error('The --limit value must be a positive integer like 1, 5, or 20.');\n    process.exit(2);\n  }\n  throw error;\n}","preventionTips":["Never pass 0, negative numbers, decimals, or non-numeric strings to --limit","Remember the maximum allowed value is 20 (MAX_LIMIT)","When deriving --limit from a variable, validate it with /^[1-9]\\d*$/ before use"],"tags":["cli","argument-parsing","validation","consult"],"backgroundTag":null,"analyzedSha":"01e15490f04e29cfefe3896951f43db46994d8ee","analyzedAt":"2026-08-13T00:31:08.655Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}