diff --git a/lib/jsv.js b/lib/jsv.js index 1ca04ae..1441fa7 100644 --- a/lib/jsv.js +++ b/lib/jsv.js @@ -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. @@ -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 undefined 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; }; /** @@ -1494,4 +1526,4 @@ var exports = exports || this, require("./environments"); //load default environments -}()); \ No newline at end of file +}());