{"record":{"id":"270a380fd8b2c815","repo":"jackwener/OpenCLI","slug":"nuget-label-must-be-a-positive-integer","errorCode":null,"errorMessage":"nuget ${label} must be a positive integer","messagePattern":"nuget (.+?) must be a positive integer","errorType":"validation","errorClass":"ArgumentError","httpStatus":null,"severity":"error","filePath":"clis/nuget/utils.js","lineNumber":28,"sourceCode":"export const NUGET_REGISTRATION_BASE = 'https://api.nuget.org/v3/registration5-semver1';\nconst UA = 'opencli-nuget-adapter/1.0 (+https://github.com/jackwener/opencli; mailto:opencli@example.com)';\n\n// NuGet ID grammar (NuGet docs §package-id): up to 100 chars, alnum + `.` + `_` + `-`,\n// must start with letter/digit. Case-insensitive; we lowercase for the registration URL\n// because NuGet's CDN is case-sensitive on the path.\nconst PACKAGE_ID_PATTERN = /^[A-Za-z0-9](?:[A-Za-z0-9._-]{0,99})$/;\n\nexport function requireString(value, label) {\n    const s = String(value ?? '').trim();\n    if (!s) throw new ArgumentError(`nuget ${label} cannot be empty`);\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(`nuget ${label} must be a positive integer`);\n    }\n    if (n > maxValue) {\n        throw new ArgumentError(`nuget ${label} must be <= ${maxValue}`);\n    }\n    return n;\n}\n\nexport function requirePackageId(value) {\n    const raw = String(value ?? '').trim();\n    if (!raw) throw new ArgumentError('nuget package id is required (e.g. \"Newtonsoft.Json\")');\n    if (!PACKAGE_ID_PATTERN.test(raw)) {\n        throw new ArgumentError(\n            `nuget package id \"${value}\" is not a valid NuGet identifier`,\n            'NuGet IDs are 1-100 chars: letters/digits/`.`/`_`/`-`, starting with letter or digit.',\n        );\n    }\n    return raw;\n}","sourceCodeStart":10,"sourceCodeEnd":46,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/nuget/utils.js#L10-L46","documentation":"This ArgumentError is thrown by requireBoundedInt in clis/nuget/utils.js when a caller passes a value for a bounded numeric option (label defaults to 'limit') that is not an integer or is <= 0. The library uses it to validate paging/limit parameters before they are sent to the NuGet API, failing fast on bad input rather than producing a confusing upstream request. Note that string values are coerced with Number(), so 'abc' or '' also land here.","triggerScenarios":"Calling a nuget command or API wrapper with limit (or any labeled bounded option) set to 0, a negative number, a non-numeric string like 'ten' or '', a float like 2.5, or NaN.","commonSituations":"Users passing --limit 0 expecting 'unlimited'; shell scripts interpolating empty variables into CLI flags; parsing user input from forms without numeric validation; locale-formatted numbers ('1,000') failing Number() coercion.","solutions":["Pass a positive integer for the option, e.g. limit: 20","If the value comes from a string, ensure it is a clean integer string like '20' (trim; no thousand separators)","Omit the option entirely to let the built-in defaultValue apply","Sanitize with Number.isInteger(Number(value)) && Number(value) > 0 before calling"],"exampleFix":"// before\nawait nuget.limit(ctx, { limit: 0 });\n// after\nawait nuget.limit(ctx, { limit: 20 }); // or omit limit to use the default","handlingStrategy":"validation","validationCode":"function isPositiveInt(v) { const n = typeof v === 'number' ? v : Number(v); return Number.isInteger(n) && n > 0; }\nif (!isPositiveInt(opts.limit)) throw new Error(`limit must be a positive integer, got ${JSON.stringify(opts.limit)}`);","typeGuard":"function isBoundedInt(v, max) { const n = typeof v === 'number' ? v : Number(v); return Number.isInteger(n) && n > 0 && n <= max; }","tryCatchPattern":"try {\n  await nuget.limit(ctx, { limit });\n} catch (e) {\n  if (e instanceof ArgumentError && e.message.includes('must be a positive integer')) {\n    limit = 20; // fall back to default\n  } else throw e;\n}","preventionTips":["Coerce and validate numeric CLI/option input before calling the library","Treat 0 as invalid — it is not 'unlimited' here","Never pass locale-formatted number strings ('1,000')","Let omitted options fall through to the library default"],"tags":["validation","arguments","input-validation","nuget"],"backgroundTag":"invalid-numeric-argument","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}