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

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

Issue 9317072: Allow omitting optional parameters for Extensions API functions (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: null and undefined arguments now handled. Comments added and variables renamed. 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
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 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
137 if (typeOrTypeList instanceof Array) { 137 if (typeOrTypeList instanceof Array) {
138 for (var i = 0; i < typeOrTypeList.length; i++) { 138 for (var i = 0; i < typeOrTypeList.length; i++) {
139 addType(this, typeOrTypeList[i]); 139 addType(this, typeOrTypeList[i]);
140 } 140 }
141 } else { 141 } else {
142 addType(this, typeOrTypeList); 142 addType(this, typeOrTypeList);
143 } 143 }
144 } 144 }
145 145
146 /** 146 /**
147 * Returns a list of strings of the types that this schema accepts.
148 */
149 chromeHidden.JSONSchemaValidator.prototype.getAllTypesForSchema = function(
Aaron Boodman 2012/03/01 22:59:05 Nit: I usually see the "function(" on the next lin
150 schema) {
151 var schemaTypes = [];
152 if (schema.type)
153 schemaTypes.push(schema.type);
154 if (schema.choices) {
155 for (var i = 0; i < schema.choices.length; i++) {
156 var choiceTypes = this.getAllTypesForSchema(schema.choices[i]);
157 schemaTypes = schemaTypes.concat(choiceTypes);
158 }
159 }
160 if (schema['$ref']) {
161 var refTypes = this.getAllTypesForSchema(this.types[schema['$ref']]);
162 schemaTypes = schemaTypes.concat(refTypes);
163 }
164 return schemaTypes;
165 };
166
167 /**
168 * Returns true if |schema| would accept an argument of type |type|.
169 */
170 chromeHidden.JSONSchemaValidator.prototype.isValidSchemaType = function(
171 type, schema) {
172 if (schema.optional && (type == "null" || type == "undefined"))
173 return true;
174
175 schemaTypes = this.getAllTypesForSchema(schema);
176 for (var i = 0; i < schemaTypes.length; i++) {
177 if (schemaTypes[i] == "any" || type == schemaTypes[i])
178 return true;
179 }
180 return type == "any";
181 };
182
183 /**
184 * Returns true if there is a non-null argument that both |schema1| and
185 * |schema2| would accept.
186 */
187 chromeHidden.JSONSchemaValidator.prototype.checkSchemaOverlap = function(
188 schema1, schema2) {
189 var schema1Types = this.getAllTypesForSchema(schema1);
190 for (var i = 0; i < schema1Types.length; i++) {
191 if (this.isValidSchemaType(schema1Types[i], schema2))
192 return true;
193 }
194 return false;
195 };
196
197 /**
147 * Validates an instance against a schema. The instance can be any JavaScript 198 * Validates an instance against a schema. The instance can be any JavaScript
148 * value and will be validated recursively. When this method returns, the 199 * value and will be validated recursively. When this method returns, the
149 * |errors| property will contain a list of errors, if any. 200 * |errors| property will contain a list of errors, if any.
150 */ 201 */
151 chromeHidden.JSONSchemaValidator.prototype.validate = function( 202 chromeHidden.JSONSchemaValidator.prototype.validate = function(
152 instance, schema, opt_path) { 203 instance, schema, opt_path) {
153 var path = opt_path || ""; 204 var path = opt_path || "";
154 205
155 if (!schema) { 206 if (!schema) {
156 this.addError(path, "schemaRequired"); 207 this.addError(path, "schemaRequired");
(...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after
443 * message. 494 * message.
444 */ 495 */
445 chromeHidden.JSONSchemaValidator.prototype.addError = function( 496 chromeHidden.JSONSchemaValidator.prototype.addError = function(
446 path, key, replacements) { 497 path, key, replacements) {
447 this.errors.push({ 498 this.errors.push({
448 path: path, 499 path: path,
449 message: chromeHidden.JSONSchemaValidator.formatError(key, replacements) 500 message: chromeHidden.JSONSchemaValidator.formatError(key, replacements)
450 }); 501 });
451 }; 502 };
452 503
504 /**
505 * Resets errors to an empty list so you can call 'validate' again.
506 */
507 chromeHidden.JSONSchemaValidator.prototype.resetErrors = function() {
508 this.errors = [];
509 };
510
453 })(); 511 })();
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698