From 5e86bf6a0598792e4b177fb88d5ebeba68197fb8 Mon Sep 17 00:00:00 2001 From: David Anson Date: Sat, 25 Nov 2023 18:33:41 -0800 Subject: [PATCH] Convert JSON schema validation to use Ajv. --- package.json | 2 +- test/markdownlint-cli2-test.js | 63 +++++++++++++++++----------------- 2 files changed, 32 insertions(+), 33 deletions(-) diff --git a/package.json b/package.json index 5041450d..d837f90f 100644 --- a/package.json +++ b/package.json @@ -74,6 +74,7 @@ }, "devDependencies": { "@iktakahiro/markdown-it-katex": "4.0.1", + "ajv": "8.12.0", "ava": "5.3.1", "c8": "8.0.1", "cpy": "11.0.0", @@ -83,7 +84,6 @@ "eslint-plugin-n": "16.3.1", "eslint-plugin-unicorn": "49.0.0", "execa": "8.0.1", - "@hyperjump/json-schema": "1.6.5", "markdown-it-emoji": "2.0.2", "markdown-it-for-inline": "0.1.1", "markdownlint-cli2-formatter-codequality": "0.0.4", diff --git a/test/markdownlint-cli2-test.js b/test/markdownlint-cli2-test.js index 1809218a..f957c010 100644 --- a/test/markdownlint-cli2-test.js +++ b/test/markdownlint-cli2-test.js @@ -4,14 +4,17 @@ const fs = require("node:fs/promises"); const path = require("node:path"); +const Ajv = require("ajv"); const test = require("ava").default; const { "main": markdownlintCli2 } = require("../markdownlint-cli2.js"); const FsMock = require("./fs-mock"); -const jsonSchemaVersion = "http://json-schema.org/draft-07/schema#"; -const markdownlintConfigSchemaUri = "https://raw.githubusercontent.com/DavidAnson/markdownlint-cli2/v0.11.0/schema/markdownlint-config-schema.json"; const markdownlintConfigSchemaDefinition = require("../schema/markdownlint-config-schema.json"); const markdownlintCli2ConfigSchemaDefinition = require("../schema/markdownlint-cli2-config-schema.json"); +const ajvOptions = { + "allowUnionTypes": true, + "strictTuples": false +}; const outputFormatterLengthIs = (t, length) => (options) => { const { results } = options; @@ -61,22 +64,20 @@ test("README files", (t) => { }); test("validateMarkdownlintConfigSchema", async (t) => { - t.plan(24); + t.plan(23); // Validate schema - const { addSchema, validate } = - await import("@hyperjump/json-schema/draft-07"); - const schemaResult = await validate( - jsonSchemaVersion, - markdownlintConfigSchemaDefinition, - "BASIC" - ); - t.true(schemaResult.valid); + // @ts-ignore + const ajv = new Ajv(ajvOptions); + const validateConfigSchema = ajv.compile(markdownlintConfigSchemaDefinition); + // t.is( + // markdownlintConfigSchemaDefinition.$id, + // markdownlintConfigSchemaDefinition.properties.$schema.default + // ); + // Validate instances // @ts-ignore - addSchema(markdownlintConfigSchemaDefinition, markdownlintConfigSchemaUri); - const validateConfigSchema = await validate(markdownlintConfigSchemaUri); const { "default": stripJsonComments } = await import("strip-json-comments"); const { globby } = await import("globby"); const files = await globby( @@ -94,10 +95,10 @@ test("validateMarkdownlintConfigSchema", async (t) => { return Promise.all(files.map(async (file) => { const content = await fs.readFile(file, "utf8"); const json = JSON.parse(stripJsonComments(content)); - const instanceResult = validateConfigSchema(json, "BASIC"); - t.true( - instanceResult.valid, - `${file}\n${JSON.stringify(instanceResult, null, 2)}` + const instanceResult = validateConfigSchema(json); + t.truthy( + instanceResult, + `${file}\n${JSON.stringify(validateConfigSchema.errors, null, 2)}` ); })); }); @@ -106,21 +107,19 @@ test("validateMarkdownlintCli2ConfigSchema", async (t) => { t.plan(86); // Validate schema - const { addSchema, validate } = - await import("@hyperjump/json-schema/draft-07"); - const schemaResult = await validate( - jsonSchemaVersion, - markdownlintCli2ConfigSchemaDefinition, - "BASIC" + // @ts-ignore + const ajv = new Ajv(ajvOptions); + ajv.addSchema( + markdownlintConfigSchemaDefinition, + "https://raw.githubusercontent.com/DavidAnson/markdownlint-cli2/v0.11.0/schema/markdownlint-config-schema.json" + ); + const validateConfigSchema = ajv.compile(markdownlintCli2ConfigSchemaDefinition); + t.is( + markdownlintCli2ConfigSchemaDefinition.$id, + markdownlintCli2ConfigSchemaDefinition.properties.$schema.default ); - t.true(schemaResult.valid); // Validate instances - // @ts-ignore - addSchema(markdownlintConfigSchemaDefinition, markdownlintConfigSchemaUri); - // @ts-ignore - addSchema(markdownlintCli2ConfigSchemaDefinition); - const validateConfigSchema = await validate(markdownlintCli2ConfigSchemaDefinition.$id); const { "default": stripJsonComments } = await import("strip-json-comments"); const { globby } = await import("globby"); const files = await globby( @@ -140,9 +139,9 @@ test("validateMarkdownlintCli2ConfigSchema", async (t) => { const content = await fs.readFile(file, "utf8"); const json = JSON.parse(stripJsonComments(content)); const instanceResult = validateConfigSchema(json, "BASIC"); - t.true( - instanceResult.valid, - `${file}\n${JSON.stringify(instanceResult, null, 2)}` + t.truthy( + instanceResult, + `${file}\n${JSON.stringify(validateConfigSchema.errors, null, 2)}` ); })); });