Project

General

Profile

« Previous | Next » 

Revision 57ce414c

Added by koszko over 1 year ago

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/

View differences:

html/repo_query.js
43 43

  
44 44
#IMPORT common/indexeddb.js AS haketilodb
45 45

  
46
#FROM common/browser.js   IMPORT browser
47
#FROM html/DOM_helpers.js IMPORT clone_template, Showable
48
#FROM common/entities.js  IMPORT item_id_string, version_string
49
#FROM html/install.js     IMPORT InstallView
46
#FROM common/browser.js    IMPORT browser
47
#FROM html/DOM_helpers.js  IMPORT clone_template, Showable
48
#FROM common/entities.js   IMPORT item_id_string, version_string
49
#FROM html/install.js      IMPORT InstallView
50
#FROM common/jsonschema.js IMPORT haketilo_validator, haketilo_schemas
50 51

  
51 52
const coll = new Intl.Collator();
52 53

  
......
68 69
    this.install_but.addEventListener("click", cb);
69 70
}
70 71

  
71
const query_schema_url_regex = new RegExp(
72
    "^https://hydrilla\\.koszko\\.org/schemas/api_query_result-1\\.([1-9][0-9]*\\.)*schema\\.json$"
73
);
74

  
75 72
function RepoEntry(query_view, repo_url) {
76 73
    Object.assign(this, clone_template("repo_query_single_repo"));
77 74
    Object.assign(this, {query_view, repo_url});
......
94 91
	if ("error_json" in response)
95 92
	    throw "Repository's response is not valid JSON :(";
96 93

  
97
	if (!response.json["$schema"])
98
	    throw "Results were served using a nonconforming response format.";
99
	if (!query_schema_url_regex.test(response.json["$schema"]))
100
	    throw "Results were served using unsupported Hydrilla API version. You might need to update Haketilo.";
94
	const $id =
95
	      `https://hydrilla.koszko.org/schemas/api_query_result-1.0.1.schema.json`;
96
	const schema = haketilo_schemas[$id];
97
	const result = haketilo_validator.validate(response.json, schema);
98
	if (result.errors.length > 0) {
99
	    console.error(result.errors);
100

  
101
	    const reg = new RegExp(schema.properties.$schema.pattern);
102
	    if (response.json.$schema && !reg.test(response.json.$schema))
103
		throw "Results were served using unsupported Hydrilla API version. You might need to update Haketilo.";
101 104

  
102
	/* TODO: here we should perform JSON schema validation! */
105
	    throw "Results were served using a nonconforming response format.";
106
	}
103 107

  
104 108
	return response.json.mappings;
105 109
    }

Also available in: Unified diff