aboutsummaryrefslogtreecommitdiff
path: root/dev
diff options
context:
space:
mode:
Diffstat (limited to 'dev')
-rw-r--r--dev/dictionary-validate.js63
1 files changed, 33 insertions, 30 deletions
diff --git a/dev/dictionary-validate.js b/dev/dictionary-validate.js
index 8953dbf8..18bba99e 100644
--- a/dev/dictionary-validate.js
+++ b/dev/dictionary-validate.js
@@ -40,35 +40,33 @@ function readSchema(relativeFileName) {
/**
* @param {import('dev/schema-validate').ValidateMode} mode
* @param {import('jszip')} zip
- * @param {string} fileNameFormat
- * @param {import('dev/dictionary-validate').Schema} schema
+ * @param {import('dev/dictionary-validate').SchemasDetails} schemasDetails
*/
-async function validateDictionaryBanks(mode, zip, fileNameFormat, schema) {
- let jsonSchema;
- try {
- jsonSchema = createJsonSchema(mode, schema);
- } catch (e) {
- const e2 = toError(e);
- e2.message += `\n(in file ${fileNameFormat})}`;
- throw e2;
- }
- let index = 1;
- while (true) {
- const fileName = fileNameFormat.replace(/\?/, `${index}`);
+async function validateDictionaryBanks(mode, zip, schemasDetails) {
+ for (const [fileName, file] of Object.entries(zip.files)) {
+ for (const [fileNameFormat, schema] of schemasDetails) {
+ if (!fileNameFormat.test(fileName)) { continue; }
- const file = zip.files[fileName];
- if (!file) { break; }
+ let jsonSchema;
+ try {
+ jsonSchema = createJsonSchema(mode, schema);
+ } catch (e) {
+ const e2 = toError(e);
+ e2.message += `\n(in file ${fileName})}`;
+ throw e2;
+ }
- const data = parseJson(await file.async('string'));
- try {
- jsonSchema.validate(data);
- } catch (e) {
- const e2 = toError(e);
- e2.message += `\n(in file ${fileName})}`;
- throw e2;
- }
+ const data = parseJson(await file.async('string'));
- ++index;
+ try {
+ jsonSchema.validate(data);
+ } catch (e) {
+ const e2 = toError(e);
+ e2.message += `\n(in file ${fileName})}`;
+ throw e2;
+ }
+ break;
+ }
}
}
@@ -98,11 +96,16 @@ export async function validateDictionary(mode, archive, schemas) {
throw e2;
}
- 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);
+ /** @type {import('dev/dictionary-validate').SchemasDetails} */
+ const schemasDetails = [
+ [/^term_bank_(\d+)\.json$/, version === 1 ? schemas.termBankV1 : schemas.termBankV3],
+ [/^term_meta_bank_(\d+)\.json$/, schemas.termMetaBankV3],
+ [/^kanji_bank_(\d+)\.json$/, version === 1 ? schemas.kanjiBankV1 : schemas.kanjiBankV3],
+ [/^kanji_meta_bank_(\d+)\.json$/, schemas.kanjiMetaBankV3],
+ [/^tag_bank_(\d+)\.json$/, schemas.tagBankV3]
+ ];
+
+ await validateDictionaryBanks(mode, archive, schemasDetails);
}
/**