2015-05-30 13:50:18 +03:00
|
|
|
'use strict';
|
|
|
|
|
2015-06-07 14:46:53 +03:00
|
|
|
var url = require('url')
|
2015-06-16 19:10:04 +03:00
|
|
|
, equal = require('./equal')
|
|
|
|
, util = require('./util');
|
2015-05-30 13:50:18 +03:00
|
|
|
|
2015-06-07 14:46:53 +03:00
|
|
|
module.exports = resolve;
|
|
|
|
|
|
|
|
resolve.normalizeId = normalizeId;
|
|
|
|
resolve.fullPath = getFullPath;
|
|
|
|
resolve.url = resolveUrl;
|
|
|
|
resolve.ids = resolveIds;
|
|
|
|
|
|
|
|
|
2015-06-16 17:10:21 +03:00
|
|
|
function resolve(compile, root, ref) {
|
2015-06-07 23:55:40 +03:00
|
|
|
var refVal = this._refs[ref];
|
2015-06-12 20:16:54 +03:00
|
|
|
if (typeof refVal == 'string') {
|
|
|
|
if (this._refs[refVal]) refVal = this._refs[refVal];
|
2015-06-16 17:10:21 +03:00
|
|
|
else return resolve.call(this, compile, root, refVal);
|
2015-06-12 20:16:54 +03:00
|
|
|
}
|
2015-06-07 23:55:40 +03:00
|
|
|
if (typeof refVal == 'function') return refVal;
|
|
|
|
var refVal = this._schemas[ref];
|
|
|
|
if (typeof refVal == 'function') return refVal;
|
2015-06-16 20:14:24 +03:00
|
|
|
var currentRoot = util.copy(root);
|
2015-06-16 17:10:21 +03:00
|
|
|
var schema = _resolve.call(this, root, ref);
|
2015-06-16 20:14:24 +03:00
|
|
|
var v;
|
2015-06-24 02:55:38 +03:00
|
|
|
if (typeof schema == 'function') v = schema;
|
|
|
|
else if (schema) v = compile.call(this, schema, root);
|
|
|
|
if (v && ref[0] != '#') this._refs[ref] = v;
|
2015-06-16 20:14:24 +03:00
|
|
|
util.copy(currentRoot, root);
|
|
|
|
return v;
|
2015-06-06 18:18:52 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-06-16 17:10:21 +03:00
|
|
|
function _resolve(root, ref) {
|
2015-06-07 23:55:40 +03:00
|
|
|
var p = url.parse(ref, false, true)
|
|
|
|
, refPath = _getFullPath(p)
|
2015-06-16 17:10:21 +03:00
|
|
|
, baseId = getFullPath(root.schema.id);
|
2015-06-07 23:55:40 +03:00
|
|
|
if (refPath !== baseId) {
|
2015-06-18 22:48:35 +03:00
|
|
|
var id = normalizeId(refPath);
|
|
|
|
var refVal = this._refs[id];
|
2015-06-07 23:55:40 +03:00
|
|
|
if (typeof refVal == 'string') refVal = this._refs[refVal];
|
2015-06-16 20:14:24 +03:00
|
|
|
if (typeof refVal == 'function') util.copy(refVal, root);
|
2015-06-07 23:55:40 +03:00
|
|
|
else {
|
2015-06-16 19:10:04 +03:00
|
|
|
var refVal = this._schemas[id];
|
|
|
|
if (typeof refVal == 'function') {
|
|
|
|
if (id == normalizeId(ref)) return refVal;
|
2015-06-16 20:14:24 +03:00
|
|
|
util.copy(refVal, root);
|
2015-06-16 19:10:04 +03:00
|
|
|
}
|
2015-06-07 23:55:40 +03:00
|
|
|
}
|
2015-06-16 17:10:21 +03:00
|
|
|
if (!root.schema) return;
|
|
|
|
baseId = getFullPath(root.schema.id);
|
2015-06-07 23:55:40 +03:00
|
|
|
}
|
2015-06-17 00:19:00 +03:00
|
|
|
return getJsonPointer.call(this, p, baseId, root);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function getJsonPointer(parsedRef, baseId, root) {
|
|
|
|
parsedRef.hash = parsedRef.hash || '';
|
|
|
|
if (parsedRef.hash.slice(0,2) != '#/') return;
|
|
|
|
var parts = parsedRef.hash.split('/');
|
2015-06-16 17:10:21 +03:00
|
|
|
var schema = root.schema;
|
2015-06-07 23:55:40 +03:00
|
|
|
|
2015-06-06 18:18:52 +03:00
|
|
|
for (var i = 1; i < parts.length; i++) {
|
2015-06-07 14:46:53 +03:00
|
|
|
var part = parts[i];
|
|
|
|
if (part) {
|
|
|
|
part = unescapeFragment(part);
|
|
|
|
schema = schema[part];
|
2015-06-07 23:55:40 +03:00
|
|
|
if (!schema) break;
|
|
|
|
if (schema.id) baseId = resolveUrl(baseId, schema.id);
|
|
|
|
if (schema.$ref) {
|
|
|
|
var $ref = resolveUrl(baseId, schema.$ref);
|
2015-06-16 17:10:21 +03:00
|
|
|
schema = _resolve.call(this, root, $ref);
|
2015-06-07 23:55:40 +03:00
|
|
|
}
|
2015-06-07 14:46:53 +03:00
|
|
|
}
|
2015-06-05 01:08:45 +03:00
|
|
|
}
|
2015-06-16 17:10:21 +03:00
|
|
|
if (schema != root.schema) return schema;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-07 14:46:53 +03:00
|
|
|
function unescapeFragment(str) {
|
2015-06-05 01:08:45 +03:00
|
|
|
return decodeURIComponent(str)
|
|
|
|
.replace(/~1/g, '/')
|
|
|
|
.replace(/~0/g, '~');
|
2015-05-30 13:50:18 +03:00
|
|
|
}
|
2015-06-07 14:46:53 +03:00
|
|
|
|
|
|
|
|
|
|
|
function escapeFragment(str) {
|
|
|
|
var str = str.replace(/~/g, '~0').replace(/\//g, '~1');
|
|
|
|
return encodeURIComponent(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function getFullPath(id, normalize) {
|
|
|
|
if (normalize !== false) id = normalizeId(id);
|
|
|
|
var p = url.parse(id, false, true);
|
2015-06-07 23:55:40 +03:00
|
|
|
return _getFullPath(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function _getFullPath(p) {
|
|
|
|
return (p.protocol||'') + (p.protocol?'//':'') + (p.host||'') + (p.path||'') + '#';
|
2015-06-07 14:46:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-07 23:55:40 +03:00
|
|
|
var TRAILING_SLASH_HASH = /#\/?$/;
|
2015-06-07 14:46:53 +03:00
|
|
|
function normalizeId(id) {
|
|
|
|
return id ? id.replace(TRAILING_SLASH_HASH, '') : '';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function resolveUrl(baseId, id) {
|
|
|
|
id = normalizeId(id);
|
|
|
|
return url.resolve(baseId, id);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function resolveIds(schema) {
|
|
|
|
var id = normalizeId(schema.id);
|
2015-06-24 03:28:40 +03:00
|
|
|
var localRefs = {};
|
2015-06-07 14:46:53 +03:00
|
|
|
_resolveIds.call(this, schema, getFullPath(id, false), id);
|
2015-06-24 03:28:40 +03:00
|
|
|
return localRefs;
|
|
|
|
|
|
|
|
function _resolveIds(schema, fullPath, baseId) {
|
|
|
|
if (Array.isArray(schema))
|
|
|
|
for (var i=0; i<schema.length; i++)
|
|
|
|
_resolveIds.call(this, schema[i], fullPath+'/'+i, baseId);
|
|
|
|
else if (schema && typeof schema == 'object') {
|
|
|
|
if (typeof schema.id == 'string') {
|
|
|
|
var id = baseId = baseId
|
|
|
|
? url.resolve(baseId, schema.id)
|
|
|
|
: normalizeId(schema.id);
|
|
|
|
|
|
|
|
var refVal = this._refs[id];
|
|
|
|
if (typeof refVal == 'string') refVal = this._refs[refVal];
|
|
|
|
if (refVal && refVal.schema) {
|
|
|
|
if (!equal(schema, refVal.schema))
|
|
|
|
throw new Error('id "' + id + '" resolves to more than one schema');
|
|
|
|
} else if (id != normalizeId(fullPath)) {
|
|
|
|
if (id[0] == '#') {
|
|
|
|
if (localRefs[id] && !equal(schema, localRefs[id]))
|
|
|
|
throw new Error('id "' + id + '" resolves to more than one schema');
|
|
|
|
localRefs[id] = schema;
|
|
|
|
} else
|
|
|
|
this._refs[id] = fullPath;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (var key in schema)
|
|
|
|
_resolveIds.call(this, schema[key], fullPath+'/'+escapeFragment(key), baseId);
|
2015-06-07 14:46:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|