diff options
author | rafaelw@chromium.org <rafaelw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-08-05 01:13:46 +0000 |
---|---|---|
committer | rafaelw@chromium.org <rafaelw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-08-05 01:13:46 +0000 |
commit | 97880fa256d45accb61d1c9887bb7e28724286cd (patch) | |
tree | d9156ef351bdc94c352285095846f392423d3099 /chrome/renderer | |
parent | ae6113fd22307e735cd27716e64dea75af6abecd (diff) | |
download | chromium_src-97880fa256d45accb61d1c9887bb7e28724286cd.zip chromium_src-97880fa256d45accb61d1c9887bb7e28724286cd.tar.gz chromium_src-97880fa256d45accb61d1c9887bb7e28724286cd.tar.bz2 |
Support for referenced schema types in json_schema.
Schema types can be referenced with "$ref" to either internal types or explicitly added types via addTypes() that define their typename via an "id" property.
Review URL: http://codereview.chromium.org/159868
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@22451 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/renderer')
-rw-r--r-- | chrome/renderer/extensions/json_schema_unittest.cc | 5 | ||||
-rw-r--r-- | chrome/renderer/renderer_resources.grd | 2 | ||||
-rw-r--r-- | chrome/renderer/resources/json_schema.js | 36 |
3 files changed, 42 insertions, 1 deletions
diff --git a/chrome/renderer/extensions/json_schema_unittest.cc b/chrome/renderer/extensions/json_schema_unittest.cc index 7d4073f..9b53094 100644 --- a/chrome/renderer/extensions/json_schema_unittest.cc +++ b/chrome/renderer/extensions/json_schema_unittest.cc @@ -77,3 +77,8 @@ TEST_F(JsonSchemaTest, TestNumber) { TEST_F(JsonSchemaTest, TestType) { TestFunction("testType"); } + +TEST_F(JsonSchemaTest, TestTypeReference) { + TestFunction("testTypeReference"); +} + diff --git a/chrome/renderer/renderer_resources.grd b/chrome/renderer/renderer_resources.grd index ec1d32c..5ec2309 100644 --- a/chrome/renderer/renderer_resources.grd +++ b/chrome/renderer/renderer_resources.grd @@ -1,6 +1,6 @@ <?xml version="1.0" encoding="UTF-8"?> <!-- This comment is only here because changes to resources are not picked up -without changes to the corresponding grd file. ek2 --> +without changes to the corresponding grd file. rw --> <grit latest_public_release="0" current_release="1"> <outputs> <output filename="grit/renderer_resources.h" type="rc_header"> diff --git a/chrome/renderer/resources/json_schema.js b/chrome/renderer/resources/json_schema.js index f5d06c6..70dd26f 100644 --- a/chrome/renderer/resources/json_schema.js +++ b/chrome/renderer/resources/json_schema.js @@ -55,6 +55,7 @@ var chrome = chrome || {}; */ chrome.JSONSchemaValidator = function() { this.errors = []; + this.types = []; }; chrome.JSONSchemaValidator.messages = { @@ -74,6 +75,7 @@ chrome.JSONSchemaValidator.messages = { invalidChoice: "Value does not match any valid type choices.", invalidPropertyType: "Missing property type.", schemaRequired: "Schema value required.", + unknownSchemaReference: "Unknown schema reference: *." }; /** @@ -115,6 +117,27 @@ chrome.JSONSchemaValidator.getType = function(value) { }; /** + * Add types that may be referenced by validated schemas that reference them + * with "$ref": <typeId>. Each type must be a valid schema and define an + * "id" property. + */ +chrome.JSONSchemaValidator.prototype.addTypes = function(typeOrTypeList) { + function addType(validator, type) { + if(!type.id) + throw "Attempt to addType with missing 'id' property"; + validator.types[type.id] = type; + } + + if (typeOrTypeList instanceof Array) { + for (var i = 0; i < typeOrTypeList.length; i++) { + addType(this, typeOrTypeList[i]); + } + } else { + addType(this, typeOrTypeList); + } +} + +/** * 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. @@ -128,10 +151,23 @@ chrome.JSONSchemaValidator.prototype.validate = function(instance, schema, return; } + // If this schema defines itself as reference type, save it in this.types. + if (schema.id) + this.types[schema.id] = schema; + // If the schema has an extends property, the instance must validate against // that schema too. if (schema.extends) this.validate(instance, schema.extends, path); + + // If the schema has a $ref property, the instance must validate against + // that schema too. It must be present in this.types to be referenced. + if (schema["$ref"]) { + if (!this.types[schema["$ref"]]) + this.addError(path, "unknownSchemaReference", [ schema["$ref"] ]); + else + this.validate(instance, this.types[schema["$ref"]], path) + } // If the schema has a choices property, the instance must validate against at // least one of the items in that array. |