Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(166)

Side by Side Diff: chrome/renderer/resources/extensions/json_schema.js

Issue 9584021: Improve implementation of isInstanceOf in JSONSchemaValidator. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Remove a test that cannot work anymore, update docs Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // ----------------------------------------------------------------------------- 5 // -----------------------------------------------------------------------------
6 // NOTE: If you change this file you need to touch renderer_resources.grd to 6 // NOTE: If you change this file you need to touch renderer_resources.grd to
7 // have your change take effect. 7 // have your change take effect.
8 // ----------------------------------------------------------------------------- 8 // -----------------------------------------------------------------------------
9 9
10 //============================================================================== 10 //==============================================================================
(...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after
273 this.validate(instance[prop], schema.properties[prop], propPath); 273 this.validate(instance[prop], schema.properties[prop], propPath);
274 } else if (!schema.properties[prop].optional) { 274 } else if (!schema.properties[prop].optional) {
275 this.addError(propPath, "propertyRequired"); 275 this.addError(propPath, "propertyRequired");
276 } 276 }
277 } 277 }
278 } 278 }
279 279
280 // If "instanceof" property is set, check that this object inherits from 280 // If "instanceof" property is set, check that this object inherits from
281 // the specified constructor (function). 281 // the specified constructor (function).
282 if (schema.isInstanceOf) { 282 if (schema.isInstanceOf) {
283 var isInstance = function() { 283 if (Object.prototype.toString.call(instance) !=
arv (Not doing code reviews) 2012/03/05 19:13:07 Maybe we should have walked the prototype chain so
Aaron Boodman 2012/03/05 21:06:08 I agree the recursive search is cooler, but in thi
284 var constructor = this[schema.isInstanceOf]; 284 "[object " + schema.isInstanceOf + "]") {
285 if (constructor) {
286 return (instance instanceof constructor);
287 }
288
289 // Special-case constructors that can not always be found on the global
290 // object, but for which we to allow validation.
291 var allowedNamedConstructors = {
292 "Window": true,
293 "ImageData": true
294 }
295 if (!allowedNamedConstructors[schema.isInstanceOf]) {
296 throw "Attempt to validate against an instance ctor that could not be" +
297 "found: " + schema.isInstanceOf;
298 }
299 return (schema.isInstanceOf == instance.constructor.name)
300 }();
301
302 if (!isInstance)
303 this.addError(propPath, "notInstance", [schema.isInstanceOf]); 285 this.addError(propPath, "notInstance", [schema.isInstanceOf]);
286 }
304 } 287 }
305 288
306 // Exit early from additional property check if "type":"any" is defined. 289 // Exit early from additional property check if "type":"any" is defined.
307 if (schema.additionalProperties && 290 if (schema.additionalProperties &&
308 schema.additionalProperties.type && 291 schema.additionalProperties.type &&
309 schema.additionalProperties.type == "any") { 292 schema.additionalProperties.type == "any") {
310 return; 293 return;
311 } 294 }
312 295
313 // By default, additional properties are not allowed on instance objects. This 296 // By default, additional properties are not allowed on instance objects. This
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
444 */ 427 */
445 chromeHidden.JSONSchemaValidator.prototype.addError = function( 428 chromeHidden.JSONSchemaValidator.prototype.addError = function(
446 path, key, replacements) { 429 path, key, replacements) {
447 this.errors.push({ 430 this.errors.push({
448 path: path, 431 path: path,
449 message: chromeHidden.JSONSchemaValidator.formatError(key, replacements) 432 message: chromeHidden.JSONSchemaValidator.formatError(key, replacements)
450 }); 433 });
451 }; 434 };
452 435
453 })(); 436 })();
OLDNEW
« no previous file with comments | « chrome/common/extensions/docs/extension.html ('k') | chrome/test/data/extensions/json_schema_test.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698