From 0011afe435f565dc5669f030da64e2c6e10ce05e Mon Sep 17 00:00:00 2001 From: Cashew <52880648+Scrub1492@users.noreply.github.com> Date: Sat, 20 Jan 2024 10:43:27 +0700 Subject: improve dictionary validation (#520) * improve dictionary validation * fix * improve performance * add undefined check * rename types * rename types * rename --- dev/dictionary-validate.js | 63 ++++++++++++++++++++++++---------------------- 1 file changed, 33 insertions(+), 30 deletions(-) (limited to 'dev') 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); } /** -- cgit v1.2.3