test: extend schemas with data enums & add betterAvjErrors

This commit is contained in:
Klein Petr 2024-09-16 19:44:53 +02:00
parent 2cd582717c
commit 435304b3c3

View file

@ -1,5 +1,6 @@
import Ajv from "https://esm.sh/ajv@8.8.1?pin=v58"; import Ajv from "https://esm.sh/ajv@8.17.1?pin=v58";
import addFormats from "https://esm.sh/ajv-formats@2.1.1"; import addFormats from "https://esm.sh/ajv-formats@2.1.1";
import { betterAjvErrors } from 'https://esm.sh/@apideck/better-ajv-errors@0.3.6?pin=v58';
import yaml from "npm:js-yaml"; import yaml from "npm:js-yaml";
import { W3PData } from "./w3pdata.js"; import { W3PData } from "./w3pdata.js";
@ -7,7 +8,7 @@ import { W3PData } from "./w3pdata.js";
const w3pd = new W3PData(); const w3pd = new W3PData();
await w3pd.init(); await w3pd.init();
const ajv = new Ajv({ strict: false }); const ajv = new Ajv({ strict: false, allErrors: true });
addFormats(ajv); addFormats(ajv);
async function loadSchemas() { async function loadSchemas() {
@ -19,6 +20,28 @@ async function loadSchemas() {
return out; return out;
} }
function getDeepPropertiesKeys(obj, parentKey = '') {
let keys = [];
if (obj.hasOwnProperty('properties')) {
const properties = obj['properties'];
for (const key in properties) {
if (properties.hasOwnProperty(key)) {
const newKey = parentKey ? `${parentKey}.${key}` : key;
if (properties[key].hasOwnProperty('properties')) {
keys = keys.concat(getDeepPropertiesKeys(properties[key], newKey));
} else {
keys.push(newKey);
}
}
}
}
return keys;
}
const matrix = { const matrix = {
categories: "category", categories: "category",
projects: "project", projects: "project",
@ -32,9 +55,9 @@ const matrix = {
const schemaDir = "./schema"; const schemaDir = "./schema";
const schemas = await loadSchemas(); const schemas = await loadSchemas();
schemas.project.properties.categories.items.enum = w3pd.data.categories.map( schemas.rank.properties.references.items.properties.field.enum = getDeepPropertiesKeys(schemas.project);
(c) => c.id schemas.project.properties.categories.items.enum = w3pd.data.categories.map((c) => c.id);
); schemas.project.properties.usecases.items.enum = w3pd.data.usecases.map((c) => c.id);
for (const col of Object.keys(w3pd.data)) { for (const col of Object.keys(w3pd.data)) {
const validator = ajv.compile(schemas[matrix[col]]); const validator = ajv.compile(schemas[matrix[col]]);
@ -61,9 +84,9 @@ for (const col of Object.keys(w3pd.data)) {
if (Object.keys(item).length > 1) { if (Object.keys(item).length > 1) {
Deno.test(testName + " (schema)", () => { Deno.test(testName + " (schema)", () => {
if (!validator(item)) { if (!validator(item)) {
// throw validator.errors; const betterErrors = betterAjvErrors({ errors: validator.errors });
// log instead of throwing to proceed building all projects throw betterErrors;
console.log(validator.errors); console.log(betterErrors);
} }
}); });
} }