Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #53 - Add initial support for lookups via json pointer #54

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions lib/jsv.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,20 @@ var exports = exports || this,
return o.toString();
}
}

function traversePointer(obj, pointer) {
if (pointer !== "/") {
var parr = pointer.split("/").slice(1);
for (var part = parr.shift(); part; part = parr.shift()) {
if (obj.hasOwnProperty(part)) {
obj = obj[part];
} else {
return null;
}
}
}
return obj;
}

/**
* The exception that is thrown when a schema fails to be created.
Expand Down Expand Up @@ -1055,11 +1069,29 @@ var exports = exports || this,
* Returns the schema registered with the provided URI.
*
* @param {String} uri The absolute URI of the required schema
* @param {JSONSchema} [parentSchema] The schema of the parent/containing instance, used in case the schema is referenced by JSON pointer.
* @returns {JSONSchema|undefined} The request schema, or <code>undefined</code> if not found
*/

Environment.prototype.findSchema = function (uri) {
return this._schemas[formatURI(uri)];
var furi = formatURI(uri);
var schema = null;
if (furi in this._schemas) {
schema = this._schemas[furi];
} else {
var base = furi.substr(0, furi.indexOf("#"));
if (formatURI(base) != furi) {
var baseSchema = this.findSchema(base);
if (baseSchema) {
var fragment = furi.substr(base.length);
var targetElem = traversePointer(baseSchema.getValue(), fragment);
if (targetElem) {
schema = this.createSchema(targetElem, null, furi);
}
}
}
}
return schema;
};

/**
Expand Down Expand Up @@ -1494,4 +1526,4 @@ var exports = exports || this,

require("./environments"); //load default environments

}());
}());