jackwener/OpenCLI · error · ArgumentError

wikidata entity id "${value}" is not a valid Q/P/L identifie

Error message

wikidata entity id "${value}" is not a valid Q/P/L identifier

What it means

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.

Source

Thrown at clis/wikidata/utils.js:41

export function requireBoundedInt(value, defaultValue, maxValue, label = 'limit') {
    const raw = value ?? defaultValue;
    const n = typeof raw === 'number' ? raw : Number(raw);
    if (!Number.isInteger(n) || n <= 0) {
        throw new ArgumentError(`wikidata ${label} must be a positive integer`);
    }
    if (n > maxValue) {
        throw new ArgumentError(`wikidata ${label} must be <= ${maxValue}`);
    }
    return n;
}

export function requireEntityId(value) {
    const raw = String(value ?? '').trim().toUpperCase();
    if (!raw) throw new ArgumentError('wikidata entity id is required (e.g. "Q937")');
    // Tolerate URL-paste like `https://www.wikidata.org/wiki/Q937`.
    const stripped = raw.replace(/^HTTPS?:\/\/[^/]+\/WIKI\//i, '');
    if (!ENTITY_ID_PATTERN.test(stripped)) {
        throw new ArgumentError(
            `wikidata entity id "${value}" is not a valid Q/P/L identifier`,
            'Expected format: "Q<digits>" (item), "P<digits>" (property), or "L<digits>" (lexeme).',
        );
    }
    return stripped;
}

export function requireLanguage(value, defaultValue = 'en') {
    const raw = String(value ?? defaultValue).trim().toLowerCase();
    // Wikidata language codes are 2-3 letter ISO 639 codes plus optional region (`zh-hans`).
    if (!/^[a-z]{2,3}(-[a-z]{2,8})?$/.test(raw)) {
        throw new ArgumentError(
            `wikidata language "${value}" is not a valid language code`,
            'Expected an ISO 639 language code such as "en", "fr", "zh", "zh-hans".',
        );
    }
    return raw;
}

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Use the canonical form: Q<digits>, P<digits>, or L<digits>, e.g. Q937
  2. Strip any URL decorations — keep only the final /wiki/<ID> path segment; the adapter auto-strips a plain /wiki/ prefix
  3. Verify the ID at wikidata.org/wiki/<ID> resolves to an entity
  4. Obtain a valid QID via `wikidata search` instead of hand-typing

Example fix

// before
await runCli(['wikidata', 'entity', 'Douglas Adams']);
// after
await runCli(['wikidata', 'entity', 'Q937']); // Q + digits, P/L also accepted
Defensive patterns

Strategy: validation

Validate before calling

const ENTITY_ID_PATTERN = /^[QPL]\d+$/;
function normalizeEntityId(v) {
    const s = String(v ?? '').trim().toUpperCase().replace(/^HTTPS?:\/\/[^/]+\/WIKI\//i, '');
    if (!ENTITY_ID_PATTERN.test(s)) throw new Error(`not a Q/P/L id: ${v}`);
    return s;
}
await runCli(['wikidata', 'entity', normalizeEntityId(raw)]);

Type guard

function isValidEntityId(v) { return /^[QPL]\d+$/.test(String(v ?? '').trim().toUpperCase().replace(/^HTTPS?:\/\/[^/]+\/WIKI\//i, '')); }

Try / catch

try {
    await runCli(['wikidata', 'entity', rawId]);
} catch (e) {
    if (/not a valid Q\/P\/L identifier/.test(e.message)) {
        console.error(`Bad entity id "${rawId}" — expected Q<digits>, P<digits>, or L<digits>`);
        process.exitCode = 2;
    } else throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of jackwener/OpenCLI@49907e53dc (2026-08-29). Data as JSON: /api/errors/bf5d00dc0605ee8b. Report an issue: GitHub.