aboutsummaryrefslogtreecommitdiff
path: root/common/jsonschema/scan.js
diff options
context:
space:
mode:
authorWojtek Kosior <koszko@koszko.org>2022-03-01 11:29:26 +0100
committerWojtek Kosior <koszko@koszko.org>2022-03-04 16:13:35 +0100
commit57ce414ca81682a71288018a4d9001604002ec23 (patch)
treeb94c9c4cc9b5e4f6a12a82ed4f1ce66537f93525 /common/jsonschema/scan.js
parent17e66592321d24a51b18019af84cbc664144d2de (diff)
downloadbrowser-extension-57ce414ca81682a71288018a4d9001604002ec23.tar.gz
browser-extension-57ce414ca81682a71288018a4d9001604002ec23.zip
validate repository responses against JSON schemas
* compute_scripts.awk (include_file): don't enforce specific path format on #INCLUDE'd files * .gitmodules, schemas: add Haketilo JSON schemas subrepo * html/install.js (InstallView): import schema validator and run it against downloaded mapping and resource definitions * html/repo_query.js (RepoEntry): import schema validator and run it against obtained query results * test/haketilo_test/unit/test_install.py (test_install_normal_usage, test_install_dialogs): use underscore instead of hyphen in item identifiers * test/haketilo_test/unit/test_install.py (test_install_dialogs): adapt error message test cases to new handling method of invalid JSON instanced * test/haketilo_test/unit/test_repo_query.py (test_repo_query_normal_usage): use underscore instead of hyphen in item identifiers * test/haketilo_test/unit/test_repo_query.py (test_repo_query_messages): use higher sample unsupported schema version to avoid having to modify the test case soon * test/haketilo_test/world_wide_library.py: use underscore instead of hyphen in item identifiers * common/jsonschema.js, common/jsonschema: adapt tdegrunt's jsonschema and include in Haketilo, load schema documents from schemas/
Diffstat (limited to 'common/jsonschema/scan.js')
-rw-r--r--common/jsonschema/scan.js99
1 files changed, 99 insertions, 0 deletions
diff --git a/common/jsonschema/scan.js b/common/jsonschema/scan.js
new file mode 100644
index 0000000..b5e5760
--- /dev/null
+++ b/common/jsonschema/scan.js
@@ -0,0 +1,99 @@
+/* SPDX-License-Identifier: MIT
+ *
+ * jsonschema is licensed under MIT license.
+ *
+ * Copyright (C) 2012-2015 Tom de Grunt <tom@degrunt.nl>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of
+ * this software and associated documentation files (the "Software"), to deal in
+ * the Software without restriction, including without limitation the rights to
+ * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
+ * of the Software, and to permit persons to whom the Software is furnished to do
+ * so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#IMPORT common/jsonschema/urllib_mock.js AS urilib
+#IMPORT common/jsonschema/helpers.js
+
+function SchemaScanResult(found, ref){
+ this.id = found;
+ this.ref = ref;
+}
+#EXPORT SchemaScanResult
+
+/**
+ * Adds a schema with a certain urn to the Validator instance.
+ * @param string uri
+ * @param object schema
+ * @return {Object}
+ */
+function scan(base, schema){
+ function scanSchema(baseuri, schema){
+ if(!schema || typeof schema!='object') return;
+ // Mark all referenced schemas so we can tell later which schemas are referred to, but never defined
+ if(schema.$ref){
+ var resolvedUri = urilib.resolve(baseuri, schema.$ref);
+ ref[resolvedUri] = ref[resolvedUri] ? ref[resolvedUri]+1 : 0;
+ return;
+ }
+ var id = schema.$id || schema.id;
+ var ourBase = id ? urilib.resolve(baseuri, id) : baseuri;
+ if (ourBase) {
+ // If there's no fragment, append an empty one
+ if(ourBase.indexOf('#')<0) ourBase += '#';
+ if(found[ourBase]){
+ if(!helpers.deepCompareStrict(found[ourBase], schema)){
+ throw new Error('Schema <'+ourBase+'> already exists with different definition');
+ }
+ return found[ourBase];
+ }
+ found[ourBase] = schema;
+ // strip trailing fragment
+ if(ourBase[ourBase.length-1]=='#'){
+ found[ourBase.substring(0, ourBase.length-1)] = schema;
+ }
+ }
+ scanArray(ourBase+'/items', (Array.isArray(schema.items)?schema.items:[schema.items]));
+ scanArray(ourBase+'/extends', (Array.isArray(schema.extends)?schema.extends:[schema.extends]));
+ scanSchema(ourBase+'/additionalItems', schema.additionalItems);
+ scanObject(ourBase+'/properties', schema.properties);
+ scanSchema(ourBase+'/additionalProperties', schema.additionalProperties);
+ scanObject(ourBase+'/definitions', schema.definitions);
+ scanObject(ourBase+'/patternProperties', schema.patternProperties);
+ scanObject(ourBase+'/dependencies', schema.dependencies);
+ scanArray(ourBase+'/disallow', schema.disallow);
+ scanArray(ourBase+'/allOf', schema.allOf);
+ scanArray(ourBase+'/anyOf', schema.anyOf);
+ scanArray(ourBase+'/oneOf', schema.oneOf);
+ scanSchema(ourBase+'/not', schema.not);
+ }
+ function scanArray(baseuri, schemas){
+ if(!Array.isArray(schemas)) return;
+ for(var i=0; i<schemas.length; i++){
+ scanSchema(baseuri+'/'+i, schemas[i]);
+ }
+ }
+ function scanObject(baseuri, schemas){
+ if(!schemas || typeof schemas!='object') return;
+ for(var p in schemas){
+ scanSchema(baseuri+'/'+p, schemas[p]);
+ }
+ }
+
+ var found = {};
+ var ref = {};
+ scanSchema(base, schema);
+ return new SchemaScanResult(found, ref);
+};
+#EXPORT scan