Chromium Code Reviews| Index: chrome/renderer/resources/extensions/json_schema.js |
| diff --git a/chrome/renderer/resources/extensions/json_schema.js b/chrome/renderer/resources/extensions/json_schema.js |
| index 9b109bfb6bcd276c2d91f69db2b28663caed4e5e..1df1863c5063789e56db37e8ace62878d07ae8d8 100644 |
| --- a/chrome/renderer/resources/extensions/json_schema.js |
| +++ b/chrome/renderer/resources/extensions/json_schema.js |
| @@ -144,6 +144,57 @@ chromeHidden.JSONSchemaValidator.prototype.addTypes = function(typeOrTypeList) { |
| } |
| /** |
| + * Returns a list of strings of the types that this schema accepts. |
| + */ |
| +chromeHidden.JSONSchemaValidator.prototype.getAllTypesForSchema = function( |
|
Aaron Boodman
2012/03/01 22:59:05
Nit: I usually see the "function(" on the next lin
|
| + schema) { |
| + var schemaTypes = []; |
| + if (schema.type) |
| + schemaTypes.push(schema.type); |
| + if (schema.choices) { |
| + for (var i = 0; i < schema.choices.length; i++) { |
| + var choiceTypes = this.getAllTypesForSchema(schema.choices[i]); |
| + schemaTypes = schemaTypes.concat(choiceTypes); |
| + } |
| + } |
| + if (schema['$ref']) { |
| + var refTypes = this.getAllTypesForSchema(this.types[schema['$ref']]); |
| + schemaTypes = schemaTypes.concat(refTypes); |
| + } |
| + return schemaTypes; |
| +}; |
| + |
| +/** |
| + * Returns true if |schema| would accept an argument of type |type|. |
| + */ |
| +chromeHidden.JSONSchemaValidator.prototype.isValidSchemaType = function( |
| + type, schema) { |
| + if (schema.optional && (type == "null" || type == "undefined")) |
| + return true; |
| + |
| + schemaTypes = this.getAllTypesForSchema(schema); |
| + for (var i = 0; i < schemaTypes.length; i++) { |
| + if (schemaTypes[i] == "any" || type == schemaTypes[i]) |
| + return true; |
| + } |
| + return type == "any"; |
| +}; |
| + |
| +/** |
| + * Returns true if there is a non-null argument that both |schema1| and |
| + * |schema2| would accept. |
| + */ |
| +chromeHidden.JSONSchemaValidator.prototype.checkSchemaOverlap = function( |
| + schema1, schema2) { |
| + var schema1Types = this.getAllTypesForSchema(schema1); |
| + for (var i = 0; i < schema1Types.length; i++) { |
| + if (this.isValidSchemaType(schema1Types[i], schema2)) |
| + return true; |
| + } |
| + return false; |
| +}; |
| + |
| +/** |
| * Validates an instance against a schema. The instance can be any JavaScript |
| * value and will be validated recursively. When this method returns, the |
| * |errors| property will contain a list of errors, if any. |
| @@ -450,4 +501,11 @@ chromeHidden.JSONSchemaValidator.prototype.addError = function( |
| }); |
| }; |
| +/** |
| + * Resets errors to an empty list so you can call 'validate' again. |
| + */ |
| +chromeHidden.JSONSchemaValidator.prototype.resetErrors = function() { |
| + this.errors = []; |
| +}; |
| + |
| })(); |