From 3c76b878e9ddcc8fa52d00fc85f561bbb72c5dd1 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 27 Jan 2024 04:11:18 -0500 Subject: [PATCH] Fix Ajv validation warning (#576) * Fix Ajv validation warning * Add test --- .../dictionary-term-bank-v3-schema.json | 2 + test/ajv-schemas.test.js | 71 +++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 test/ajv-schemas.test.js diff --git a/ext/data/schemas/dictionary-term-bank-v3-schema.json b/ext/data/schemas/dictionary-term-bank-v3-schema.json index 066229c304..bcb1d5ed48 100644 --- a/ext/data/schemas/dictionary-term-bank-v3-schema.json +++ b/ext/data/schemas/dictionary-term-bank-v3-schema.json @@ -539,6 +539,8 @@ { "type": "array", "description": "Deinflection of the term to an uninflected term.", + "minItems": 2, + "maxItems": 2, "items": [ { "type": "string", diff --git a/test/ajv-schemas.test.js b/test/ajv-schemas.test.js new file mode 100644 index 0000000000..49545c1799 --- /dev/null +++ b/test/ajv-schemas.test.js @@ -0,0 +1,71 @@ +/* + * Copyright (C) 2024 Yomitan Authors + * + * 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 . + */ + +import Ajv from 'ajv'; +import standaloneCode from 'ajv/dist/standalone/index.js'; +import {readFileSync, readdirSync} from 'fs'; +import {dirname, join} from 'path'; +import {fileURLToPath} from 'url'; +import {describe, test} from 'vitest'; +import {parseJson} from '../dev/json.js'; + +/** + * @param {string} path + * @returns {import('ajv').AnySchema} + */ +function loadSchema(path) { + return parseJson(readFileSync(path, {encoding: 'utf8'})); +} + +const extDir = join(dirname(fileURLToPath(import.meta.url)), '..', 'ext'); + +describe('Ajv schema compilation', () => { + const schemaDir = join(extDir, 'data/schemas/'); + const schemaFileNames = readdirSync(schemaDir); + /** @type {{name: string, schema: import('ajv').AnySchema}[]} */ + const schemaTestCases = []; + for (const schemaFileName of schemaFileNames) { + schemaTestCases.push({name: schemaFileName, schema: loadSchema(join(schemaDir, schemaFileName))}); + } + + describe.each(schemaTestCases)('Validating $name', ({schema}) => { + test('Compiles without warnings', ({expect}) => { + /** @type {string[]} */ + const messages = []; + /** + * @param {...unknown} args + */ + const log = (...args) => { + messages.push(args.join(' ')); + }; + const ajv = new Ajv({ + schemas: [schema], + code: {source: true, esm: true}, + allowUnionTypes: true, + logger: { + log, + warn: log, + error: log + } + }); + standaloneCode(ajv); + if (messages.length > 0) { + expect.fail(messages.join('\n')); + } + }); + }); +});