Skip to content

Commit

Permalink
Merge pull request moodle#140 from andrewnicols/jsonLint
Browse files Browse the repository at this point in the history
[repo] Ensure that projects.json is appropriately linted
  • Loading branch information
sarjona authored May 25, 2022
2 parents c204367 + b4e309b commit dad507d
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .lintstagedrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
"src/**/*.{js,jsx,ts,tsx,mjs}": [
"cspell --no-must-find-files --no-progress"
],
"data/projects.json": [
"yarn ajv --spec=draft2019 validate -s static/schema/projects.json -d"
],
"data/versions.json": [
"yarn ajv --spec=draft2019 validate -s static/schema/versions.json -d"
]
Expand Down
1 change: 0 additions & 1 deletion .vscode/extensions.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"davidanson.vscode-markdownlint",
"streetsidesoftware.code-spell-checker",
"dbaeumer.vscode-eslint",
"tberman.json-schema-validator",
"silvenon.mdx",
"gamunu.vscode-yarn",
"EditorConfig.EditorConfig"
Expand Down
1 change: 1 addition & 0 deletions cspell.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ const config = {
],
ignorePaths: [
'*.js',
'*.mjs',
'CHANGELOG.md',
'package.json',
'yarn.lock',
Expand Down
80 changes: 80 additions & 0 deletions scripts/jsonlint.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#!/usr/bin/env node --experimental-json-modules
/**
* Copyright (c) Moodle Pty Ltd.
*
* Moodle is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Moodle is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Moodle. If not, see <http://www.gnu.org/licenses/>.
*/

/* eslint-disable import/no-extraneous-dependencies */
import path from 'path';
import { readFile } from 'fs/promises';
import { program } from 'commander';
import $RefParser from '@apidevtools/json-schema-ref-parser';
import Ajv from 'ajv/dist/2019.js';
import Draft7Schema from 'ajv/dist/refs/json-schema-draft-07.json' assert {type: 'json'};

program
.name('jsonlint')
.description('Check a JSON file for validity against a schema')
.version('1.0.0');

const getSchemaDefinition = async (schema) => {
console.log(`Getting schema at ${schema}`);
try {
return await $RefParser.dereference(schema);
} catch (err) {
console.error(err);
process.exit(1);
}
return Promise.reject();
};

const getSchema = (filepath, schema) => {
if (schema.startsWith('http://') || schema.startsWith('https://')) {
return schema;
}

if (schema.startsWith('file://') || schema.startsWith('.')) {
return path.resolve(path.dirname(filepath), schema);
}

return schema;
};

program
.arguments('<filepath>', 'Path to the file to check')
.action(async (filename) => {
const filepath = path.resolve(filename);
const rawJson = await readFile(filepath, { encoding: 'utf-8' });
const json = JSON.parse(rawJson);
if (typeof json.$schema === 'undefined') {
console.log('Nothing to validate against');
process.exit(0);
}

const schema = await getSchemaDefinition(getSchema(filepath, json.$schema));

const ajv = new Ajv();
ajv.addMetaSchema(Draft7Schema);

const valid = ajv.validate(schema, json);
if (valid) {
console.log(`No errors found validating ${filepath} against ${json.$schema}`);
} else {
console.log(ajv.errors);
process.exit(1);
}
});

program.parse();
2 changes: 1 addition & 1 deletion static/schema/projects.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"title": "JSON schema for the Moodle Project list",
"$schema": "http://json-schema.org/draft-04/schema",
"$id": "https://moodle.github.io/devdocs/schema/projects.json",
"$schema": "https://json-schema.org/draft/2019-09/schema",
"type": "object",
"properties": {
"projects": {
Expand Down

0 comments on commit dad507d

Please sign in to comment.