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/test | |
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/test')
-rw-r--r-- | chrome/test/data/extensions/json_schema_test.js | 78 |
1 files changed, 76 insertions, 2 deletions
diff --git a/chrome/test/data/extensions/json_schema_test.js b/chrome/test/data/extensions/json_schema_test.js index 195dcba..4231888 100644 --- a/chrome/test/data/extensions/json_schema_test.js +++ b/chrome/test/data/extensions/json_schema_test.js @@ -7,8 +7,10 @@ function assert(truth) { throw new Error("Assertion failed."); } -function assertValid(type, instance, schema) { +function assertValid(type, instance, schema, types) { var validator = new chrome.JSONSchemaValidator(); + if (types) + validator.addTypes(types); validator["validate" + type](instance, schema, ""); if (validator.errors.length != 0) { log("Got unexpected errors"); @@ -17,8 +19,10 @@ function assertValid(type, instance, schema) { } } -function assertNotValid(type, instance, schema, errors) { +function assertNotValid(type, instance, schema, errors, types) { var validator = new chrome.JSONSchemaValidator(); + if (types) + validator.addTypes(types); validator["validate" + type](instance, schema, ""); assert(validator.errors.length === errors.length); for (var i = 0; i < errors.length; i++) { @@ -199,6 +203,76 @@ function testObject() { [formatError("invalidType", ["integer", "string"])]); } +function testTypeReference() { + var referencedTypes = [ + { + id: "MinLengthString", + type: "string", + minLength: 2 + }, + { + id: "Max10Int", + type: "integer", + maximum: 10 + } + ]; + + var schema = { + type: "object", + properties: { + "foo": { + type: "string" + }, + "bar": { + $ref: "Max10Int" + }, + "baz": { + $ref: "MinLengthString" + } + } + }; + + var schemaInlineReference = { + type: "object", + properties: { + "foo": { + type: "string" + }, + "bar": { + id: "NegativeInt", + type: "integer", + maximum: 0 + }, + "baz": { + $ref: "NegativeInt" + } + } + } + + // Valid type references externally added. + assertValid("", {foo:"foo",bar:4,baz:"ab"}, schema, referencedTypes); + + // Valida type references internally defined. + assertValid("", {foo:"foo",bar:-4,baz:-2}, schemaInlineReference); + + // Failures in validation, but succesful schema reference. + assertNotValid("", {foo:"foo",bar:4,baz:"a"}, schema, + [formatError("stringMinLength", [2])], referencedTypes); + assertNotValid("", {foo:"foo",bar:20,baz:"abc"}, schema, + [formatError("numberMaxValue", [10])], referencedTypes); + + // Remove MinLengthString type. + referencedTypes.shift(); + assertNotValid("", {foo:"foo",bar:4,baz:"ab"}, schema, + [formatError("unknownSchemaReference", ["MinLengthString"])], + referencedTypes); + + // Remove internal type "NegativeInt" + delete schemaInlineReference.properties.bar; + assertNotValid("", {foo:"foo",baz:-2}, schemaInlineReference, + [formatError("unknownSchemaReference", ["NegativeInt"])]); +} + function testArrayTuple() { var schema = { items: [ |