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 @@
"returns": {
"type": "array",
"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 @@
"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": [],
"returns": {
"type": "object", "isInstanceOf": "Window", "additionalProperties": { "type": "any" }
"type": "object", "isInstanceOf": "global", "additionalProperties": { "type": "any" }
}
},
{
......@@ -172,7 +172,7 @@
"returns": {
"type": "array",
"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
</div><div class="apiItem">
<a name="method-getBackgroundPage"></a> <!-- method-anchor -->
<h4>getBackgroundPage</h4>
<div class="summary"><span>Window</span>
<div class="summary"><span>global</span>
<!-- Note: intentionally longer 80 columns -->
<span>chrome.extension.getBackgroundPage</span>()</div>
<div class="description">
......@@ -562,7 +562,7 @@ For details, see
(
<span id="typeTemplate">
<span>
<span>Window</span>
<span>global</span>
</span>
</span>
)
......@@ -652,7 +652,7 @@ For details, see
</div><div class="apiItem">
<a name="method-getViews"></a> <!-- method-anchor -->
<h4>getViews</h4>
<div class="summary"><span>array of Window</span>
<div class="summary"><span>array of global</span>
<!-- Note: intentionally longer 80 columns -->
<span>chrome.extension.getViews</span>(<span class="optional"><span>object</span>
<var><span>fetchProperties</span></var></span>)</div>
......@@ -759,7 +759,7 @@ For details, see
<span>
array of <span><span>
<span>
<span>Window</span>
<span>global</span>
</span>
</span></span>
</span>
......
......@@ -280,27 +280,10 @@ chromeHidden.JSONSchemaValidator.prototype.validateObject = function(
// If "instanceof" property is set, check that this object inherits from
// the specified constructor (function).
if (schema.isInstanceOf) {
var isInstance = function() {
var constructor = this[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)
if (Object.prototype.toString.call(instance) !=
"[object " + schema.isInstanceOf + "]") {
this.addError(propPath, "notInstance", [schema.isInstanceOf]);
}
}
// Exit early from additional property check if "type":"any" is defined.
......
// 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
// found in the LICENSE file.
......@@ -167,16 +167,6 @@ function testExtends() {
assertValid("", 43, schema);
}
function ClassA() {
this.a = "a";
}
function ClassB() {
}
ClassB.prototype = new ClassA();
function ClassC() {
this.a = "a";
}
function testObject() {
var schema = {
properties: {
......@@ -214,28 +204,6 @@ function testObject() {
assertValid("Object", {foo:"foo", bar:undefined}, schema);
assertNotValid("Object", {foo:"foo", bar:"42"}, schema,
[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() {
......
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