{"record":{"id":"033b772b09a0487c","repo":"jackwener/OpenCLI","slug":"wikidata-label-cannot-be-empty","errorCode":null,"errorMessage":"wikidata ${label} cannot be empty","messagePattern":"wikidata (.+?) cannot be empty","errorType":"validation","errorClass":"ArgumentError","httpStatus":null,"severity":"error","filePath":"clis/wikidata/utils.js","lineNumber":19,"sourceCode":"// Shared helpers for the Wikidata adapters.\n//\n// Wikidata exposes two complementary public endpoints:\n//   • `wbsearchentities` on `www.wikidata.org/w/api.php` for keyword → Q-IDs\n//   • `Special:EntityData/<qid>.json` for the canonical entity dump\n// No API key. Anonymous traffic is rate-limited but generous; we set a polite UA.\nimport { ArgumentError, CommandExecutionError, EmptyResultError } from '@jackwener/opencli/errors';\n\nexport const WIKIDATA_BASE = 'https://www.wikidata.org';\nconst UA = 'opencli-wikidata-adapter/1.0 (+https://github.com/jackwener/opencli; mailto:opencli@example.com)';\n\n// Q-ID = an item; P-ID = a property; L-ID = a lexeme. We accept all three so the\n// adapter can be reused for properties / lexemes without a separate command, but\n// search only returns Q-IDs by default.\nconst ENTITY_ID_PATTERN = /^[QPL]\\d+$/;\n\nexport function requireString(value, label) {\n    const s = String(value ?? '').trim();\n    if (!s) throw new ArgumentError(`wikidata ${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(`wikidata ${label} must be a positive integer`);\n    }\n    if (n > maxValue) {\n        throw new ArgumentError(`wikidata ${label} must be <= ${maxValue}`);\n    }\n    return n;\n}\n\nexport function requireEntityId(value) {\n    const raw = String(value ?? '').trim().toUpperCase();\n    if (!raw) throw new ArgumentError('wikidata entity id is required (e.g. \"Q937\")');","sourceCodeStart":1,"sourceCodeEnd":37,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/wikidata/utils.js#L1-L37","documentation":"requireString is the wikidata adapter's shared guard for mandatory string arguments (e.g. the search query). It stringifies the value, trims it, and throws an ArgumentError if nothing remains. This fails fast with a clear message instead of sending an empty query to the Wikidata API.","triggerScenarios":"Calling requireString (directly or via the `query` argument path of wikidata search) with undefined, null, '', or a whitespace-only string for the labeled argument.","commonSituations":"An empty shell variable passed to the CLI (`wikidata search \"$Q\"` with Q unset); a config file with a blank query field; a script building args programmatically that drops the query.","solutions":["Provide a non-empty value for the labeled argument (e.g. the search query)","Check the environment variable or config key feeding the argument is actually set","Trim/validate inputs in your script before invoking the command"],"exampleFix":"// before\nconst query = process.env.QUERY; // may be undefined\nawait runCli(['wikidata', 'search', query]);\n// after\nconst query = process.env.QUERY?.trim();\nif (!query) throw new Error('QUERY env var must be a non-empty search term');\nawait runCli(['wikidata', 'search', query]);","handlingStrategy":"validation","validationCode":"const query = String(rawQuery ?? '').trim();\nif (!query) throw new Error('wikidata search requires a non-empty query');\nawait runCli(['wikidata', 'search', query]);","typeGuard":"function isNonEmptyString(v) { return typeof v === 'string' && v.trim().length > 0; }","tryCatchPattern":"try {\n    await runCli(['wikidata', 'search', query]);\n} catch (e) {\n    if (/cannot be empty/.test(e.message)) {\n        console.error('Provide a search term');\n        process.exitCode = 2;\n    } else throw e;\n}","preventionTips":["Always trim and check string args before invoking","Avoid passing env vars directly without a fallback","Fail fast in scripts when required inputs are blank","Use `${VAR:?}` shell guards for mandatory variables"],"tags":["validation","arguments","input-validation"],"backgroundTag":"invalid-argument","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}