Comparing version 0.2.3 to 0.2.4
@@ -5,2 +5,3 @@ 'use strict'; | ||
$.findByRef = require('./util/find-reference'); | ||
$.resolveSchema = require('./util/resolve-schema'); | ||
@@ -18,3 +19,3 @@ $.normalizeSchema = require('./util/normalize-schema'); | ||
if (typeof refs !== 'object') { | ||
if (!Array.isArray(refs)) { | ||
ex = !!refs; | ||
@@ -24,25 +25,30 @@ refs = []; | ||
function pushReference(from) { | ||
var fixed = $.normalizeSchema(fakeroot, from); | ||
function push(ref) { | ||
if (typeof ref.id === 'string') { | ||
var base = $.getDocumentURI(ref.id) || ref.id; | ||
var base = $.getDocumentURI(fixed.id); | ||
if (/#([^\/]+)/.test(ref.id)) { | ||
base = ref.id.split('#')[1]; | ||
if (!$ref.refs[base]) { | ||
$ref.refs[base] = fixed; | ||
} | ||
if (!$ref.refs[base]) { | ||
$ref.refs[base] = { | ||
$ref: ref.id | ||
}; | ||
} | ||
var id = fixed.id.split('#')[1] || ''; | ||
base = ref.id; | ||
} | ||
if (id && !$ref.refs[id]) { | ||
$ref.refs[id] = fixed; | ||
if (!$ref.refs[base]) { | ||
$ref.refs[base] = ref; | ||
} | ||
} | ||
} | ||
for (var key in refs) { | ||
pushReference(refs[key]); | ||
} | ||
refs.concat([schema]).forEach(function(ref) { | ||
schema = $.normalizeSchema(fakeroot, ref, push); | ||
push(schema); | ||
}); | ||
pushReference(schema); | ||
return $.resolveSchema($.normalizeSchema(fakeroot, schema), $ref.refs, ex); | ||
return $.resolveSchema(schema, $ref.refs, ex); | ||
} | ||
@@ -49,0 +55,0 @@ |
'use strict'; | ||
module.exports = function(refs, id) { | ||
console.log(id); | ||
console.log(refs); | ||
var $ = require('./uri-helpers'); | ||
var cloneObj = require('./clone-obj'); | ||
function get(obj, path) { | ||
var hash = path.split('#')[1]; | ||
var parts = hash.split('/').slice(1); | ||
while (parts.length) { | ||
var key = decodeURIComponent(parts.shift()).replace(/~1/g, '/').replace(/~0/g, '~'); | ||
if (typeof obj[key] === 'undefined') { | ||
throw new Error('Reference not found: ' + path); | ||
} | ||
obj = obj[key]; | ||
} | ||
return obj; | ||
} | ||
var find = module.exports = function(id, refs) { | ||
var target = refs[id] || refs[id.split('#')[1]] || refs[$.getDocumentURI(id)]; | ||
if (!target) { | ||
for (var key in refs) { | ||
if ($.resolveURL(refs[key].id, id) === refs[key].id) { | ||
target = refs[key]; | ||
break; | ||
} | ||
} | ||
} | ||
if (id.indexOf('#/') > -1) { | ||
target = get(target, id); | ||
} | ||
if (!target) { | ||
throw new Error('Reference not found: ' + id); | ||
} | ||
while (target.$ref) { | ||
target = find(target.$ref, refs); | ||
} | ||
if (id !== target.id) { | ||
target = cloneObj(target); | ||
target.id = $.resolveURL(target.id, id); | ||
} | ||
return target; | ||
}; |
@@ -5,5 +5,7 @@ 'use strict'; | ||
var cloneObj = require('./clone-obj'); | ||
var SCHEMA_URI = 'http://json-schema.org/schema#'; | ||
function expand(obj, parent) { | ||
function expand(obj, parent, callback) { | ||
if (obj) { | ||
@@ -23,39 +25,14 @@ if (typeof obj.id === 'string') { | ||
if (typeof value === 'object') { | ||
expand(value, parent); | ||
expand(value, parent, callback); | ||
} | ||
} | ||
} | ||
function clone(obj, seen) { | ||
if (!obj || typeof obj !== 'object') { | ||
return obj; | ||
if (typeof callback === 'function') { | ||
callback(obj); | ||
} | ||
var target = Array.isArray(obj) ? [] : {}; | ||
if (seen.indexOf(obj) !== -1) { | ||
return target; | ||
} | ||
seen.push(obj); | ||
function copy(key, value) { | ||
target[key] = clone(value, seen); | ||
} | ||
if (Array.isArray(target)) { | ||
obj.forEach(function(value, key) { | ||
copy(key, value); | ||
}); | ||
} else if (Object.prototype.toString.call(obj) === '[object Object]') { | ||
Object.keys(obj).forEach(function(key) { | ||
copy(key, obj[key]); | ||
}); | ||
} | ||
return target; | ||
} | ||
module.exports = function(fakeroot, schema) { | ||
module.exports = function(fakeroot, schema, push) { | ||
if (typeof fakeroot === 'object') { | ||
push = schema; | ||
schema = fakeroot; | ||
@@ -65,3 +42,3 @@ fakeroot = null; | ||
var copy = clone(schema, []); | ||
var copy = cloneObj(schema); | ||
@@ -76,5 +53,5 @@ if (!copy.$schema) { | ||
expand(copy, copy.id); | ||
expand(copy, copy.id, push); | ||
return copy; | ||
}; |
@@ -5,20 +5,4 @@ 'use strict'; | ||
function get(obj, path) { | ||
var hash = path.split('#')[1]; | ||
var find = require('./find-reference'); | ||
var parts = hash.split('/').slice(1); | ||
while (parts.length) { | ||
var key = decodeURIComponent(parts.shift()).replace(/~1/g, '/').replace(/~0/g, '~'); | ||
if (typeof obj[key] === 'undefined') { | ||
throw new Error('Reference not found: ' + path); | ||
} | ||
obj = obj[key]; | ||
} | ||
return obj; | ||
} | ||
function clone(obj, refs, child, expand) { | ||
@@ -32,22 +16,8 @@ var copy = {}; | ||
if ($.isURL(obj.$ref)) { | ||
var fixed = find(obj.$ref, refs); | ||
var id = obj.$ref.split('#')[1], | ||
base = $.getDocumentURI(obj.$ref) || obj.$ref; | ||
if (fixed && expand) { | ||
obj = fixed; | ||
if (refs[id] || refs[base]) { | ||
var fixed = refs[id] || refs[base]; | ||
if (obj.$ref.indexOf('#/') > -1) { | ||
fixed = get(fixed, obj.$ref); | ||
} | ||
if (obj.$ref !== fixed.id) { | ||
return clone(fixed, refs, true, expand); | ||
} | ||
if (expand) { | ||
obj = fixed; | ||
delete obj.$ref; | ||
} | ||
delete obj.$ref; | ||
} | ||
@@ -54,0 +24,0 @@ } |
@@ -40,3 +40,3 @@ 'use strict'; | ||
pathname = output.join(''); | ||
pathname = output.join('') || '/'; | ||
@@ -43,0 +43,0 @@ if (flag) { |
{ | ||
"name": "deref", | ||
"version": "0.2.3", | ||
"version": "0.2.4", | ||
"description": "JSON-Schema $ref resolution", | ||
@@ -12,2 +12,3 @@ "main": "lib/index.js", | ||
"clone": "^0.1.18", | ||
"glob": "^4.2.1", | ||
"grunt": "^0.4.5", | ||
@@ -14,0 +15,0 @@ "grunt-parts": "^0.3.0", |
@@ -94,6 +94,8 @@ Do you have $ref's ? | ||
- **refs** (array|object) | ||
- **refs** (array) | ||
Any additional schemas used while dereferencing. | ||
Since `0.2.4` passing an object is not longer supported. | ||
- **ex** (boolean) | ||
@@ -129,5 +131,8 @@ | ||
- `getDocumentURI(path)` | ||
- `findByRef(uri, refs)` | ||
- `resolveSchema(schema, refs)` | ||
- `normalizeSchema(fakeroot, schema)` | ||
Any `refs` passed MUST be an object of normalized schemas. | ||
Note that calling `$(schema)` will not read/download any local/remote files. | ||
@@ -134,0 +139,0 @@ |
10400
8
266
141
7