export interface Category { key: string; label: string; } export interface Tool { name: string; url: string; description: string; categories: string[]; author?: string; source?: string; official: boolean; } export interface SiteData { categories: Category[]; tools: Tool[]; } function isValidUrl(value: string): boolean { try { new URL(value); return true; } catch { return false; } } export function validate(raw: unknown): SiteData { if (typeof raw !== "object" || raw === null || Array.isArray(raw)) { throw new Error("TOML root must be an object."); } const obj = raw as Record; // --- Validate categories --- if (!Array.isArray(obj.category)) { throw new Error("data/tools.toml: missing [[category]] array."); } const categoryKeys = new Set(); const categories: Category[] = []; for (let i = 0; i < obj.category.length; i++) { const cat = obj.category[i] as Record; if (typeof cat.key !== "string" || cat.key.trim() === "") { throw new Error(`category[${i}]: "key" is required and must be a non-empty string.`); } if (typeof cat.label !== "string" || cat.label.trim() === "") { throw new Error(`category[${i}] (key="${cat.key}"): "label" is required and must be a non-empty string.`); } if (categoryKeys.has(cat.key)) { throw new Error(`Duplicate category key: "${cat.key}". Category keys must be unique.`); } categoryKeys.add(cat.key); categories.push({ key: cat.key, label: cat.label }); } // --- Validate tools --- if (!Array.isArray(obj.tool)) { throw new Error("data/tools.toml: missing [[tool]] array."); } const tools: Tool[] = []; for (let i = 0; i < obj.tool.length; i++) { const t = obj.tool[i] as Record; const id = `tool[${i}]${typeof t.name === "string" ? ` ("${t.name}")` : ""}`; if (typeof t.name !== "string" || t.name.trim() === "") { throw new Error(`${id}: "name" is required and must be a non-empty string.`); } if (typeof t.url !== "string" || t.url.trim() === "") { throw new Error(`${id}: "url" is required and must be a non-empty string.`); } if (!isValidUrl(t.url)) { throw new Error(`${id}: "url" is not a valid URL: "${t.url}".`); } if (typeof t.description !== "string" || t.description.trim() === "") { throw new Error(`${id}: "description" is required and must be a non-empty string.`); } if (!Array.isArray(t.categories) || t.categories.length === 0) { throw new Error(`${id}: "categories" is required and must be a non-empty array.`); } for (const catKey of t.categories) { if (typeof catKey !== "string") { throw new Error(`${id}: each entry in "categories" must be a string.`); } if (!categoryKeys.has(catKey)) { throw new Error( `${id}: unknown category key "${catKey}". ` + `Valid keys: ${[...categoryKeys].map((k) => `"${k}"`).join(", ")}.` ); } } if (t.source !== undefined) { if (typeof t.source !== "string" || !isValidUrl(t.source)) { throw new Error(`${id}: "source" must be a valid URL when provided, got: "${t.source}".`); } } tools.push({ name: t.name, url: t.url, description: t.description, categories: t.categories as string[], author: typeof t.author === "string" ? t.author : undefined, source: typeof t.source === "string" ? t.source : undefined, official: t.official === true, }); } return { categories, tools }; }