Revision 57ce414c
Added by koszko over 1 year ago
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
validate repository responses against JSON schemas