can1357/oh-my-pi · error · OmpTypeError

invalid index signature pattern ${keyDefinition}

Error message

invalid index signature pattern ${keyDefinition}

What it means

An index-signature key written as a /regex/ literal must compile; if new RegExp(regex[1], regex[2]) throws, parseObjectDefinition wraps that failure into this OmpTypeError naming the offending key definition. It means the pattern between the slashes is not a syntactically valid JavaScript regular expression.

Source

Thrown at packages/omptype/src/ir.ts:1361

			} else {
				if (Array.isArray(val) && val.length === 3 && val[1] === "=") {
					throw new OmpTypeError("index signatures cannot specify a default");
				}
				value = parseDef(val, resolve);
				if (isEmbedded(val) && val.hasDefault) {
					throw new OmpTypeError("index signatures cannot specify a default");
				}
			}
			const keyDefinition = originalKey.slice(1, -1);
			const regex = /^\/((?:\\.|[^\\/])*)\/([dgimsuvy]*)$/.exec(keyDefinition);
			let key: IR;
			if (regex === null) {
				key = parseDef(keyDefinition, resolve);
			} else {
				try {
					key = patternIR(new RegExp(regex[1], regex[2]));
				} catch {
					throw new OmpTypeError(`invalid index signature pattern ${keyDefinition}`);
				}
			}
			const objectIndexes = indexes ?? { patterns: [] };
			indexes = objectIndexes;
			indexKeyKind(key, value, props, objectIndexes);
			if (simple && (!isSimpleIR(key) || !isSimpleIR(value))) simple = false;
			continue;
		}
		const escapedOptional = typeof originalKey === "string" && originalKey.endsWith("\\?");
		const escapedMeta =
			typeof originalKey === "string" &&
			(originalKey === "\\+" || originalKey === "\\..." || originalKey.startsWith("\\["));
		const rawKey = escapedOptional
			? `${originalKey.slice(0, -2)}?`
			: escapedMeta
				? originalKey.slice(1)
				: originalKey;
		const opt = typeof rawKey === "string" && !escapedOptional && !escapedMeta && rawKey.endsWith("?");

View on GitHub (pinned to 9690622007)

Solutions

  1. Fix the regex literal in the key so it compiles in new RegExp
  2. Remove or correct invalid regex flags (allowed: d g i m s u v y)
  3. If you meant a literal key, drop the surrounding slashes and escape special chars instead
  4. Test the pattern in isolation with new RegExp(pattern, flags) before putting it in the schema

Example fix

// before
type({ "[/^(id_|name)/guu]": "string" }) // duplicate 'u' flag throws
// after
type({ "[/^(id_|name)/gu]": "string" })
Defensive patterns

Strategy: validation

Validate before calling

function validateRegexKey(keyDef) {
  const m = /^\/((?:\\.|[^\\/])*)\/([dgimsuvy]*)$/.exec(keyDef);
  if (m === null) return true; // not a regex literal, fine
  try { new RegExp(m[1], m[2]); return true; } catch { return false; }
}

Type guard

const isValidRegexKey = (k) => { const m = /^\/((?:\\.|[^\\/])*)\/([dgimsuvy]*)$/.exec(k); return m === null || (() => { try { new RegExp(m[1], m[2]); return true; } catch { return false; } })(); };

Try / catch

try { const T = type(def); } catch (e) {
  if (String(e.message).startsWith("invalid index signature pattern")) {
    // surface the offending key to the schema author
  } else throw e;
}

Prevention

When it happens

Trigger: A bracketed object key of the form "/pattern/flags" where pattern contains an unterminated escape, an invalid group, or the flags string contains characters outside dgimsuvy — i.e. any input making the RegExp constructor throw inside the ir.ts index-signature branch.

Common situations: Hand-written regex keys in schema strings with a typo (unbalanced paren, stray backslash); invalid flag letters like /a/q or /a/gg; copying regexes with lookahead variants unsupported by the runtime.

Related errors


AI-assisted analysis of can1357/oh-my-pi@9690622007 (2026-08-31). Data as JSON: /api/errors/219dc8dedfccce8d. Report an issue: GitHub.