Skip to content

Commit

Permalink
refactor(state): Add full uri check for resolution purpose
Browse files Browse the repository at this point in the history
  • Loading branch information
korzio committed Jul 18, 2017
1 parent fb9c5a9 commit 1bdf4e8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
6 changes: 6 additions & 0 deletions lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
12 changes: 6 additions & 6 deletions lib/utils/state.js
Original file line number Diff line number Diff line change
@@ -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, {
Expand Down Expand Up @@ -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);
}

Expand All @@ -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)
);
}

Expand All @@ -108,7 +108,7 @@ State.prototype = Object.assign(Object.create(Array.prototype), {
}
}

// this.push(parentSchema);
this.push(parentSchema);
return parentSchema;
},
descent(reference, parentSchema) {
Expand Down

0 comments on commit 1bdf4e8

Please sign in to comment.