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 schemaTypes = []; |
| 152 if (schema.type) |
| 153 schemaTypes.push(schema.type); |
| 154 if (schema.choices) { |
| 155 for (var i = 0; i < schema.choices.length; i++) |
| 156 schemaTypes.push(schema.choices[i].type); |
| 157 } |
| 158 if (schema['$ref']) { |
| 159 var refTypes = this.getAllTypesForSchema(this.types[schema['$ref']]); |
| 160 schemaTypes = schemaTypes.concat(refTypes); |
| 161 } |
| 162 return schemaTypes; |
| 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 schemaTypes = this.getAllTypesForSchema(schema); |
| 171 for (var i = 0; i < schemaTypes.length; i++) { |
| 172 if (schemaTypes[i] == "any" || type == schemaTypes[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 schema1Types = this.getAllTypesForSchema(schema1); |
| 185 for (var i = 0; i < schema1Types.length; i++) { |
| 186 if (this.isValidSchemaType(schema1Types[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 * Resets errors to an empty list so you can call 'validate' again. |
| 499 */ |
| 500 chromeHidden.JSONSchemaValidator.prototype.resetErrors = function() { |
| 501 this.errors = []; |
| 502 }; |
| 503 |
451 })(); | 504 })(); |
OLD | NEW |