diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2022-05-16 20:09:38 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-16 20:09:38 -0400 |
commit | 5a723034b8ac9eb86854bdcb16f624d18fa17178 (patch) | |
tree | 3c33468b7e93c779b0010b92ac88a230222ee33d /dev | |
parent | 84c9231a5e9733a6300d2edf1718b520467d7dc5 (diff) |
Dictionary validate updates (#2137)
* Reuse JsomSchema instance
* Install ajv
* Add support for using ajv as a JSON schema validator
* Update usage
Diffstat (limited to 'dev')
-rw-r--r-- | dev/dictionary-validate.js | 63 | ||||
-rw-r--r-- | dev/schema-validate.js | 41 |
2 files changed, 79 insertions, 25 deletions
diff --git a/dev/dictionary-validate.js b/dev/dictionary-validate.js index cf33c787..7f31c61f 100644 --- a/dev/dictionary-validate.js +++ b/dev/dictionary-validate.js @@ -19,15 +19,7 @@ const fs = require('fs'); const path = require('path'); const {performance} = require('perf_hooks'); const {JSZip} = require('./util'); -const {VM} = require('./vm'); - -const vm = new VM(); -vm.execute([ - 'js/core.js', - 'js/general/cache-map.js', - 'js/data/json-schema.js' -]); -const JsonSchema = vm.get('JsonSchema'); +const {createJsonSchema} = require('./schema-validate'); function readSchema(relativeFileName) { @@ -37,7 +29,14 @@ function readSchema(relativeFileName) { } -async function validateDictionaryBanks(zip, fileNameFormat, schema) { +async function validateDictionaryBanks(mode, zip, fileNameFormat, schema) { + let jsonSchema; + try { + jsonSchema = createJsonSchema(mode, schema); + } catch (e) { + e.message += `\n(in file ${fileNameFormat})}`; + throw e; + } let index = 1; while (true) { const fileName = fileNameFormat.replace(/\?/, index); @@ -46,14 +45,20 @@ async function validateDictionaryBanks(zip, fileNameFormat, schema) { if (!file) { break; } const data = JSON.parse(await file.async('string')); - new JsonSchema(schema).validate(data); + try { + jsonSchema.validate(data); + } catch (e) { + e.message += `\n(in file ${fileName})}`; + throw e; + } ++index; } } -async function validateDictionary(archive, schemas) { - const indexFile = archive.files['index.json']; +async function validateDictionary(mode, archive, schemas) { + const fileName = 'index.json'; + const indexFile = archive.files[fileName]; if (!indexFile) { throw new Error('No dictionary index found in archive'); } @@ -61,13 +66,19 @@ async function validateDictionary(archive, schemas) { const index = JSON.parse(await indexFile.async('string')); const version = index.format || index.version; - new JsonSchema(schemas.index).validate(index); + try { + const jsonSchema = createJsonSchema(mode, schemas.index); + jsonSchema.validate(index); + } catch (e) { + e.message += `\n(in file ${fileName})}`; + throw e; + } - await validateDictionaryBanks(archive, 'term_bank_?.json', version === 1 ? schemas.termBankV1 : schemas.termBankV3); - await validateDictionaryBanks(archive, 'term_meta_bank_?.json', schemas.termMetaBankV3); - await validateDictionaryBanks(archive, 'kanji_bank_?.json', version === 1 ? schemas.kanjiBankV1 : schemas.kanjiBankV3); - await validateDictionaryBanks(archive, 'kanji_meta_bank_?.json', schemas.kanjiMetaBankV3); - await validateDictionaryBanks(archive, 'tag_bank_?.json', schemas.tagBankV3); + await validateDictionaryBanks(mode, archive, 'term_bank_?.json', version === 1 ? schemas.termBankV1 : schemas.termBankV3); + await validateDictionaryBanks(mode, archive, 'term_meta_bank_?.json', schemas.termMetaBankV3); + await validateDictionaryBanks(mode, archive, 'kanji_bank_?.json', version === 1 ? schemas.kanjiBankV1 : schemas.kanjiBankV3); + await validateDictionaryBanks(mode, archive, 'kanji_meta_bank_?.json', schemas.kanjiMetaBankV3); + await validateDictionaryBanks(mode, archive, 'tag_bank_?.json', schemas.tagBankV3); } function getSchemas() { @@ -84,7 +95,7 @@ function getSchemas() { } -async function testDictionaryFiles(dictionaryFileNames) { +async function testDictionaryFiles(mode, dictionaryFileNames) { const schemas = getSchemas(); for (const dictionaryFileName of dictionaryFileNames) { @@ -93,7 +104,7 @@ async function testDictionaryFiles(dictionaryFileNames) { console.log(`Validating ${dictionaryFileName}...`); const source = fs.readFileSync(dictionaryFileName); const archive = await JSZip.loadAsync(source); - await validateDictionary(archive, schemas); + await validateDictionary(mode, archive, schemas); const end = performance.now(); console.log(`No issues detected (${((end - start) / 1000).toFixed(2)}s)`); } catch (e) { @@ -110,12 +121,18 @@ async function main() { if (dictionaryFileNames.length === 0) { console.log([ 'Usage:', - ' node dictionary-validate <dictionary-file-names>...' + ' node dictionary-validate [--ajv] <dictionary-file-names>...' ].join('\n')); return; } - await testDictionaryFiles(dictionaryFileNames); + let mode = null; + if (dictionaryFileNames[0] === '--ajv') { + mode = 'ajv'; + dictionaryFileNames.splice(0, 1); + } + + await testDictionaryFiles(mode, dictionaryFileNames); } diff --git a/dev/schema-validate.js b/dev/schema-validate.js index 2f0e2090..1a69ca48 100644 --- a/dev/schema-validate.js +++ b/dev/schema-validate.js @@ -27,17 +27,49 @@ vm.execute([ ]); const JsonSchema = vm.get('JsonSchema'); +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; } + const {errors} = this._validate(data); + const message = errors.map((e) => e.toString()).join('\n'); + throw new Error(message); + } +} + +function createJsonSchema(mode, schema) { + switch (mode) { + case 'ajv': return new JsonSchemaAjv(schema); + default: return new JsonSchema(schema); + } +} function main() { const args = process.argv.slice(2); if (args.length < 2) { console.log([ 'Usage:', - ' node schema-validate <schema-file-name> <data-file-names>...' + ' node schema-validate [--ajv] <schema-file-name> <data-file-names>...' ].join('\n')); return; } + let mode = null; + if (args[0] === '--ajv') { + mode = 'ajv'; + args.splice(0, 1); + } + const schemaSource = fs.readFileSync(args[0], {encoding: 'utf8'}); const schema = JSON.parse(schemaSource); @@ -47,7 +79,7 @@ function main() { console.log(`Validating ${dataFileName}...`); const dataSource = fs.readFileSync(dataFileName, {encoding: 'utf8'}); const data = JSON.parse(dataSource); - new JsonSchema(schema).validate(data); + createJsonSchema(mode, schema).validate(data); const end = performance.now(); console.log(`No issues detected (${((end - start) / 1000).toFixed(2)}s)`); } catch (e) { @@ -60,3 +92,8 @@ function main() { if (require.main === module) { main(); } + + +module.exports = { + createJsonSchema +}; |