From 57ce414ca81682a71288018a4d9001604002ec23 Mon Sep 17 00:00:00 2001 From: Wojtek Kosior Date: Tue, 1 Mar 2022 11:29:26 +0100 Subject: 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/ --- common/jsonschema/validator.js | 360 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 360 insertions(+) create mode 100644 common/jsonschema/validator.js (limited to 'common/jsonschema/validator.js') diff --git a/common/jsonschema/validator.js b/common/jsonschema/validator.js new file mode 100644 index 0000000..4d8e0cf --- /dev/null +++ b/common/jsonschema/validator.js @@ -0,0 +1,360 @@ +/* SPDX-License-Identifier: MIT + * + * jsonschema is licensed under MIT license. + * + * Copyright (C) 2012-2015 Tom de Grunt + * + * 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/attribute.js +#IMPORT common/jsonschema/helpers.js + +#FROM common/jsonschema/scan.js IMPORT scan AS scanSchema + +var ValidatorResult = helpers.ValidatorResult; +var ValidatorResultError = helpers.ValidatorResultError; +var SchemaError = helpers.SchemaError; +var SchemaContext = helpers.SchemaContext; +//var anonymousBase = 'vnd.jsonschema:///'; +var anonymousBase = '/'; + +/** + * Creates a new Validator object + * @name Validator + * @constructor + */ +var Validator = function Validator () { + // Allow a validator instance to override global custom formats or to have their + // own custom formats. + this.customFormats = Object.create(Validator.prototype.customFormats); + this.schemas = {}; + this.unresolvedRefs = []; + + // Use Object.create to make this extensible without Validator instances stepping on each other's toes. + this.types = Object.create(types); + this.attributes = Object.create(attribute.validators); +}; + +// Allow formats to be registered globally. +Validator.prototype.customFormats = {}; + +// Hint at the presence of a property +Validator.prototype.schemas = null; +Validator.prototype.types = null; +Validator.prototype.attributes = null; +Validator.prototype.unresolvedRefs = null; + +/** + * Adds a schema with a certain urn to the Validator instance. + * @param schema + * @param urn + * @return {Object} + */ +Validator.prototype.addSchema = function addSchema (schema, base) { + var self = this; + if (!schema) { + return null; + } + var scan = scanSchema(base||anonymousBase, schema); + var ourUri = base || schema.$id || schema.id; + for(var uri in scan.id){ + this.schemas[uri] = scan.id[uri]; + } + for(var uri in scan.ref){ + // If this schema is already defined, it will be filtered out by the next step + this.unresolvedRefs.push(uri); + } + // Remove newly defined schemas from unresolvedRefs + this.unresolvedRefs = this.unresolvedRefs.filter(function(uri){ + return typeof self.schemas[uri]==='undefined'; + }); + return this.schemas[ourUri]; +}; + +Validator.prototype.addSubSchemaArray = function addSubSchemaArray(baseuri, schemas) { + if(!Array.isArray(schemas)) return; + for(var i=0; i", schema); + } + var subschema = helpers.objectGetPath(ctx.schemas[document], fragment.substr(1)); + if(subschema===undefined){ + throw new SchemaError("no such schema " + fragment + " located in <" + document + ">", schema); + } + return {subschema: subschema, switchSchema: switchSchema}; +}; + +/** + * Tests whether the instance if of a certain type. + * @private + * @param instance + * @param schema + * @param options + * @param ctx + * @param type + * @return {boolean} + */ +Validator.prototype.testType = function validateType (instance, schema, options, ctx, type) { + if(type===undefined){ + return; + }else if(type===null){ + throw new SchemaError('Unexpected null in "type" keyword'); + } + if (typeof this.types[type] == 'function') { + return this.types[type].call(this, instance); + } + if (type && typeof type == 'object') { + var res = this.validateSchema(instance, type, options, ctx); + return res === undefined || !(res && res.errors.length); + } + // Undefined or properties not on the list are acceptable, same as not being defined + return true; +}; + +var types = Validator.prototype.types = {}; +types.string = function testString (instance) { + return typeof instance == 'string'; +}; +types.number = function testNumber (instance) { + // isFinite returns false for NaN, Infinity, and -Infinity + return typeof instance == 'number' && isFinite(instance); +}; +types.integer = function testInteger (instance) { + return (typeof instance == 'number') && instance % 1 === 0; +}; +types.boolean = function testBoolean (instance) { + return typeof instance == 'boolean'; +}; +types.array = function testArray (instance) { + return Array.isArray(instance); +}; +types['null'] = function testNull (instance) { + return instance === null; +}; +types.date = function testDate (instance) { + return instance instanceof Date; +}; +types.any = function testAny (instance) { + return true; +}; +types.object = function testObject (instance) { + // TODO: fix this - see #15 + return instance && (typeof instance === 'object') && !(Array.isArray(instance)) && !(instance instanceof Date); +}; + +#EXPORT Validator -- cgit v1.2.3