1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
5 import { JsonSchema } from "resource://gre/modules/JsonSchema.sys.mjs";
10 * Validate JSON result from the shopping API.
12 * @param {object} json
13 * JSON object from the API request.
14 * @param {string} SchemaURL
15 * URL string for the schema to validate with.
16 * @param {boolean} logErrors
17 * Should invalid JSON log out the errors.
18 * @returns {boolean} result
19 * If the JSON is valid or not.
21 async function validate(json, SchemaURL, logErrors) {
22 if (!schemas[SchemaURL]) {
23 schemas[SchemaURL] = await fetch(SchemaURL).then(rsp => rsp.json());
26 let result = JsonSchema.validate(json, schemas[SchemaURL]);
27 let { errors } = result;
28 if (!result.valid && logErrors) {
29 console.error(`Invalid result: ${JSON.stringify(errors, undefined, 2)}`);
34 export const ProductValidator = {