checkedLexicon

tcmts.dev.lexicons.checkedLexicon · #d26v36hv · term · runs anywhere

A lexicon document, checked against the Lexicon spec (as @atcute/lexicon-doc reads one): an error says where it isn't one.

import { lexiconDoc } from "@atcute/lexicon-doc";
import * as v from "valibot";

export function checkedLexicon(doc: Record<string, unknown>): Record<string, unknown> {
  const r = v.safeParse(lexiconDoc, doc);
  if (!r.success)
    throw new Error(
      `${String(doc.id)} isn't a lexicon document: ${r.issues
        .map((i) => `${i.path?.map((p) => String(p.key)).join(".") ?? ""}: ${i.message}`)
        .join("; ")}`,
    );
  return doc;
}

used by