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/install.js
45 45
#IMPORT html/dialog.js
46 46
#IMPORT html/item_preview.js AS ip
47 47

  
48
#FROM common/browser.js   IMPORT browser
49
#FROM html/DOM_helpers.js IMPORT clone_template, Showable
50
#FROM common/entities.js  IMPORT item_id_string, version_string, get_files
51
#FROM common/misc.js      IMPORT sha256_async AS compute_sha256
48
#FROM common/browser.js    IMPORT browser
49
#FROM html/DOM_helpers.js  IMPORT clone_template, Showable
50
#FROM common/entities.js   IMPORT item_id_string, version_string, get_files
51
#FROM common/misc.js       IMPORT sha256_async AS compute_sha256
52
#FROM common/jsonschema.js IMPORT haketilo_validator, haketilo_schemas
52 53

  
53 54
const coll = new Intl.Collator();
54 55

  
......
113 114
	    new Promise((...cbs) => [work.resolve_cb, work.reject_cb] = cbs)];
114 115
}
115 116

  
116
function _make_url_reg(item_type) {
117
    return new RegExp(
118
	`^https://hydrilla\\.koszko\\.org/schemas/api_${item_type}_description-1\\.([1-9][0-9]*\\.)*schema\\.json$`
119
    );
120
}
121

  
122
const _regexes = {};
123

  
124
function item_schema_url_regex(item_type) {
125
    _regexes[item_type] = _regexes[item_type] || _make_url_reg(item_type);
126
    return _regexes[item_type];
127
}
128

  
129 117
function InstallView(tab_id, on_view_show, on_view_hide) {
130 118
    Showable.call(this, on_view_show, on_view_hide);
131 119

  
......
204 192
	}
205 193

  
206 194
	const captype = item_type[0].toUpperCase() + item_type.substring(1);
207
	const reg = item_schema_url_regex(item_type);
208 195

  
209
	if (!response.json["$schema"]) {
196
	const $id =
197
	      `https://hydrilla.koszko.org/schemas/api_${item_type}_description-1.0.1.schema.json`;
198
	const schema = haketilo_schemas[$id];
199
	const result = haketilo_validator.validate(response.json, schema);
200
	if (result.errors.length > 0) {
201
	    const reg = new RegExp(schema.allOf[2].properties.$schema.pattern);
202
	    if (response.json.$schema && !reg.test(response.json.$schema)) {
203
		const msg = `${captype} ${item_id_string(id, ver)} was served using unsupported Hydrilla API version. You might need to update Haketilo.`;
204
		return work.err(result.errors, msg);
205
	    }
206

  
210 207
	    const msg = `${captype} ${item_id_string(id, ver)} was served using a nonconforming response format.`;
211
	    return work.err(null, msg);
212
	} else if (!reg.test(response.json["$schema"])) {
213
	    const msg = `${captype} ${item_id_string(id, ver)} was served using unsupported Hydrilla API version. You might need to update Haketilo.`;
214
	    return work.err(null, msg);
208
	    return work.err(result.errors, msg);
215 209
	}
216 210

  
217
	/* TODO: JSON schema validation should be added here. */
218

  
219 211
	const scripts = item_type === "resource" && response.json.scripts;
220 212
	const files = response.json.source_copyright.concat(scripts || []);
221 213

  

Also available in: Unified diff