OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
137 if (typeOrTypeList instanceof Array) { | 137 if (typeOrTypeList instanceof Array) { |
138 for (var i = 0; i < typeOrTypeList.length; i++) { | 138 for (var i = 0; i < typeOrTypeList.length; i++) { |
139 addType(this, typeOrTypeList[i]); | 139 addType(this, typeOrTypeList[i]); |
140 } | 140 } |
141 } else { | 141 } else { |
142 addType(this, typeOrTypeList); | 142 addType(this, typeOrTypeList); |
143 } | 143 } |
144 } | 144 } |
145 | 145 |
146 /** | 146 /** |
147 * Returns a list of strings of the types that this schema accepts. | |
148 */ | |
149 chromeHidden.JSONSchemaValidator.prototype.getAllTypesForSchema = function( | |
150 schema) { | |
151 var schema_types = []; | |
152 if (schema.type) | |
153 schema_types.push(schema.type); | |
154 if (schema.choices) { | |
155 for (var i = 0; i < schema.choices.length; i++) | |
156 schema_types.push(schema.choices[i].type); | |
157 } | |
158 if (schema['$ref']) { | |
159 var ref_types = this.getAllTypesForSchema(this.types[schema['$ref']]); | |
160 schema_types = schema_types.concat(ref_types); | |
161 } | |
162 return schema_types; | |
163 }; | |
164 | |
165 /** | |
166 * Returns true if |schema| would accept an argument of type |type|. | |
167 */ | |
168 chromeHidden.JSONSchemaValidator.prototype.isValidSchemaType = function( | |
169 type, schema) { | |
170 schema_types = this.getAllTypesForSchema(schema); | |
171 for (var i = 0; i < schema_types.length; i++) { | |
172 if (schema_types[i] == "any" || type == schema_types[i]) | |
173 return true; | |
174 } | |
175 return type == "any"; | |
176 }; | |
177 | |
178 /** | |
179 * Returns true if there is a non-null argument that both |schema1| and | |
180 * |schema2| would accept. | |
181 */ | |
182 chromeHidden.JSONSchemaValidator.prototype.checkSchemaOverlap = function( | |
183 schema1, schema2) { | |
184 var schema1_types = this.getAllTypesForSchema(schema1); | |
185 for (var i = 0; i < schema1_types.length; i++) { | |
186 if (this.isValidSchemaType(schema1_types[i], schema2)) | |
187 return true; | |
188 } | |
189 return false; | |
190 }; | |
191 | |
192 /** | |
147 * Validates an instance against a schema. The instance can be any JavaScript | 193 * Validates an instance against a schema. The instance can be any JavaScript |
148 * value and will be validated recursively. When this method returns, the | 194 * value and will be validated recursively. When this method returns, the |
149 * |errors| property will contain a list of errors, if any. | 195 * |errors| property will contain a list of errors, if any. |
150 */ | 196 */ |
151 chromeHidden.JSONSchemaValidator.prototype.validate = function( | 197 chromeHidden.JSONSchemaValidator.prototype.validate = function( |
152 instance, schema, opt_path) { | 198 instance, schema, opt_path) { |
153 var path = opt_path || ""; | 199 var path = opt_path || ""; |
154 | 200 |
155 if (!schema) { | 201 if (!schema) { |
156 this.addError(path, "schemaRequired"); | 202 this.addError(path, "schemaRequired"); |
(...skipping 284 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
441 * message. | 487 * message. |
442 */ | 488 */ |
443 chromeHidden.JSONSchemaValidator.prototype.addError = function( | 489 chromeHidden.JSONSchemaValidator.prototype.addError = function( |
444 path, key, replacements) { | 490 path, key, replacements) { |
445 this.errors.push({ | 491 this.errors.push({ |
446 path: path, | 492 path: path, |
447 message: chromeHidden.JSONSchemaValidator.formatError(key, replacements) | 493 message: chromeHidden.JSONSchemaValidator.formatError(key, replacements) |
448 }); | 494 }); |
449 }; | 495 }; |
450 | 496 |
497 /** | |
498 * Adds an error message. |key| is an index into the |messages| object. | |
499 * |replacements| is an array of values to replace '*' characters in the | |
500 * message. | |
Matt Perry
2012/02/24 02:00:12
Comment needs updating.
Matt Tytel
2012/02/24 02:29:36
Done.
| |
501 */ | |
502 chromeHidden.JSONSchemaValidator.prototype.resetErrors = function() { | |
503 this.errors = []; | |
504 }; | |
505 | |
451 })(); | 506 })(); |
OLD | NEW |