{"record":{"id":"bf5d00dc0605ee8b","repo":"jackwener/OpenCLI","slug":"wikidata-entity-id-value-is-not-a-valid-q-p-l","errorCode":null,"errorMessage":"wikidata entity id \"${value}\" is not a valid Q/P/L identifier","messagePattern":"wikidata entity id \"(.+?)\" is not a valid Q/P/L identifier","errorType":"validation","errorClass":"ArgumentError","httpStatus":null,"severity":"error","filePath":"clis/wikidata/utils.js","lineNumber":41,"sourceCode":"export 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\")');\n    // Tolerate URL-paste like `https://www.wikidata.org/wiki/Q937`.\n    const stripped = raw.replace(/^HTTPS?:\\/\\/[^/]+\\/WIKI\\//i, '');\n    if (!ENTITY_ID_PATTERN.test(stripped)) {\n        throw new ArgumentError(\n            `wikidata entity id \"${value}\" is not a valid Q/P/L identifier`,\n            'Expected format: \"Q<digits>\" (item), \"P<digits>\" (property), or \"L<digits>\" (lexeme).',\n        );\n    }\n    return stripped;\n}\n\nexport function requireLanguage(value, defaultValue = 'en') {\n    const raw = String(value ?? defaultValue).trim().toLowerCase();\n    // Wikidata language codes are 2-3 letter ISO 639 codes plus optional region (`zh-hans`).\n    if (!/^[a-z]{2,3}(-[a-z]{2,8})?$/.test(raw)) {\n        throw new ArgumentError(\n            `wikidata language \"${value}\" is not a valid language code`,\n            'Expected an ISO 639 language code such as \"en\", \"fr\", \"zh\", \"zh-hans\".',\n        );\n    }\n    return raw;\n}","sourceCodeStart":23,"sourceCodeEnd":59,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/wikidata/utils.js#L23-L59","documentation":"requireEntityId accepts only IDs matching /^[QPL]\\d+$/ (items, properties, lexemes) after stripping an optional wiki-URL prefix. Anything else — wrong prefix, missing digits, embedded junk — throws this ArgumentError explaining the expected Q/P/L format. URL-pasted IDs like https://www.wikidata.org/wiki/Q937 are tolerated.","triggerScenarios":"Calling `wikidata entity` with an ID like 'Q937 ' handled fine but '937', 'q-937', 'Q937#P31', 'Item:Q937', or a full URL with a path segment beyond /wiki/ — the stripped value fails the pattern.","commonSituations":"Pasting a Wikidata URL with query params or fragments; passing a bare numeric ID without the Q prefix; passing a Wikipedia page title instead of a Wikidata QID; lowercase-with-extra-characters from manual typing.","solutions":["Use the canonical form: Q<digits>, P<digits>, or L<digits>, e.g. Q937","Strip any URL decorations — keep only the final /wiki/<ID> path segment; the adapter auto-strips a plain /wiki/ prefix","Verify the ID at wikidata.org/wiki/<ID> resolves to an entity","Obtain a valid QID via `wikidata search` instead of hand-typing"],"exampleFix":"// before\nawait runCli(['wikidata', 'entity', 'Douglas Adams']);\n// after\nawait runCli(['wikidata', 'entity', 'Q937']); // Q + digits, P/L also accepted","handlingStrategy":"validation","validationCode":"const ENTITY_ID_PATTERN = /^[QPL]\\d+$/;\nfunction normalizeEntityId(v) {\n    const s = String(v ?? '').trim().toUpperCase().replace(/^HTTPS?:\\/\\/[^/]+\\/WIKI\\//i, '');\n    if (!ENTITY_ID_PATTERN.test(s)) throw new Error(`not a Q/P/L id: ${v}`);\n    return s;\n}\nawait runCli(['wikidata', 'entity', normalizeEntityId(raw)]);","typeGuard":"function isValidEntityId(v) { return /^[QPL]\\d+$/.test(String(v ?? '').trim().toUpperCase().replace(/^HTTPS?:\\/\\/[^/]+\\/WIKI\\//i, '')); }","tryCatchPattern":"try {\n    await runCli(['wikidata', 'entity', rawId]);\n} catch (e) {\n    if (/not a valid Q\\/P\\/L identifier/.test(e.message)) {\n        console.error(`Bad entity id \"${rawId}\" — expected Q<digits>, P<digits>, or L<digits>`);\n        process.exitCode = 2;\n    } else throw e;\n}","preventionTips":["Extract only the final /wiki/<ID> segment when pasting URLs","Keep the Q/P/L prefix — never pass a bare number","Don't pass Wikipedia titles; resolve them to QIDs via search first","Validate IDs with the /^^[QPL]\\d+$^/ pattern in pipelines"],"tags":["validation","arguments","format"],"backgroundTag":"invalid-argument","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}