{"record":{"id":"5b346181cd016cb4","repo":"jackwener/OpenCLI","slug":"invalid-subreddit-name","errorCode":null,"errorMessage":"Invalid subreddit name.","messagePattern":"Invalid subreddit name\\.","errorType":"validation","errorClass":"ArgumentError","httpStatus":null,"severity":"error","filePath":"clis/reddit/subreddit-info.js","lineNumber":19,"sourceCode":"import { ArgumentError, CommandExecutionError, EmptyResultError } from '@jackwener/opencli/errors';\nimport { cli, Strategy } from '@jackwener/opencli/registry';\n\n// Reddit subreddit names: 3–21 chars, letters/digits/underscore, must start\n// with a letter. Accept an optional `r/` prefix and normalise it off.\nconst SUBREDDIT_NAME_RE = /^[A-Za-z][A-Za-z0-9_]{2,20}$/;\n\nexport function parseSubredditName(raw) {\n    let name = String(raw || '').trim();\n    if (!name) {\n        throw new ArgumentError(\n            'Subreddit name is required.',\n            'Pass a subreddit name like `python` (or `r/python`).',\n        );\n    }\n    if (name.startsWith('/r/')) name = name.slice(3);\n    else if (name.startsWith('r/')) name = name.slice(2);\n    if (!SUBREDDIT_NAME_RE.test(name)) {\n        throw new ArgumentError(\n            'Invalid subreddit name.',\n            'Subreddit names are 3–21 characters, start with a letter, and contain only letters, digits, and underscores.',\n        );\n    }\n    return name;\n}\n\ncli({\n    site: 'reddit',\n    name: 'subreddit-info',\n    access: 'read',\n    description: 'Show metadata for a Reddit subreddit (subscribers, description, created date, NSFW)',\n    domain: 'reddit.com',\n    strategy: Strategy.COOKIE,\n    browser: true,\n    args: [\n        { name: 'name', type: 'string', required: true, positional: true, help: 'Subreddit name (no `r/` prefix needed)' },\n    ],","sourceCodeStart":1,"sourceCodeEnd":37,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/reddit/subreddit-info.js#L1-L37","documentation":"`parseSubredditName` throws this ArgumentError when the name, after stripping an optional `r/` or `/r/` prefix, fails SUBREDDIT_NAME_RE (/^[A-Za-z][A-Za-z0-9_]{2,20}$/). Valid subreddit names are 3–21 characters, must start with a letter, and contain only letters, digits, and underscores. The library validates locally so bad names never reach Reddit's API.","triggerScenarios":"Passing names like 'ab' (too short), '4chan' (starts with digit — though real subs cannot start with digits anyway), names with hyphens/dots/spaces, URLs like 'reddit.com/r/python' that were not reduced to just the name, or names longer than 21 characters.","commonSituations":"Pasting a full subreddit URL instead of the name; passing 'r/' with nothing after it; including query strings or trailing slashes; typos or non-Latin characters in the name.","solutions":["Pass only the bare subreddit name: `sub python` or `sub r/python`, not a URL","Strip 'https://www.reddit.com/r/' prefixes from pasted URLs, keeping just the name segment","Verify the name is 3–21 chars, starts with a letter, and uses only [A-Za-z0-9_]","Validate with the same regex in your own code before calling, to give a friendlier message"],"exampleFix":"// before\nawait runCli(['reddit', 'sub', 'https://www.reddit.com/r/AskProgramming/']); // invalid\n// after\nconst name = 'https://www.reddit.com/r/AskProgramming/'.match(/reddit\\.com\\/r\\/([A-Za-z0-9_]+)/)?.[1];\nawait runCli(['reddit', 'sub', name]); // 'AskProgramming'","handlingStrategy":"validation","validationCode":"// Mirror the library's own regex before calling:\nconst SUBREDDIT_NAME_RE = /^[A-Za-z][A-Za-z0-9_]{2,20}$/;\nfunction toSubredditName(raw) {\n  let name = String(raw ?? '').trim().replace(/^\\/r\\//, '').replace(/^r\\/, '');\n  name = name.replace(/^https?:\\/\\/[^/]*reddit\\.com\\/r\\//, '').split('/')[0];\n  if (!SUBREDDIT_NAME_RE.test(name)) throw new Error(`Invalid subreddit name: ${name}`);\n  return name;\n}","typeGuard":"function isValidSubredditName(v) {\n  return typeof v === 'string' && /^[A-Za-z][A-Za-z0-9_]{2,20}$/.test(v.replace(/^(\\/r\\/|r\\/)/, ''));\n}","tryCatchPattern":"try {\n  await runCli(['reddit', 'sub', raw]);\n} catch (e) {\n  if (e instanceof ArgumentError && /Invalid subreddit name/.test(e.message)) {\n    console.error('Names are 3-21 chars, start with a letter, [A-Za-z0-9_] only. Pass `python` or `r/python`.');\n  } else throw e;\n}","preventionTips":["Reduce pasted subreddit URLs to the bare name before calling","Reject names with hyphens, dots, spaces, or non-Latin characters early","Enforce the 3–21 char / letter-first rule in your own input layer"],"tags":["argument-validation","regex","reddit"],"backgroundTag":"schema-validation-failed","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}