Skip to content

Commit

Permalink
Convert JSON schema validation to use Ajv.
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidAnson committed Nov 26, 2023
1 parent 21daa4a commit 5e86bf6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 33 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
63 changes: 31 additions & 32 deletions test/markdownlint-cli2-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand All @@ -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)}`
);
}));
});
Expand All @@ -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(
Expand All @@ -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)}`
);
}));
});
Expand Down

0 comments on commit 5e86bf6

Please sign in to comment.