{"record":{"id":"2e1e15f777230ee3","repo":"jackwener/OpenCLI","slug":"npm-label-must-be-a-positive-integer","errorCode":null,"errorMessage":"npm ${label} must be a positive integer","messagePattern":"npm (.+?) must be a positive integer","errorType":"validation","errorClass":"ArgumentError","httpStatus":null,"severity":"error","filePath":"clis/npm/utils.js","lineNumber":37,"sourceCode":"    const s = String(value ?? '').trim();\n    if (!s) throw new ArgumentError('npm package name is required (e.g. \"react\", \"@vercel/og\")');\n    if (s.length > 214) {\n        throw new ArgumentError(`npm package name \"${value}\" is too long (max 214 chars)`);\n    }\n    if (!PKG_NAME.test(s)) {\n        throw new ArgumentError(\n            `npm package name \"${value}\" is not a valid registry name`,\n            'Names are 1–214 chars of lowercase a-z / 0-9 / \"-._\" (scoped form: \"@scope/name\").',\n        );\n    }\n    return s;\n}\n\nexport function requireBoundedInt(value, defaultValue, maxValue, label = 'limit') {\n    const raw = value ?? defaultValue;\n    const n = typeof raw === 'number' ? raw : Number(raw);\n    if (!Number.isInteger(n) || n <= 0) {\n        throw new ArgumentError(`npm ${label} must be a positive integer`);\n    }\n    if (n > maxValue) {\n        throw new ArgumentError(`npm ${label} must be <= ${maxValue}`);\n    }\n    return n;\n}\n\nexport async function npmFetch(url, label) {\n    let resp;\n    try {\n        resp = await fetch(url, { headers: { 'user-agent': UA, accept: 'application/json' } });\n    }\n    catch (err) {\n        throw new CommandExecutionError(\n            `${label} request failed: ${err?.message ?? err}`,\n            'Check that registry.npmjs.org / api.npmjs.org are reachable from this network.',\n        );\n    }","sourceCodeStart":19,"sourceCodeEnd":55,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/npm/utils.js#L19-L55","documentation":"requireBoundedInt coerces its value to a number and requires a positive integer, throwing ArgumentError `npm ${label} must be a positive integer` otherwise (default label 'limit'). It guards pagination inputs like `limit` in the search command before a request is made; undefined falls back to the default, so only explicit bad values trigger this.","triggerScenarios":"Passing limit as a non-integer (20.5), zero, a negative number, or a non-numeric string ('twenty', '', 'abc') — any value that Number() fails to turn into a positive integer.","commonSituations":"CLI flag parsed as a string containing '0' or a negative; user typo `--limit -1`; NaN-producing strings from config; fractional limits computed by division in scripts.","solutions":["Pass a positive integer: `{ limit: 20 }`, or omit the argument to use the default (20).","For CLI/config strings, coerce with Number.parseInt and validate before calling.","Guard inputs yourself: reject n <= 0 or non-integers upstream with your own message.","Catch ArgumentError and re-prompt with the valid range (1..maxValue)."],"exampleFix":"// before\nawait npmSearch({ query: 'react', limit: '-5' }); // ArgumentError\n// after\nconst raw = Number.parseInt(process.env.LIMIT ?? '20', 10);\nconst limit = Number.isInteger(raw) && raw > 0 ? raw : 20;\nawait npmSearch({ query: 'react', limit });","handlingStrategy":"validation","validationCode":"function toPositiveInt(v, dflt) {\n  if (v == null) return dflt;\n  const n = typeof v === 'number' ? v : Number(v);\n  return Number.isInteger(n) && n > 0 ? n : dflt;\n}\nconst limit = toPositiveInt(args.limit, 20);","typeGuard":"function isPositiveInt(v) {\n  return typeof v === 'number' && Number.isInteger(v) && v > 0;\n}","tryCatchPattern":"try {\n  return await npmSearch({ query, limit });\n} catch (e) {\n  if (e.name === 'ArgumentError' && /positive integer/.test(e.message)) {\n    return await npmSearch({ query, limit: 20 }); // fall back to default\n  }\n  throw e;\n}","preventionTips":["Coerce CLI/config strings with Number.parseInt and validate before passing.","Never pass fractional or negative limits; default is 20.","Centralize numeric-arg parsing for all commands.","Document valid ranges in CLI help text."],"tags":["validation","argument-error","npm","integer-validation","pagination"],"backgroundTag":"invalid-argument","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}