Bug 1530465 - Add support for JSON type in schema validator. r=Felipe
authorMichael Kaply <mozilla@kaply.com>
Tue, 26 Feb 2019 15:12:44 +0000
changeset 519046 43848f381d6cbe6a5835583b97b0d18ea4a0bedd
parent 519045 c68cc2526fbde767f1277da772664a485c45c4e9
child 519047 4f37d111e4a97456129ebb9b61ef074682e2d58f
push id10862
push userffxbld-merge
push dateMon, 11 Mar 2019 13:01:11 +0000
treeherdermozilla-beta@a2e7f5c935da [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersFelipe
bugs1530465
milestone67.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1530465 - Add support for JSON type in schema validator. r=Felipe Differential Revision: https://phabricator.services.mozilla.com/D21048
toolkit/components/utils/JsonSchemaValidator.jsm
toolkit/components/utils/test/browser/browser_JsonSchemaValidator.js
--- a/toolkit/components/utils/JsonSchemaValidator.jsm
+++ b/toolkit/components/utils/JsonSchemaValidator.jsm
@@ -151,16 +151,32 @@ function validateAndParseParamRecursive(
           if (!valid) {
             return [false, null];
           }
           parsedObj[item] = parsedValue;
         }
       }
       return [true, parsedObj];
     }
+
+    case "JSON":
+      if (typeof(param) == "object") {
+        return [true, param];
+      }
+      try {
+        let json = JSON.parse(param);
+        if (typeof(json) != "object") {
+          log.error("JSON was not an object");
+          return [false, null];
+        }
+        return [true, json];
+      } catch (e) {
+        log.error("JSON string couldn't be parsed");
+        return [false, null];
+      }
   }
 
   return [false, null];
 }
 
 function validateAndParseSimpleParam(param, type) {
   let valid = false;
   let parsedParam = param;
--- a/toolkit/components/utils/test/browser/browser_JsonSchemaValidator.js
+++ b/toolkit/components/utils/test/browser/browser_JsonSchemaValidator.js
@@ -442,8 +442,49 @@ add_task(async function test_patternProp
       "[": {" type": "string" },
     },
   };
 
   Assert.throws(() => {
     [valid, parsed] = JsonSchemaValidator.validateAndParseParameters({}, schema);
   }, /Invalid property pattern/, "Checking that invalid property patterns throw");
 });
+
+add_task(async function test_JSON_type() {
+  let schema = {
+    type: "JSON",
+  };
+
+  let valid, parsed;
+  [valid, parsed] = JsonSchemaValidator.validateAndParseParameters({
+    "a": "b",
+  }, schema);
+
+  ok(valid, "Object is valid");
+  ok(typeof(parsed) == "object", "parsed in an object");
+  is(parsed.a, "b", "parsedProperty is correct");
+
+  [valid, parsed] = JsonSchemaValidator.validateAndParseParameters(
+    "{\"a\": \"b\"}"
+  , schema);
+
+  ok(valid, "Object is valid");
+  ok(typeof(parsed) == "object", "parsed in an object");
+  is(parsed.a, "b", "parsedProperty is correct");
+
+  [valid, parsed] = JsonSchemaValidator.validateAndParseParameters(
+    "{This{is{not{JSON}}}}"
+  , schema);
+
+  ok(!valid, "Object is not valid since JSON was incorrect");
+
+  [valid, parsed] = JsonSchemaValidator.validateAndParseParameters(
+    "0"
+  , schema);
+
+  ok(!valid, "Object is not valid since input wasn't an object");
+
+  [valid, parsed] = JsonSchemaValidator.validateAndParseParameters(
+    "true"
+  , schema);
+
+  ok(!valid, "Object is not valid since input wasn't an object");
+});