Skip to content

Commit

Permalink
Added self referencing support to links.
Browse files Browse the repository at this point in the history
  • Loading branch information
garycourt committed Nov 29, 2010
1 parent 9be261a commit 4eae4c6
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 9 deletions.
9 changes: 7 additions & 2 deletions lib/json-schema-draft-01.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* @fileOverview Implementation of the first revision of the JSON Schema specification draft.
* @author <a href="mailto:[email protected]">Gary Court</a>
* @version 1.3
* @version 1.4
* @see http://github.com/garycourt/JSV
*/

Expand Down Expand Up @@ -799,7 +799,12 @@
var instance = arg[1],
href = link["href"];
href = href.replace(/\{(.+)\}/g, function (str, p1, offset, s) {
var value = instance.getValueOfProperty(p1);
var value;
if (p1 === "-this") {
value = instance.getValue();
} else {
value = instance.getValueOfProperty(p1);
}
return value !== undefined ? String(value) : "";
});
return href ? JSV.formatURI(instance.resolveURI(href)) : href;
Expand Down
9 changes: 7 additions & 2 deletions lib/json-schema-draft-02.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* @fileOverview Implementation of the second revision of the JSON Schema specification draft.
* @author <a href="mailto:[email protected]">Gary Court</a>
* @version 1.3
* @version 1.4
* @see http://github.com/garycourt/JSV
*/

Expand Down Expand Up @@ -823,7 +823,12 @@
var instance = arg[1],
href = link["href"];
href = href.replace(/\{(.+)\}/g, function (str, p1, offset, s) {
var value = instance.getValueOfProperty(p1);
var value;
if (p1 === "-this") {
value = instance.getValue();
} else {
value = instance.getValueOfProperty(p1);
}
return value !== undefined ? String(value) : "";
});
return href ? JSV.formatURI(instance.resolveURI(href)) : href;
Expand Down
21 changes: 17 additions & 4 deletions lib/json-schema-draft-03.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* @fileOverview Implementation of the third revision of the JSON Schema specification draft.
* @author <a href="mailto:[email protected]">Gary Court</a>
* @version 0.9
* @version 1.0
* @see http://github.com/garycourt/JSV
*/

Expand Down Expand Up @@ -811,7 +811,8 @@
var links,
linkSchemaURI = self.getValueOfProperty("items")["$ref"],
linkSchema = self.getEnvironment().findSchema(linkSchemaURI),
linkParser = linkSchema && linkSchema.getValueOfProperty("parser");
linkParser = linkSchema && linkSchema.getValueOfProperty("parser"),
selfReferenceVariable;
arg = JSV.toArray(arg);

if (typeof linkParser === "function") {
Expand All @@ -829,19 +830,27 @@
}

if (arg[1]) {
selfReferenceVariable = self.getValueOfProperty("selfReferenceVariable");
links = JSV.mapArray(links, function (link) {
var instance = arg[1],
href = link["href"];
href = href.replace(/\{(.+)\}/g, function (str, p1, offset, s) {
var value = instance.getValueOfProperty(p1);
var value;
if (p1 === selfReferenceVariable) {
value = instance.getValue();
} else {
value = instance.getValueOfProperty(p1);
}
return value !== undefined ? String(value) : "";
});
return href ? JSV.formatURI(instance.resolveURI(href)) : href;
});
}

return links;
}
},

"selfReferenceVariable" : "-this"
},

"fragmentResolution" : {
Expand Down Expand Up @@ -1451,6 +1460,10 @@
"id" : "http://json-schema.org/draft-03/hyper-schema#",

"properties" : {
"links" : {
"selfReferenceVariable" : "@"
},

"root" : {
"deprecated" : true
},
Expand Down
5 changes: 5 additions & 0 deletions tests/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,11 @@ test("Links Validation", function () {
schema = env.createSchema({ "properties" : { "two" : { "id" : "http://test.example.com/2", "type" : "object" } } }, null, "http://not.example.com/2");
equal(env.validate({}, { "$ref" : "http://test.example.com/2" }).errors.length, 0);
notEqual(env.validate(null, { "$ref" : "http://test.example.com/2" }).errors.length, 0);

//links api
schema = env.createSchema({ "links" : [ { "rel" : "bar", "href" : "http:{-this}#" } ] });
instance = env.createInstance("foo");
equal(schema.getLink("bar", instance), "http:foo#", "'bar' link and self reference");
});

test("PathStart Validation", function () {
Expand Down
9 changes: 8 additions & 1 deletion tests/tests3.js
Original file line number Diff line number Diff line change
Expand Up @@ -425,8 +425,9 @@ test("JSON Schema Validation", function () {
equal(hyperSchema.validate(links).errors.length, 0, "hyperSchema.validate(links)");
});

(function(curDraftId){
test("Links Validation", function () {
var schema;
var schema, instance;
//full
equal(env.validate({ 'a' : {} }, { 'type' : 'object', 'additionalProperties' : { '$ref' : '#' } }).errors.length, 0);
notEqual(env.validate({ 'a' : 1 }, { 'type' : 'object', 'additionalProperties' : { '$ref' : '#' } }).errors.length, 0);
Expand All @@ -440,7 +441,13 @@ test("Links Validation", function () {
schema = env.createSchema({ "properties" : { "two" : { "id" : "http://test.example.com/2", "type" : "object" } } }, null, "http://not.example.com/2");
equal(env.validate({}, { "$ref" : "http://test.example.com/2" }).errors.length, 0);
notEqual(env.validate(null, { "$ref" : "http://test.example.com/2" }).errors.length, 0);

//links api
schema = env.createSchema({ "links" : [ { "rel" : "bar", "href" : "http:" + (curDraftId < 3 ? "{-this}" : "{@}") + "#" } ] });
instance = env.createInstance("foo");
equal(schema.getLink("bar", instance), "http:foo#", "'bar' link and self reference");
});
}(curDraftId));

test("PathStart Validation", function () {
var instance = env.createInstance({}, "http://test.example.com/4"),
Expand Down

0 comments on commit 4eae4c6

Please sign in to comment.