affaan-m/ECC · error · Error

${label} must be a positive integer

Error message

${label} must be a positive integer

What it means

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.

Source

Thrown at scripts/consult.js:187

function tokenize(value) {
  const normalized = normalizeToken(value);
  if (!normalized) {
    return [];
  }

  const tokens = [];
  for (const token of normalized.split(/\s+/)) {
    if (!token || STOP_WORDS.has(token)) {
      continue;
    }
    tokens.push(...expandToken(token));
  }
  return [...new Set(tokens)];
}

function parsePositiveInteger(value, label) {
  if (!/^[1-9]\d*$/.test(String(value || ''))) {
    throw new Error(`${label} must be a positive integer`);
  }
  return Number(value);
}

function parseArgs(argv) {
  const args = argv.slice(2);
  const parsed = {
    queryParts: [],
    target: DEFAULT_TARGET,
    limit: DEFAULT_LIMIT,
    json: false,
    help: false,
  };

  if (args.includes('--help') || args.includes('-h')) {
    parsed.help = true;
    return parsed;
  }

View on GitHub (pinned to 01e15490f0)

Solutions

  1. Provide a positive integer starting with 1-9, e.g. --limit 5
  2. If deriving from a variable, validate it before passing: only pass --limit if the value is >= 1
  3. Remember the script also enforces a MAX_LIMIT of 20 internally

Example fix

// before
node scripts/consult.js --limit 0 security
node scripts/consult.js --limit 3.5 security
// after
node scripts/consult.js --limit 5 security
Defensive patterns

Strategy: validation

Validate before calling

// Validate the limit value before passing it to consult.js
function isValidPositiveInteger(value) {
  return /^[1-9]\d*$/.test(String(value || ''));
}

const limit = process.env.CONSULT_LIMIT || '5';
if (!isValidPositiveInteger(limit)) {
  console.error(`Invalid limit: ${limit}. Must be a positive integer (1 or greater).`);
  process.exit(1);
}
// Now safe to use: node scripts/consult.js --limit ${limit} ...

Type guard

// Type guard for a positive integer string
function isPositiveIntegerString(value) {
  return typeof value === 'string' && /^[1-9]\d*$/.test(value);
}

Try / catch

try {
  const options = parseArgs(process.argv);
} catch (error) {
  if (error.message.includes('must be a positive integer')) {
    console.error('The --limit value must be a positive integer like 1, 5, or 20.');
    process.exit(2);
  }
  throw error;
}

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Related errors


AI-assisted analysis of affaan-m/ECC@01e15490f0 (2026-08-13). Data as JSON: /api/errors/0429134d2d56f672. Report an issue: GitHub.