OLD | NEW |
---|---|
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
64 arrayMinItems: "Array must have at least * items.", | 64 arrayMinItems: "Array must have at least * items.", |
65 arrayMaxItems: "Array must not have more than * items.", | 65 arrayMaxItems: "Array must not have more than * items.", |
66 itemRequired: "Item is required.", | 66 itemRequired: "Item is required.", |
67 stringMinLength: "String must be at least * characters long.", | 67 stringMinLength: "String must be at least * characters long.", |
68 stringMaxLength: "String must not be more than * characters long.", | 68 stringMaxLength: "String must not be more than * characters long.", |
69 stringPattern: "String must match the pattern: *.", | 69 stringPattern: "String must match the pattern: *.", |
70 numberMinValue: "Value must not be less than *.", | 70 numberMinValue: "Value must not be less than *.", |
71 numberMaxValue: "Value must not be greater than *.", | 71 numberMaxValue: "Value must not be greater than *.", |
72 numberMaxDecimal: "Value must not have more than * decimal places.", | 72 numberMaxDecimal: "Value must not have more than * decimal places.", |
73 invalidType: "Expected '*' but got '*'.", | 73 invalidType: "Expected '*' but got '*'.", |
74 invalidChoice: "Value does not match any valid type choices." | 74 invalidChoice: "Value does not match any valid type choices.", |
75 invalidPropertyType: "Missing property type.", | |
76 schemaRequired: "Schema value required.", | |
75 }; | 77 }; |
76 | 78 |
77 /** | 79 /** |
78 * Builds an error message. Key is the property in the |errors| object, and | 80 * Builds an error message. Key is the property in the |errors| object, and |
79 * |opt_replacements| is an array of values to replace "*" characters with. | 81 * |opt_replacements| is an array of values to replace "*" characters with. |
80 */ | 82 */ |
81 chromium.JSONSchemaValidator.formatError = function(key, opt_replacements) { | 83 chromium.JSONSchemaValidator.formatError = function(key, opt_replacements) { |
82 var message = this.messages[key]; | 84 var message = this.messages[key]; |
83 if (opt_replacements) { | 85 if (opt_replacements) { |
84 for (var i = 0; i < opt_replacements.length; i++) { | 86 for (var i = 0; i < opt_replacements.length; i++) { |
(...skipping 29 matching lines...) Expand all Loading... | |
114 | 116 |
115 /** | 117 /** |
116 * Validates an instance against a schema. The instance can be any JavaScript | 118 * Validates an instance against a schema. The instance can be any JavaScript |
117 * value and will be validated recursively. When this method returns, the | 119 * value and will be validated recursively. When this method returns, the |
118 * |errors| property will contain a list of errors, if any. | 120 * |errors| property will contain a list of errors, if any. |
119 */ | 121 */ |
120 chromium.JSONSchemaValidator.prototype.validate = function(instance, schema, | 122 chromium.JSONSchemaValidator.prototype.validate = function(instance, schema, |
121 opt_path) { | 123 opt_path) { |
122 var path = opt_path || ""; | 124 var path = opt_path || ""; |
123 | 125 |
126 if (!schema) { | |
127 this.addError(path, "schemaRequired"); | |
128 return; | |
129 } | |
130 | |
124 // If the schema has an extends property, the instance must validate against | 131 // If the schema has an extends property, the instance must validate against |
125 // that schema too. | 132 // that schema too. |
126 if (schema.extends) | 133 if (schema.extends) |
127 this.validate(instance, schema.extends, path); | 134 this.validate(instance, schema.extends, path); |
128 | 135 |
129 // If the schema has a choices property, the instance must validate against at | 136 // If the schema has a choices property, the instance must validate against at |
130 // least one of the items in that array. | 137 // least one of the items in that array. |
131 if (schema.choices) { | 138 if (schema.choices) { |
132 this.validateChoices(instance, schema, path); | 139 this.validateChoices(instance, schema, path); |
133 return; | 140 return; |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
202 }; | 209 }; |
203 | 210 |
204 /** | 211 /** |
205 * Validates an instance against an object schema and populates the errors | 212 * Validates an instance against an object schema and populates the errors |
206 * property. | 213 * property. |
207 */ | 214 */ |
208 chromium.JSONSchemaValidator.prototype.validateObject = function(instance, | 215 chromium.JSONSchemaValidator.prototype.validateObject = function(instance, |
209 schema, path) { | 216 schema, path) { |
210 for (var prop in schema.properties) { | 217 for (var prop in schema.properties) { |
211 var propPath = path ? path + "." + prop : prop; | 218 var propPath = path ? path + "." + prop : prop; |
212 if (prop in instance && instance[prop] !== null && | 219 if (!schema.properties[prop]) { |
220 this.addError(propPath, "invalidPropertyType"); | |
Aaron Boodman
2009/04/28 20:21:31
This branch will get entered when schema.propertie
Erik does not do reviews
2009/04/28 20:29:34
No. I'm looking for undefined. The problem I'm t
| |
221 } else if (prop in instance && instance[prop] !== null && | |
213 instance[prop] !== undefined) { | 222 instance[prop] !== undefined) { |
214 this.validate(instance[prop], schema.properties[prop], propPath); | 223 this.validate(instance[prop], schema.properties[prop], propPath); |
215 } else if (!schema.properties[prop].optional) { | 224 } else if (!schema.properties[prop].optional) { |
216 this.addError(propPath, "propertyRequired"); | 225 this.addError(propPath, "propertyRequired"); |
217 } | 226 } |
218 } | 227 } |
219 | 228 |
220 // By default, additional properties are not allowed on instance objects. This | 229 // By default, additional properties are not allowed on instance objects. This |
221 // can be overridden by setting the additionalProperties property to a schema | 230 // can be overridden by setting the additionalProperties property to a schema |
222 // which any additional properties must validate against. | 231 // which any additional properties must validate against. |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
367 return { | 376 return { |
368 choice: [ | 377 choice: [ |
369 type, | 378 type, |
370 { type: "array", item: type } | 379 { type: "array", item: type } |
371 ] | 380 ] |
372 }; | 381 }; |
373 }; | 382 }; |
374 | 383 |
375 chromium.types = types; | 384 chromium.types = types; |
376 })(); | 385 })(); |
OLD | NEW |