OLD | NEW |
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 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
160 addType(this, typeOrTypeList); | 160 addType(this, typeOrTypeList); |
161 } | 161 } |
162 } | 162 } |
163 | 163 |
164 /** | 164 /** |
165 * Returns a list of strings of the types that this schema accepts. | 165 * Returns a list of strings of the types that this schema accepts. |
166 */ | 166 */ |
167 JSONSchemaValidator.prototype.getAllTypesForSchema = function(schema) { | 167 JSONSchemaValidator.prototype.getAllTypesForSchema = function(schema) { |
168 var schemaTypes = []; | 168 var schemaTypes = []; |
169 if (schema.type) | 169 if (schema.type) |
170 schemaTypes.push(schema.type); | 170 $Array.push(schemaTypes, schema.type); |
171 if (schema.choices) { | 171 if (schema.choices) { |
172 for (var i = 0; i < schema.choices.length; i++) { | 172 for (var i = 0; i < schema.choices.length; i++) { |
173 var choiceTypes = this.getAllTypesForSchema(schema.choices[i]); | 173 var choiceTypes = this.getAllTypesForSchema(schema.choices[i]); |
174 schemaTypes = schemaTypes.concat(choiceTypes); | 174 schemaTypes = $Array.concat(schemaTypes, choiceTypes); |
175 } | 175 } |
176 } | 176 } |
177 var ref = schema['$ref']; | 177 var ref = schema['$ref']; |
178 if (ref) { | 178 if (ref) { |
179 var type = this.getOrAddType(ref); | 179 var type = this.getOrAddType(ref); |
180 CHECK(type, 'Could not find type ' + ref); | 180 CHECK(type, 'Could not find type ' + ref); |
181 schemaTypes = schemaTypes.concat(this.getAllTypesForSchema(type)); | 181 schemaTypes = $Array.concat(schemaTypes, this.getAllTypesForSchema(type)); |
182 } | 182 } |
183 return schemaTypes; | 183 return schemaTypes; |
184 }; | 184 }; |
185 | 185 |
186 JSONSchemaValidator.prototype.getOrAddType = function(typeName) { | 186 JSONSchemaValidator.prototype.getOrAddType = function(typeName) { |
187 if (!this.types[typeName]) | 187 if (!this.types[typeName]) |
188 this.types[typeName] = loadTypeSchema(typeName); | 188 this.types[typeName] = loadTypeSchema(typeName); |
189 return this.types[typeName]; | 189 return this.types[typeName]; |
190 }; | 190 }; |
191 | 191 |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
334 JSONSchemaValidator.prototype.validateObject = | 334 JSONSchemaValidator.prototype.validateObject = |
335 function(instance, schema, path) { | 335 function(instance, schema, path) { |
336 if (schema.properties) { | 336 if (schema.properties) { |
337 for (var prop in schema.properties) { | 337 for (var prop in schema.properties) { |
338 // It is common in JavaScript to add properties to Object.prototype. This | 338 // It is common in JavaScript to add properties to Object.prototype. This |
339 // check prevents such additions from being interpreted as required | 339 // check prevents such additions from being interpreted as required |
340 // schema properties. | 340 // schema properties. |
341 // TODO(aa): If it ever turns out that we actually want this to work, | 341 // TODO(aa): If it ever turns out that we actually want this to work, |
342 // there are other checks we could put here, like requiring that schema | 342 // there are other checks we could put here, like requiring that schema |
343 // properties be objects that have a 'type' property. | 343 // properties be objects that have a 'type' property. |
344 if (!schema.properties.hasOwnProperty(prop)) | 344 if (!$Object.hasOwnProperty(schema.properties, prop)) |
345 continue; | 345 continue; |
346 | 346 |
347 var propPath = path ? path + "." + prop : prop; | 347 var propPath = path ? path + "." + prop : prop; |
348 if (schema.properties[prop] == undefined) { | 348 if (schema.properties[prop] == undefined) { |
349 this.addError(propPath, "invalidPropertyType"); | 349 this.addError(propPath, "invalidPropertyType"); |
350 } else if (prop in instance && !isOptionalValue(instance[prop])) { | 350 } else if (prop in instance && !isOptionalValue(instance[prop])) { |
351 this.validate(instance[prop], schema.properties[prop], propPath); | 351 this.validate(instance[prop], schema.properties[prop], propPath); |
352 } else if (!schema.properties[prop].optional) { | 352 } else if (!schema.properties[prop].optional) { |
353 this.addError(propPath, "propertyRequired"); | 353 this.addError(propPath, "propertyRequired"); |
354 } | 354 } |
(...skipping 15 matching lines...) Expand all Loading... |
370 } | 370 } |
371 | 371 |
372 // By default, additional properties are not allowed on instance objects. This | 372 // By default, additional properties are not allowed on instance objects. This |
373 // can be overridden by setting the additionalProperties property to a schema | 373 // can be overridden by setting the additionalProperties property to a schema |
374 // which any additional properties must validate against. | 374 // which any additional properties must validate against. |
375 for (var prop in instance) { | 375 for (var prop in instance) { |
376 if (schema.properties && prop in schema.properties) | 376 if (schema.properties && prop in schema.properties) |
377 continue; | 377 continue; |
378 | 378 |
379 // Any properties inherited through the prototype are ignored. | 379 // Any properties inherited through the prototype are ignored. |
380 if (!instance.hasOwnProperty(prop)) | 380 if (!$Object.hasOwnProperty(instance, prop)) |
381 continue; | 381 continue; |
382 | 382 |
383 var propPath = path ? path + "." + prop : prop; | 383 var propPath = path ? path + "." + prop : prop; |
384 if (schema.additionalProperties) | 384 if (schema.additionalProperties) |
385 this.validate(instance[prop], schema.additionalProperties, propPath); | 385 this.validate(instance[prop], schema.additionalProperties, propPath); |
386 else | 386 else |
387 this.addError(propPath, "unexpectedProperty"); | 387 this.addError(propPath, "unexpectedProperty"); |
388 } | 388 } |
389 }; | 389 }; |
390 | 390 |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
495 return false; | 495 return false; |
496 } | 496 } |
497 }; | 497 }; |
498 | 498 |
499 /** | 499 /** |
500 * Adds an error message. |key| is an index into the |messages| object. | 500 * Adds an error message. |key| is an index into the |messages| object. |
501 * |replacements| is an array of values to replace '*' characters in the | 501 * |replacements| is an array of values to replace '*' characters in the |
502 * message. | 502 * message. |
503 */ | 503 */ |
504 JSONSchemaValidator.prototype.addError = function(path, key, replacements) { | 504 JSONSchemaValidator.prototype.addError = function(path, key, replacements) { |
505 this.errors.push({ | 505 $Array.push(this.errors, { |
506 path: path, | 506 path: path, |
507 message: JSONSchemaValidator.formatError(key, replacements) | 507 message: JSONSchemaValidator.formatError(key, replacements) |
508 }); | 508 }); |
509 }; | 509 }; |
510 | 510 |
511 /** | 511 /** |
512 * Resets errors to an empty list so you can call 'validate' again. | 512 * Resets errors to an empty list so you can call 'validate' again. |
513 */ | 513 */ |
514 JSONSchemaValidator.prototype.resetErrors = function() { | 514 JSONSchemaValidator.prototype.resetErrors = function() { |
515 this.errors = []; | 515 this.errors = []; |
516 }; | 516 }; |
517 | 517 |
518 exports.JSONSchemaValidator = JSONSchemaValidator; | 518 exports.JSONSchemaValidator = JSONSchemaValidator; |
OLD | NEW |