diff --git a/lib/utils/index.js b/lib/utils/index.js index af30262..92d40c7 100644 --- a/lib/utils/index.js +++ b/lib/utils/index.js @@ -44,10 +44,16 @@ function tail(uri) { return parts[1]; } +function isFullUri(uri) { + return /:\/\//.test(uri); +} + module.exports = { cleanId, asExpression, + // TODO move to utils/uri joinPath, + isFullUri, head, tail, }; diff --git a/lib/utils/state.js b/lib/utils/state.js index f20c7f8..710bc85 100644 --- a/lib/utils/state.js +++ b/lib/utils/state.js @@ -1,6 +1,6 @@ const { list: validators } = require('../validators'); const { body, restore, template } = require('./template'); -const { joinPath, head, tail } = require('./'); +const { joinPath, head, tail, isFullUri } = require('./'); function State(schema = {}, env) { Object.assign(this, { @@ -63,7 +63,7 @@ State.prototype = Object.assign(Object.create(Array.prototype), { // if schema id is partial // it should be resolved against the closest parent(s) // so only valid URIs added to resolution - if (schema && schema.id && this.length && /:\/\//.test(schema.id)) { + if (schema && schema.id && this.length && isFullUri(schema.id)) { this.resolution.push(this.length); } @@ -79,16 +79,16 @@ State.prototype = Object.assign(Object.create(Array.prototype), { * @returns {object} parentSchema */ ascend(reference) { - const indexOfParent = this.resolution[this.resolution.length - 1]; + const indexOfParent = Math.max(this.resolution[this.resolution.length - 1], 1); let parentSchemaPath = head(reference); let parentSchema = this[indexOfParent]; - if (reference && reference !== '#' && this.length > 1) { + if (parentSchemaPath && !isFullUri(reference) && this.length > 1) { parentSchemaPath = joinPath( this.slice(indexOfParent) .map(({ id }) => id) - .concat(reference) + .concat(parentSchemaPath) ); } @@ -108,7 +108,7 @@ State.prototype = Object.assign(Object.create(Array.prototype), { } } - // this.push(parentSchema); + this.push(parentSchema); return parentSchema; }, descent(reference, parentSchema) {