summaryrefslogtreecommitdiff
path: root/ext/js/language/dictionary-importer.js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2021-05-22 15:45:20 -0400
committerGitHub <noreply@github.com>2021-05-22 15:45:20 -0400
commitd16739a83a20e1729e08dbcbbc155be15972d146 (patch)
treef7c12fa946a688750365ecf0f66227fcad4f927d /ext/js/language/dictionary-importer.js
parentb48052ff320f1a68aac317158c4c757a70b14f04 (diff)
Json schema validation improvements (#1697)
* Create new JsonSchema class * Add proxy handler * Update tests * Update validation scripts * Update backend * Update audio downloader * Update options util * Update dictionary importer * Update json schema file reference * Remove old json-schema.js * Rename new json-schema.js * Update file names * Rename class
Diffstat (limited to 'ext/js/language/dictionary-importer.js')
-rw-r--r--ext/js/language/dictionary-importer.js24
1 files changed, 14 insertions, 10 deletions
diff --git a/ext/js/language/dictionary-importer.js b/ext/js/language/dictionary-importer.js
index e893ffb6..9365683b 100644
--- a/ext/js/language/dictionary-importer.js
+++ b/ext/js/language/dictionary-importer.js
@@ -17,14 +17,13 @@
/* global
* JSZip
- * JsonSchemaValidator
+ * JsonSchema
* MediaUtil
*/
class DictionaryImporter {
constructor() {
this._schemas = new Map();
- this._jsonSchemaValidator = new JsonSchemaValidator();
}
async importDictionary(dictionaryDatabase, archiveSource, details, onProgress) {
@@ -235,22 +234,27 @@ class DictionaryImporter {
return schemaPromise;
}
- schemaPromise = this._fetchJsonAsset(fileName);
+ schemaPromise = this._createSchema(fileName);
this._schemas.set(fileName, schemaPromise);
return schemaPromise;
}
+ async _createSchema(fileName) {
+ const schema = await this._fetchJsonAsset(fileName);
+ return new JsonSchema(schema);
+ }
+
_validateJsonSchema(value, schema, fileName) {
try {
- this._jsonSchemaValidator.validate(value, schema);
+ schema.validate(value);
} catch (e) {
throw this._formatSchemaError(e, fileName);
}
}
_formatSchemaError(e, fileName) {
- const valuePathString = this._getSchemaErrorPathString(e.info.valuePath, 'dictionary');
- const schemaPathString = this._getSchemaErrorPathString(e.info.schemaPath, 'schema');
+ const valuePathString = this._getSchemaErrorPathString(e.valuePath, 'dictionary');
+ const schemaPathString = this._getSchemaErrorPathString(e.schemaPath, 'schema');
const e2 = new Error(`Dictionary has invalid data in '${fileName}' for value '${valuePathString}', validated against '${schemaPathString}': ${e.message}`);
e2.data = e;
@@ -260,16 +264,16 @@ class DictionaryImporter {
_getSchemaErrorPathString(infoList, base='') {
let result = base;
- for (const [part] of infoList) {
- switch (typeof part) {
+ for (const {path} of infoList) {
+ switch (typeof path) {
case 'string':
if (result.length > 0) {
result += '.';
}
- result += part;
+ result += path;
break;
case 'number':
- result += `[${part}]`;
+ result += `[${path}]`;
break;
}
}