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