summaryrefslogtreecommitdiff
path: root/dev/dictionary-validate.js
diff options
context:
space:
mode:
authorCashew <52880648+Scrub1492@users.noreply.github.com>2024-01-20 10:43:27 +0700
committerGitHub <noreply@github.com>2024-01-20 03:43:27 +0000
commit0011afe435f565dc5669f030da64e2c6e10ce05e (patch)
treeb7cc9598d6a958cbfdd15fff32294ab85dda550e /dev/dictionary-validate.js
parentbe267e1994f8fda2ef68562c5c3a5edc9442a8a8 (diff)
improve dictionary validation (#520)
* improve dictionary validation * fix * improve performance * add undefined check * rename types * rename types * rename
Diffstat (limited to 'dev/dictionary-validate.js')
-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);
}
/**