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