2020-02-23 18:05:48 +00:00
|
|
|
/*
|
2022-02-03 01:43:10 +00:00
|
|
|
* Copyright (C) 2020-2022 Yomichan Authors
|
2020-02-23 18:05:48 +00:00
|
|
|
*
|
|
|
|
* This program 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.
|
|
|
|
*
|
|
|
|
* This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2020-02-08 18:14:30 +00:00
|
|
|
const fs = require('fs');
|
2021-05-22 19:19:56 +00:00
|
|
|
const {performance} = require('perf_hooks');
|
2020-09-19 23:04:28 +00:00
|
|
|
const {VM} = require('./vm');
|
2020-02-08 18:14:30 +00:00
|
|
|
|
2020-03-03 03:20:47 +00:00
|
|
|
const vm = new VM();
|
2020-06-28 16:38:34 +00:00
|
|
|
vm.execute([
|
2021-02-14 03:52:28 +00:00
|
|
|
'js/core.js',
|
|
|
|
'js/general/cache-map.js',
|
2021-02-14 16:19:54 +00:00
|
|
|
'js/data/json-schema.js'
|
2020-06-28 16:38:34 +00:00
|
|
|
]);
|
2021-05-22 19:45:20 +00:00
|
|
|
const JsonSchema = vm.get('JsonSchema');
|
2020-02-08 18:14:30 +00:00
|
|
|
|
2022-05-17 00:09:38 +00:00
|
|
|
class JsonSchemaAjv {
|
|
|
|
constructor(schema) {
|
|
|
|
const Ajv = require('ajv');
|
|
|
|
const ajv = new Ajv({
|
|
|
|
meta: false,
|
|
|
|
strictTuples: false,
|
|
|
|
allowUnionTypes: true
|
|
|
|
});
|
|
|
|
ajv.addMetaSchema(require('ajv/dist/refs/json-schema-draft-07.json'));
|
|
|
|
this._validate = ajv.compile(schema);
|
|
|
|
}
|
|
|
|
|
|
|
|
validate(data) {
|
|
|
|
if (this._validate(data)) { return; }
|
2022-10-17 02:08:03 +00:00
|
|
|
const {errors} = this._validate;
|
|
|
|
const error = new Error('Schema validation failed');
|
|
|
|
error.data = JSON.parse(JSON.stringify(errors));
|
|
|
|
throw error;
|
2022-05-17 00:09:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function createJsonSchema(mode, schema) {
|
|
|
|
switch (mode) {
|
|
|
|
case 'ajv': return new JsonSchemaAjv(schema);
|
|
|
|
default: return new JsonSchema(schema);
|
|
|
|
}
|
|
|
|
}
|
2020-02-08 18:14:30 +00:00
|
|
|
|
|
|
|
function main() {
|
|
|
|
const args = process.argv.slice(2);
|
|
|
|
if (args.length < 2) {
|
|
|
|
console.log([
|
|
|
|
'Usage:',
|
2022-05-17 00:09:38 +00:00
|
|
|
' node schema-validate [--ajv] <schema-file-name> <data-file-names>...'
|
2020-02-08 18:14:30 +00:00
|
|
|
].join('\n'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-05-17 00:09:38 +00:00
|
|
|
let mode = null;
|
|
|
|
if (args[0] === '--ajv') {
|
|
|
|
mode = 'ajv';
|
|
|
|
args.splice(0, 1);
|
|
|
|
}
|
|
|
|
|
2020-02-08 18:14:30 +00:00
|
|
|
const schemaSource = fs.readFileSync(args[0], {encoding: 'utf8'});
|
|
|
|
const schema = JSON.parse(schemaSource);
|
|
|
|
|
|
|
|
for (const dataFileName of args.slice(1)) {
|
2021-05-22 19:19:56 +00:00
|
|
|
const start = performance.now();
|
2020-02-08 18:14:30 +00:00
|
|
|
try {
|
|
|
|
console.log(`Validating ${dataFileName}...`);
|
|
|
|
const dataSource = fs.readFileSync(dataFileName, {encoding: 'utf8'});
|
|
|
|
const data = JSON.parse(dataSource);
|
2022-05-17 00:09:38 +00:00
|
|
|
createJsonSchema(mode, schema).validate(data);
|
2021-05-22 19:19:56 +00:00
|
|
|
const end = performance.now();
|
|
|
|
console.log(`No issues detected (${((end - start) / 1000).toFixed(2)}s)`);
|
2020-02-08 18:14:30 +00:00
|
|
|
} catch (e) {
|
2021-05-22 19:19:56 +00:00
|
|
|
const end = performance.now();
|
|
|
|
console.log(`Encountered an error (${((end - start) / 1000).toFixed(2)}s)`);
|
2020-02-08 18:14:30 +00:00
|
|
|
console.warn(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-18 03:30:16 +00:00
|
|
|
if (require.main === module) { main(); }
|
2022-05-17 00:09:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
createJsonSchema
|
|
|
|
};
|