Something went wrong. Try again.
link aggregator for FIRST Tech Challenge tools & resources firsttech.tools
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117export 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<string, unknown>;
// --- Validate categories --- if (!Array.isArray(obj.category)) { throw new Error("data/tools.toml: missing [[category]] array."); }
const categoryKeys = new Set<string>(); const categories: Category[] = [];
for (let i = 0; i < obj.category.length; i++) { const cat = obj.category[i] as Record<string, unknown>; 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<string, unknown>; 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 };}