2023-10-07 20:48:35 +02:00
|
|
|
import Ajv from "https://esm.sh/ajv@8.8.1?pin=v58";
|
|
|
|
import addFormats from "https://esm.sh/ajv-formats@2.1.1";
|
|
|
|
import yaml from "npm:js-yaml";
|
|
|
|
|
2023-10-07 20:01:09 +02:00
|
|
|
import { W3PData } from "./w3pdata.js";
|
|
|
|
|
2023-10-07 20:48:35 +02:00
|
|
|
const w3pd = new W3PData();
|
|
|
|
await w3pd.init();
|
|
|
|
|
|
|
|
const ajv = new Ajv({ strict: false });
|
|
|
|
addFormats(ajv);
|
|
|
|
|
|
|
|
async function loadSchemas() {
|
|
|
|
const out = {};
|
|
|
|
for await (const f of Deno.readDir(schemaDir)) {
|
|
|
|
const col = f.name.split(".")[0];
|
|
|
|
out[col] = yaml.load(await Deno.readTextFile(`${schemaDir}/${f.name}`));
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
const matrix = {
|
|
|
|
categories: "category",
|
|
|
|
projects: "project",
|
2024-09-12 18:11:39 +02:00
|
|
|
assets: "asset",
|
|
|
|
ecosystems: "ecosystem",
|
|
|
|
features: "feature",
|
|
|
|
usecases: "usecase",
|
|
|
|
ranks: "rank",
|
2023-10-07 20:48:35 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const schemaDir = "./schema";
|
|
|
|
const schemas = await loadSchemas();
|
|
|
|
|
2024-05-18 11:30:19 +02:00
|
|
|
schemas.project.properties.categories.items.enum = w3pd.data.categories.map(
|
|
|
|
(c) => c.id
|
|
|
|
);
|
2023-10-08 01:16:03 +02:00
|
|
|
|
2023-10-07 20:48:35 +02:00
|
|
|
for (const col of Object.keys(w3pd.data)) {
|
|
|
|
const validator = ajv.compile(schemas[matrix[col]]);
|
2024-05-18 11:30:19 +02:00
|
|
|
const ids = [];
|
2023-10-07 20:01:09 +02:00
|
|
|
|
2023-10-07 20:48:35 +02:00
|
|
|
for (const item of w3pd.data[col]) {
|
2024-05-18 11:30:19 +02:00
|
|
|
delete item._path;
|
|
|
|
const testName =
|
|
|
|
`${col}/${item.id} ` +
|
|
|
|
(col === "projects"
|
|
|
|
? `[${
|
|
|
|
Array.isArray(item.categories)
|
|
|
|
? item.categories.join(", ")
|
|
|
|
: item.categories
|
|
|
|
}]`
|
|
|
|
: "");
|
2023-10-07 21:06:05 +02:00
|
|
|
|
2023-10-08 01:16:03 +02:00
|
|
|
if (ids.includes(item.id)) {
|
2024-05-18 11:30:19 +02:00
|
|
|
Deno.test(testName + " (id-duplicates)", () => {
|
|
|
|
throw { message: `Duplicate project id="${item.id}"` };
|
|
|
|
});
|
2023-10-07 21:06:05 +02:00
|
|
|
}
|
|
|
|
|
2024-05-18 11:30:19 +02:00
|
|
|
if (Object.keys(item).length > 1) {
|
|
|
|
Deno.test(testName + " (schema)", () => {
|
2023-10-08 01:16:03 +02:00
|
|
|
if (!validator(item)) {
|
2024-05-18 11:30:19 +02:00
|
|
|
// throw validator.errors;
|
|
|
|
// log instead of throwing to proceed building all projects
|
|
|
|
console.log(validator.errors);
|
2023-10-08 01:16:03 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2024-05-18 11:30:19 +02:00
|
|
|
ids.push(item.id);
|
2023-10-07 20:48:35 +02:00
|
|
|
}
|
|
|
|
}
|