can1357/oh-my-pi · error · OmpTypeError

invalid date literal in "${src}"

Error message

invalid date literal in "${src}"

What it means

A date literal `d'...'` was correctly closed, but its contents do not parse to a valid `Date` (or a bare-integer epoch that yields NaN). The tokenizer validates the constructed date and throws `OmpTypeError` on `Number.isNaN(value.valueOf())`.

Source

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

const SIMPLE_OPS = "|&()[]=?%,#";

function tokenize(src: string): Tok[] {
	const toks: Tok[] = [];
	let i = 0;
	const n = src.length;
	while (i < n) {
		const c = src[i];
		if (c === " " || c === "\t" || c === "\n" || c === "\r") {
			i++;
			continue;
		}
		if (c === "d" && (src[i + 1] === "'" || src[i + 1] === '"')) {
			const quote = src[i + 1];
			const end = src.indexOf(quote, i + 2);
			if (end < 0) throw new OmpTypeError(`unterminated date literal in "${src}"`);
			const source = src.slice(i + 2, end).trim();
			const value = /^\d+$/.test(source) ? new Date(Number(source)) : new Date(source);
			if (Number.isNaN(value.valueOf())) throw new OmpTypeError(`invalid date literal in "${src}"`);
			toks.push({ t: "date", v: value });
			i = end + 1;
			continue;
		}
		if (c === "'" || c === '"') {
			let j = i + 1;
			let value = "";
			for (; j < n && src[j] !== c; j++) {
				if (src[j] === "\\") {
					j++;
					if (j >= n) break;
				}
				value += src[j];
			}
			if (j >= n) throw new OmpTypeError(`unterminated string literal in "${src}"`);
			toks.push({ t: "str", v: value });
			i = j + 1;
			continue;

View on GitHub (pinned to 9690622007)

Solutions

  1. Use an ISO 8601 format the `Date` constructor accepts: `d'2024-01-01'` or `d'2024-01-01T00:00:00Z'`.
  2. For epoch values, supply a valid numeric millisecond timestamp.
  3. Pre-validate the date string with `!Number.isNaN(new Date(s).valueOf())` before building the expression.

Example fix

// before
type(`date > d'31/12/2024'`); // invalid
// after
type(`date > d'2024-12-31'`);
Defensive patterns

Strategy: validation

Validate before calling

const d = sourceOrEpoch;
const value = /^\d+$/.test(d) ? new Date(Number(d)) : new Date(d);
if (Number.isNaN(value.valueOf())) throw new Error(`invalid date: ${d}`);

Try / catch

try {
  const t = new TypeExpression(src);
} catch (err) {
  if (err instanceof Error && err.message.includes("invalid date literal")) {
    throw new Error(`Use ISO 8601 (e.g. d'2024-01-01'): ${err.message}`);
  }
  throw err;
}

Prevention

When it happens

Trigger: `d'not-a-date'`, `d'2024-13-45'` (invalid month/day), `d''` (empty), or a numeric epoch string that produces an invalid date.

Common situations: Typos in dates in config/constraint strings; locale-dependent date formats (`d'31/12/2024'` parsed as month 31); empty literals from empty template variables.

Related errors


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