| 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 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 46 if (instance.constructor.name == className) | 46 if (instance.constructor.name == className) |
| 47 return true; | 47 return true; |
| 48 } | 48 } |
| 49 return false; | 49 return false; |
| 50 } | 50 } |
| 51 | 51 |
| 52 function isOptionalValue(value) { | 52 function isOptionalValue(value) { |
| 53 return typeof(value) === 'undefined' || value === null; | 53 return typeof(value) === 'undefined' || value === null; |
| 54 } | 54 } |
| 55 | 55 |
| 56 function enumToString(enumValue) { | |
| 57 return enumValue.name || enumValue; | |
| 58 } | |
| 59 | |
| 60 /** | 56 /** |
| 61 * Validates an instance against a schema and accumulates errors. Usage: | 57 * Validates an instance against a schema and accumulates errors. Usage: |
| 62 * | 58 * |
| 63 * var validator = new JSONSchemaValidator(); | 59 * var validator = new JSONSchemaValidator(); |
| 64 * validator.validate(inst, schema); | 60 * validator.validate(inst, schema); |
| 65 * if (validator.errors.length == 0) | 61 * if (validator.errors.length == 0) |
| 66 * console.log("Valid!"); | 62 * console.log("Valid!"); |
| 67 * else | 63 * else |
| 68 * console.log(validator.errors); | 64 * console.log(validator.errors); |
| 69 * | 65 * |
| (...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 314 this.addError(path, "invalidChoice"); | 310 this.addError(path, "invalidChoice"); |
| 315 }; | 311 }; |
| 316 | 312 |
| 317 /** | 313 /** |
| 318 * Validates an instance against a schema with an enum type. Populates the | 314 * Validates an instance against a schema with an enum type. Populates the |
| 319 * |errors| property, and returns a boolean indicating whether the instance | 315 * |errors| property, and returns a boolean indicating whether the instance |
| 320 * validates. | 316 * validates. |
| 321 */ | 317 */ |
| 322 JSONSchemaValidator.prototype.validateEnum = function(instance, schema, path) { | 318 JSONSchemaValidator.prototype.validateEnum = function(instance, schema, path) { |
| 323 for (var i = 0; i < schema.enum.length; i++) { | 319 for (var i = 0; i < schema.enum.length; i++) { |
| 324 if (instance === enumToString(schema.enum[i])) | 320 if (instance === schema.enum[i]) |
| 325 return true; | 321 return true; |
| 326 } | 322 } |
| 327 | 323 |
| 328 this.addError(path, "invalidEnum", | 324 this.addError(path, "invalidEnum", [schema.enum.join(", ")]); |
| 329 [schema.enum.map(enumToString).join(", ")]); | |
| 330 return false; | 325 return false; |
| 331 }; | 326 }; |
| 332 | 327 |
| 333 /** | 328 /** |
| 334 * Validates an instance against an object schema and populates the errors | 329 * Validates an instance against an object schema and populates the errors |
| 335 * property. | 330 * property. |
| 336 */ | 331 */ |
| 337 JSONSchemaValidator.prototype.validateObject = | 332 JSONSchemaValidator.prototype.validateObject = |
| 338 function(instance, schema, path) { | 333 function(instance, schema, path) { |
| 339 if (schema.properties) { | 334 if (schema.properties) { |
| (...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 512 }; | 507 }; |
| 513 | 508 |
| 514 /** | 509 /** |
| 515 * Resets errors to an empty list so you can call 'validate' again. | 510 * Resets errors to an empty list so you can call 'validate' again. |
| 516 */ | 511 */ |
| 517 JSONSchemaValidator.prototype.resetErrors = function() { | 512 JSONSchemaValidator.prototype.resetErrors = function() { |
| 518 this.errors = []; | 513 this.errors = []; |
| 519 }; | 514 }; |
| 520 | 515 |
| 521 exports.JSONSchemaValidator = JSONSchemaValidator; | 516 exports.JSONSchemaValidator = JSONSchemaValidator; |
| OLD | NEW |