-
Notifications
You must be signed in to change notification settings - Fork 384
/
Copy pathjsonlint.mjs
executable file
·80 lines (69 loc) · 2.55 KB
/
jsonlint.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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();