diff options
author | Darius Jahandarie <djahandarie@gmail.com> | 2023-11-03 23:32:33 +0900 |
---|---|---|
committer | Darius Jahandarie <djahandarie@gmail.com> | 2023-11-03 23:52:30 +0900 |
commit | 376151096431d4362e4baaacf0cef4a534e169f7 (patch) | |
tree | 18536a224fd4d833aaf691d0363a3a4b59175dd7 /dev | |
parent | b64f51c3b13a46af4dd7f1e43048ac19c781ca7b (diff) |
Replace JsonSchema with ajv for dictionary validation
Diffstat (limited to 'dev')
-rw-r--r-- | dev/build.js | 16 |
1 files changed, 15 insertions, 1 deletions
diff --git a/dev/build.js b/dev/build.js index 24b1e2d0..3bfb5418 100644 --- a/dev/build.js +++ b/dev/build.js @@ -24,7 +24,8 @@ const childProcess = require('child_process'); const util = require('./util'); const {getAllFiles, getArgs, testMain} = util; const {ManifestUtil} = require('./manifest-util'); - +const Ajv = require('ajv'); +const standaloneCode = require('ajv/dist/standalone').default; async function createZip(directory, excludeFiles, outputFileName, sevenZipExes, onUpdate, dryRun) { try { @@ -130,6 +131,19 @@ async function build(buildDir, extDir, manifestUtil, variantNames, manifestPath, process.stdout.write(message); }; + process.stdout.write('Building schema validators using ajv\n'); + const schemaDir = path.join(extDir, 'data/schemas/'); + const schemaFileNames = fs.readdirSync(schemaDir); + const schemas = schemaFileNames.map((schemaFileName) => JSON.parse(fs.readFileSync(path.join(schemaDir, schemaFileName)))); + const ajv = new Ajv({schemas: schemas, code: {source: true, esm: true}}); + const moduleCode = standaloneCode(ajv); + + // https://github.com/ajv-validator/ajv/issues/2209 + const patchedModuleCode = moduleCode.replaceAll('require("ajv/dist/runtime/ucs2length").default', 'import("/lib/ucs2length.js").default'); + + fs.writeFileSync(path.join(extDir, 'lib/validate-schemas.js'), patchedModuleCode); + + process.stdout.write(`Version: ${yomitanVersion}...\n`); for (const variantName of variantNames) { |