ajv/lib/compile/resolve.js

153 lines
4.2 KiB
JavaScript
Raw Normal View History

2015-05-30 13:50:18 +03:00
'use strict';
var url = require('url')
, equal = require('./equal')
, util = require('./util');
2015-05-30 13:50:18 +03:00
module.exports = resolve;
resolve.normalizeId = normalizeId;
resolve.fullPath = getFullPath;
resolve.url = resolveUrl;
resolve.ids = resolveIds;
function resolve(compile, root, ref) {
2015-06-07 23:55:40 +03:00
var refVal = this._refs[ref];
if (typeof refVal == 'string') {
if (this._refs[refVal]) refVal = this._refs[refVal];
else return resolve.call(this, compile, root, refVal);
}
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;
var currentRoot = util.copy(root);
var schema = _resolve.call(this, root, ref);
var v;
if (typeof schema == 'function') v = schema;
else if (schema) v = compile.call(this, schema, root);
if (v && ref[0] != '#') this._refs[ref] = v;
util.copy(currentRoot, root);
return v;
2015-06-06 18:18:52 +03:00
};
function _resolve(root, ref) {
2015-06-07 23:55:40 +03:00
var p = url.parse(ref, false, true)
, refPath = _getFullPath(p)
, baseId = getFullPath(root.schema.id);
2015-06-07 23:55:40 +03:00
if (refPath !== baseId) {
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];
if (typeof refVal == 'function') util.copy(refVal, root);
2015-06-07 23:55:40 +03:00
else {
var refVal = this._schemas[id];
if (typeof refVal == 'function') {
if (id == normalizeId(ref)) return refVal;
util.copy(refVal, root);
}
2015-06-07 23:55:40 +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('/');
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++) {
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);
schema = _resolve.call(this, root, $ref);
2015-06-07 23:55:40 +03:00
}
}
}
if (schema != root.schema) return schema;
}
function unescapeFragment(str) {
return decodeURIComponent(str)
.replace(/~1/g, '/')
.replace(/~0/g, '~');
2015-05-30 13:50:18 +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 23:55:40 +03:00
var TRAILING_SLASH_HASH = /#\/?$/;
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 = {};
_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);
}
}
}