Commit abbbb096 authored by aa@chromium.org's avatar aa@chromium.org

Improve implementation of isInstanceOf in JSONSchemaValidator.

This does not update the docs because they have gotten badly
out of date. I will update them separately.

BUG=116490


Review URL: http://codereview.chromium.org/9584021

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@124835 0039d316-1c4b-4281-b951-d872f2087c98
parent 09ce892f
...@@ -148,7 +148,7 @@ ...@@ -148,7 +148,7 @@
"returns": { "returns": {
"type": "array", "type": "array",
"description": "Array of global objects", "description": "Array of global objects",
"items": { "type": "object", "isInstanceOf": "Window", "additionalProperties": { "type": "any" } } "items": { "type": "object", "isInstanceOf": "global", "additionalProperties": { "type": "any" } }
} }
}, },
{ {
...@@ -157,7 +157,7 @@ ...@@ -157,7 +157,7 @@
"description": "Returns the JavaScript 'window' object for the background page running inside the current extension. Returns null if the extension has no background page.", "description": "Returns the JavaScript 'window' object for the background page running inside the current extension. Returns null if the extension has no background page.",
"parameters": [], "parameters": [],
"returns": { "returns": {
"type": "object", "isInstanceOf": "Window", "additionalProperties": { "type": "any" } "type": "object", "isInstanceOf": "global", "additionalProperties": { "type": "any" }
} }
}, },
{ {
...@@ -172,7 +172,7 @@ ...@@ -172,7 +172,7 @@
"returns": { "returns": {
"type": "array", "type": "array",
"description": "Array of global window objects", "description": "Array of global window objects",
"items": { "type": "object", "isInstanceOf": "Window", "additionalProperties": { "type": "any" } } "items": { "type": "object", "isInstanceOf": "global", "additionalProperties": { "type": "any" } }
} }
}, },
{ {
......
...@@ -542,7 +542,7 @@ For details, see ...@@ -542,7 +542,7 @@ For details, see
</div><div class="apiItem"> </div><div class="apiItem">
<a name="method-getBackgroundPage"></a> <!-- method-anchor --> <a name="method-getBackgroundPage"></a> <!-- method-anchor -->
<h4>getBackgroundPage</h4> <h4>getBackgroundPage</h4>
<div class="summary"><span>Window</span> <div class="summary"><span>global</span>
<!-- Note: intentionally longer 80 columns --> <!-- Note: intentionally longer 80 columns -->
<span>chrome.extension.getBackgroundPage</span>()</div> <span>chrome.extension.getBackgroundPage</span>()</div>
<div class="description"> <div class="description">
...@@ -562,7 +562,7 @@ For details, see ...@@ -562,7 +562,7 @@ For details, see
( (
<span id="typeTemplate"> <span id="typeTemplate">
<span> <span>
<span>Window</span> <span>global</span>
</span> </span>
</span> </span>
) )
...@@ -652,7 +652,7 @@ For details, see ...@@ -652,7 +652,7 @@ For details, see
</div><div class="apiItem"> </div><div class="apiItem">
<a name="method-getViews"></a> <!-- method-anchor --> <a name="method-getViews"></a> <!-- method-anchor -->
<h4>getViews</h4> <h4>getViews</h4>
<div class="summary"><span>array of Window</span> <div class="summary"><span>array of global</span>
<!-- Note: intentionally longer 80 columns --> <!-- Note: intentionally longer 80 columns -->
<span>chrome.extension.getViews</span>(<span class="optional"><span>object</span> <span>chrome.extension.getViews</span>(<span class="optional"><span>object</span>
<var><span>fetchProperties</span></var></span>)</div> <var><span>fetchProperties</span></var></span>)</div>
...@@ -759,7 +759,7 @@ For details, see ...@@ -759,7 +759,7 @@ For details, see
<span> <span>
array of <span><span> array of <span><span>
<span> <span>
<span>Window</span> <span>global</span>
</span> </span>
</span></span> </span></span>
</span> </span>
......
...@@ -280,28 +280,11 @@ chromeHidden.JSONSchemaValidator.prototype.validateObject = function( ...@@ -280,28 +280,11 @@ chromeHidden.JSONSchemaValidator.prototype.validateObject = function(
// If "instanceof" property is set, check that this object inherits from // If "instanceof" property is set, check that this object inherits from
// the specified constructor (function). // the specified constructor (function).
if (schema.isInstanceOf) { if (schema.isInstanceOf) {
var isInstance = function() { if (Object.prototype.toString.call(instance) !=
var constructor = this[schema.isInstanceOf]; "[object " + schema.isInstanceOf + "]") {
if (constructor) {
return (instance instanceof constructor);
}
// Special-case constructors that can not always be found on the global
// object, but for which we to allow validation.
var allowedNamedConstructors = {
"Window": true,
"ImageData": true
}
if (!allowedNamedConstructors[schema.isInstanceOf]) {
throw "Attempt to validate against an instance ctor that could not be" +
"found: " + schema.isInstanceOf;
}
return (schema.isInstanceOf == instance.constructor.name)
}();
if (!isInstance)
this.addError(propPath, "notInstance", [schema.isInstanceOf]); this.addError(propPath, "notInstance", [schema.isInstanceOf]);
} }
}
// Exit early from additional property check if "type":"any" is defined. // Exit early from additional property check if "type":"any" is defined.
if (schema.additionalProperties && if (schema.additionalProperties &&
......
// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
...@@ -167,16 +167,6 @@ function testExtends() { ...@@ -167,16 +167,6 @@ function testExtends() {
assertValid("", 43, schema); assertValid("", 43, schema);
} }
function ClassA() {
this.a = "a";
}
function ClassB() {
}
ClassB.prototype = new ClassA();
function ClassC() {
this.a = "a";
}
function testObject() { function testObject() {
var schema = { var schema = {
properties: { properties: {
...@@ -214,28 +204,6 @@ function testObject() { ...@@ -214,28 +204,6 @@ function testObject() {
assertValid("Object", {foo:"foo", bar:undefined}, schema); assertValid("Object", {foo:"foo", bar:undefined}, schema);
assertNotValid("Object", {foo:"foo", bar:"42"}, schema, assertNotValid("Object", {foo:"foo", bar:"42"}, schema,
[formatError("invalidType", ["integer", "string"])]); [formatError("invalidType", ["integer", "string"])]);
var classASchema = {
properties: {
"a": { type: "string" }
},
isInstanceOf: "ClassA"
};
var classBSchema = {
properties: {},
isInstanceOf: "ClassB"
};
var a = new ClassA();
var b = new ClassB();
var c = new ClassC();
assertValid("Object", a, classASchema);
assertValid("Object", b, classBSchema);
assertValid("Object", b, classASchema);
assertNotValid("Object", c, classASchema,
[formatError("notInstance", [classASchema.isInstanceOf])]);
} }
function testTypeReference() { function testTypeReference() {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment