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

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

Issue 2953073004: [Extensions Bindings] Move loadTypeSchema from utils to json_schema (Closed)
Patch Set: remove unnecessary require Created 3 years, 5 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
« no previous file with comments | « extensions/renderer/resources/binding.js ('k') | extensions/renderer/resources/utils.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 6 // NOTE: If you change this file you need to touch
7 // extension_renderer_resources.grd to have your change take effect. 7 // extension_renderer_resources.grd to have your change take effect.
8 // ----------------------------------------------------------------------------- 8 // -----------------------------------------------------------------------------
9 9
10 //============================================================================== 10 //==============================================================================
(...skipping 22 matching lines...) Expand all
33 // - null counts as 'unspecified' for optional values 33 // - null counts as 'unspecified' for optional values
34 // - added the 'choices' property, to allow specifying a list of possible types 34 // - added the 'choices' property, to allow specifying a list of possible types
35 // for a value 35 // for a value
36 // - by default an "object" typed schema does not allow additional properties. 36 // - by default an "object" typed schema does not allow additional properties.
37 // if present, "additionalProperties" is to be a schema against which all 37 // if present, "additionalProperties" is to be a schema against which all
38 // additional properties will be validated. 38 // additional properties will be validated.
39 //============================================================================== 39 //==============================================================================
40 40
41 var utils = require('utils'); 41 var utils = require('utils');
42 var loggingNative = requireNative('logging'); 42 var loggingNative = requireNative('logging');
43 var getObjectType = requireNative('schema_registry').GetObjectType; 43 var schemaRegistry = requireNative('schema_registry');
44 var CHECK = loggingNative.CHECK; 44 var CHECK = loggingNative.CHECK;
45 var DCHECK = loggingNative.DCHECK; 45 var DCHECK = loggingNative.DCHECK;
46 var WARNING = loggingNative.WARNING;
47
48 function loadTypeSchema(typeName, defaultSchema) {
49 var parts = $String.split(typeName, '.');
50 if (parts.length == 1) {
51 if (defaultSchema == null) {
52 WARNING('Trying to reference "' + typeName + '" ' +
53 'with neither namespace nor default schema.');
54 return null;
55 }
56 var types = defaultSchema.types;
57 } else {
58 var schemaName = $Array.join($Array.slice(parts, 0, parts.length - 1), '.');
59 var types = schemaRegistry.GetSchema(schemaName).types;
60 }
61 for (var i = 0; i < types.length; ++i) {
62 if (types[i].id == typeName)
63 return types[i];
64 }
65 return null;
66 }
46 67
47 function isInstanceOfClass(instance, className) { 68 function isInstanceOfClass(instance, className) {
48 while ((instance = instance.__proto__)) { 69 while ((instance = instance.__proto__)) {
49 if (instance.constructor.name == className) 70 if (instance.constructor.name == className)
50 return true; 71 return true;
51 } 72 }
52 return false; 73 return false;
53 } 74 }
54 75
55 function isOptionalValue(value) { 76 function isOptionalValue(value) {
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
132 * Classifies a value as one of the JSON schema primitive types. Note that we 153 * Classifies a value as one of the JSON schema primitive types. Note that we
133 * don't explicitly disallow 'function', because we want to allow functions in 154 * don't explicitly disallow 'function', because we want to allow functions in
134 * the input values. 155 * the input values.
135 */ 156 */
136 utils.defineProperty(JSONSchemaValidator, 'getType', function(value) { 157 utils.defineProperty(JSONSchemaValidator, 'getType', function(value) {
137 // If we can determine the type safely in JS, it's fastest to do it here. 158 // If we can determine the type safely in JS, it's fastest to do it here.
138 // However, Object types are difficult to classify, so we have to do it in 159 // However, Object types are difficult to classify, so we have to do it in
139 // C++. 160 // C++.
140 var s = typeof value; 161 var s = typeof value;
141 if (s === 'object') 162 if (s === 'object')
142 return value === null ? 'null' : getObjectType(value); 163 return value === null ? 'null' : schemaRegistry.GetObjectType(value);
143 if (s === 'number') 164 if (s === 'number')
144 return value % 1 === 0 ? 'integer' : 'number'; 165 return value % 1 === 0 ? 'integer' : 'number';
145 return s; 166 return s;
146 }); 167 });
147 168
148 /** 169 /**
149 * Add types that may be referenced by validated schemas that reference them 170 * Add types that may be referenced by validated schemas that reference them
150 * with "$ref": <typeId>. Each type must be a valid schema and define an 171 * with "$ref": <typeId>. Each type must be a valid schema and define an
151 * "id" property. 172 * "id" property.
152 */ 173 */
(...skipping 30 matching lines...) Expand all
183 if (ref) { 204 if (ref) {
184 var type = this.getOrAddType(ref); 205 var type = this.getOrAddType(ref);
185 CHECK(type, 'Could not find type ' + ref); 206 CHECK(type, 'Could not find type ' + ref);
186 schemaTypes = $Array.concat(schemaTypes, this.getAllTypesForSchema(type)); 207 schemaTypes = $Array.concat(schemaTypes, this.getAllTypesForSchema(type));
187 } 208 }
188 return schemaTypes; 209 return schemaTypes;
189 }; 210 };
190 211
191 JSONSchemaValidator.prototype.getOrAddType = function(typeName) { 212 JSONSchemaValidator.prototype.getOrAddType = function(typeName) {
192 if (!this.types[typeName]) 213 if (!this.types[typeName])
193 this.types[typeName] = utils.loadTypeSchema(typeName); 214 this.types[typeName] = loadTypeSchema(typeName);
194 return this.types[typeName]; 215 return this.types[typeName];
195 }; 216 };
196 217
197 /** 218 /**
198 * Returns true if |schema| would accept an argument of type |type|. 219 * Returns true if |schema| would accept an argument of type |type|.
199 */ 220 */
200 JSONSchemaValidator.prototype.isValidSchemaType = function(type, schema) { 221 JSONSchemaValidator.prototype.isValidSchemaType = function(type, schema) {
201 if (type == 'any') 222 if (type == 'any')
202 return true; 223 return true;
203 224
(...skipping 314 matching lines...) Expand 10 before | Expand all | Expand 10 after
518 }; 539 };
519 540
520 /** 541 /**
521 * Resets errors to an empty list so you can call 'validate' again. 542 * Resets errors to an empty list so you can call 'validate' again.
522 */ 543 */
523 JSONSchemaValidator.prototype.resetErrors = function() { 544 JSONSchemaValidator.prototype.resetErrors = function() {
524 this.errors = []; 545 this.errors = [];
525 }; 546 };
526 547
527 exports.$set('JSONSchemaValidator', JSONSchemaValidator); 548 exports.$set('JSONSchemaValidator', JSONSchemaValidator);
549 exports.$set('loadTypeSchema', loadTypeSchema);
OLDNEW
« no previous file with comments | « extensions/renderer/resources/binding.js ('k') | extensions/renderer/resources/utils.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698