Spaces:
Sleeping
Sleeping
File size: 9,236 Bytes
4cadbaf |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
var env;
//calls ok(true) if no error is thrown
function okNoError(func, msg) {
try {
func();
ok(true, msg);
} catch (e) {
ok(false, msg + ': ' + e);
}
}
function setupEnv () {
env = env || require('../lib/jsv').JSV.createEnvironment("json-schema-draft-03");
// AddressBook example from http://relaxng.org/compact-tutorial-20030326.html
env.createSchema({
"type": "object",
"id": "http://example.com/addressbook.json",
"description": "AddressBook example from http://relaxng.org/compact-tutorial-20030326.html",
"properties": {
"cards": {
"type": "array",
"items": {
"type": "array",
"items": {
"type": "string"
},
"minItems": 2,
"maxItems": 2,
"$schema":"http://json-schema.org/draft-03/schema#"
},
"required": true
}
},
"$schema":"http://json-schema.org/draft-03/schema#"
},
undefined,
"http://example.com/addressbook.json");
// The referral target schema, with a canonical id.
env.createSchema({
"type": "array",
"id": "http://example.com/subdir/card.json",
"description": "Referral target",
"items": {
"type": "string"
},
"minItems": 2,
"maxItems": 2,
"$schema":"http://json-schema.org/draft-03/schema#"
},
undefined,
"http://example.com/subdir/card.json");
// Similar example, using $ref to factor part of the schema.
env.createSchema({
"type": "object",
"id": "http://example.com/addressbook_ref.json",
"description": "Similar example, using $ref to factor part of the schema.",
"properties": {
"cards": {
"type": "array",
"items": {
"$ref": "./subdir/card.json"
},
"required": true
}
},
"$schema":"http://json-schema.org/draft-03/schema#"
},
undefined,
"http://example.com/addressbook_ref.json");
// Similar example, using extends to factor part of the schema.
env.createSchema({
"type": "object",
"id": "http://example.com/addressbook_extends.json",
"description": "Similar example, using extends to factor part of the schema.",
"properties": {
"cards": {
"type": "array",
"items": {
"extends": {
"$ref": "./subdir/card.json"
}
},
"required": true
}
},
"$schema":"http://json-schema.org/draft-03/schema#"
},
undefined,
"http://example.com/addressbook_extends.json");
}
//
//
// Tests
//
//
module("Reference Tests");
test("Self Identifying Schemas", function () {
setupEnv();
// While createSchema takes a URI argument, for V3 schemas the validator should be able
// to use the canonical id field to find it, if it has been registered.
// http://tools.ietf.org/html/draft-zyp-json-schema-03#section-5.27
env.createSchema({ "id": "http://example.com/foo.json",
"$schema":"http://json-schema.org/draft-03/schema#"
});
ok(env.findSchema("http://example.com/foo.json"), "found schema by id");
});
test("Forward Referral", function () {
setupEnv();
// A schema with a reference in it should be accepted, even if the destination schema
// hasn't been registered yet.
okNoError(function () {
env.createSchema({
"type": "object",
"id": "http://example.com/addressbook.json",
"properties": {
"cards": {
"type": "array",
"items": {
"$ref": "http://example.com/subdir/card.json"
},
"required": true
}
},
"$schema":"http://json-schema.org/draft-03/schema#"
});
}, "Can make schema with forward reference");
});
test("Validate Schema", function () {
setupEnv();
var jsonschema = env.findSchema(env.getOption("latestJSONSchemaSchemaURI"));
var explicit_schema = env.findSchema("http://example.com/addressbook.json");
var referring_schema = env.findSchema("http://example.com/addressbook_ref.json");
var card_schema = env.findSchema("http://example.com/subdir/card.json");
ok(explicit_schema, "explicit addressbook schema");
ok(referring_schema, "referring addressbook schema");
ok(card_schema, "card schema");
equal(jsonschema.validate(explicit_schema).errors.length, 0, 'valid explicit schema');
equal(jsonschema.validate(referring_schema).errors.length, 0, 'valid referring schema');
equal(jsonschema.validate(card_schema).errors.length, 0, 'valid referral target schema');
});
test("Simple Referral", function () {
setupEnv();
var schema = { "$ref": "http://example.com/subdir/card.json" };
notEqual(env.validate({}, schema).errors.length, 0, "card must be array");
notEqual(env.validate([], schema).errors.length, 0, "card must have fields");
notEqual(env.validate(["foo"], schema).errors.length, 0, "card must have two fields");
notEqual(env.validate(["foo", {}], schema).errors.length, 0, "both fields must be string");
notEqual(env.validate(["foo", "bar", "baz"], schema).errors.length, 0, "card maxItems 2");
equal(env.validate(["foo", "bar"], schema).errors.length, 0, "card maxItems 2");
});
function validateAddressbook (test_name, schema_uri) {
var schema = { "$ref": schema_uri };
test(test_name, function () {
setupEnv();
notEqual(env.validate('', schema).errors.length, 0, "addressbook is object");
notEqual(env.validate({}, schema).errors.length, 0, "cards required");
notEqual(env.validate({ "cards": {}}, schema).errors.length, 0, "cards must be array");
equal(env.validate({ "cards": []}, schema).errors.length, 0, "empty array ok");
notEqual(env.validate({ "cards": [ {} ] }, schema).errors.length, 0,
"cards schema is enforced on items");
notEqual(env.validate({ "cards": [['foo']]}, schema).errors.length, 0,
"each card must have length 2");
notEqual(env.validate({ "cards": [ ['foo', 'bar' ], ["foo" ] ] }, schema).errors.length, 0,
"second card is bad");
equal(env.validate({ "cards": [ ["foo", "bar" ] ] }, schema).errors.length, 0,
"good addressbook with one card");
equal(env.validate({ "cards": [ ["foo", "bar" ], ["bar", "foo" ] ] }, schema).errors.length, 0,
"good addressbook with two cards");
});
}
validateAddressbook("Explicit Schema", "http://example.com/addressbook.json");
validateAddressbook("Referring Schema", "http://example.com/addressbook_ref.json");
validateAddressbook("Extends Schema", "http://example.com/addressbook_extends.json");
|