{"record":{"id":"079a62162270bf29","repo":"jackwener/OpenCLI","slug":"label-must-be-a-non-negative-integer-got-jso","errorCode":null,"errorMessage":"${label} must be a non-negative integer, got ${JSON.stringify(value)}","messagePattern":"(.+?) must be a non-negative integer, got (.+?)","errorType":"validation","errorClass":"ArgumentError","httpStatus":null,"severity":"error","filePath":"clis/_shared/search-adapter.js","lineNumber":27,"sourceCode":"}\n\nexport function requireBoundedInteger(value, defaultValue, min, max, label) {\n  const raw = value ?? defaultValue;\n  const parsed = typeof raw === 'number' ? raw : Number(raw);\n  if (!Number.isInteger(parsed)) {\n    throw new ArgumentError(`${label} must be an integer between ${min} and ${max}, got ${JSON.stringify(value)}`);\n  }\n  if (parsed < min || parsed > max) {\n    throw new ArgumentError(`${label} must be between ${min} and ${max}, got ${parsed}`);\n  }\n  return parsed;\n}\n\nexport function requireNonNegativeInteger(value, defaultValue, label) {\n  const raw = value ?? defaultValue;\n  const parsed = typeof raw === 'number' ? raw : Number(raw);\n  if (!Number.isInteger(parsed) || parsed < 0) {\n    throw new ArgumentError(`${label} must be a non-negative integer, got ${JSON.stringify(value)}`);\n  }\n  return parsed;\n}\n\nexport function unwrapBrowserResult(value) {\n  if (value && typeof value === 'object' && !Array.isArray(value) && 'session' in value && 'data' in value) {\n    return value.data;\n  }\n  return value;\n}\n\nexport function requireRows(value, label) {\n  const rows = unwrapBrowserResult(value);\n  if (!Array.isArray(rows)) {\n    throw new CommandExecutionError(`${label} returned an unexpected payload shape; expected an array of result rows.`);\n  }\n  return rows;\n}","sourceCodeStart":9,"sourceCodeEnd":45,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/_shared/search-adapter.js#L9-L45","documentation":"requireNonNegativeInteger validates a numeric CLI argument (falling back to defaultValue when null/undefined) and throws an ArgumentError from the shared search adapter when the value is not an integer or is negative. The message includes the JSON-stringified original value so you can see exactly what was passed. It guards helpers like page-size/offset parameters in browser-backed search commands.","triggerScenarios":"Calling a search CLI command with a negative number (e.g. --limit -1), a non-numeric string (e.g. --limit 'abc'), or a float (e.g. --offset 2.5) where requireNonNegativeInteger is used as the arg validator.","commonSituations":"Copy-pasted flags with stray characters (e.g. '--limit 10,'), shell scripts interpolating empty or malformed variables into the flag, units accidentally included ('--limit 20px'), and scripts using 0-based negatives from elsewhere.","solutions":["Pass a whole non-negative integer for the flagged argument (check the command's --help for valid values).","If omitting the flag is acceptable, drop it so the defaultValue is used.","If you pass the value programmatically, coerce/validate first: Number.isInteger(Number(v)) && Number(v) >= 0.","Quote the value in shell to avoid stray characters being split into the flag."],"exampleFix":"// before\nopencli mysearch results --limit -5\n// after\nopencli mysearch results --limit 10","handlingStrategy":"validation","validationCode":"function isValidLimit(v) {\n  const n = Number(v);\n  return Number.isInteger(n) && n >= 0;\n}\nif (!isValidLimit(myArg)) throw new Error('limit must be a non-negative integer');","typeGuard":"function isNonNegativeInt(v) {\n  return typeof v === 'number' && Number.isInteger(v) && v >= 0;\n}","tryCatchPattern":"try {\n  await runCommand(['mysearch', 'results', '--limit', String(n)]);\n} catch (e) {\n  if (e instanceof ArgumentError && /non-negative integer/.test(e.message)) {\n    console.error('Bad --limit value; using default.');\n  } else throw e;\n}","preventionTips":["Validate numeric flags with Number.isInteger and a range check before invoking CLI commands","Quote flag values in shell scripts to avoid stray characters","Never substitute raw user/env input directly into numeric flags without coercion","Prefer omitting flags to rely on documented defaults"],"tags":["cli","argument-validation","input-validation"],"backgroundTag":"invalid-argument-value","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}