ajv/lib/compile/resolve.js

199 lines
5.5 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')
, SchemaObject = require('./schema_obj');
2015-05-30 13:50:18 +03:00
module.exports = resolve;
resolve.normalizeId = normalizeId;
resolve.fullPath = getFullPath;
resolve.url = resolveUrl;
resolve.ids = resolveIds;
2015-08-20 00:54:05 +03:00
resolve.inlineRef = inlineRef;
function resolve(compile, root, ref) {
2015-08-11 21:24:31 +03:00
/* jshint validthis: true */
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);
}
refVal = refVal || this._schemas[ref];
if (refVal instanceof SchemaObject)
return refVal.validate || this._compile(refVal, root);
var res = _resolve.call(this, root, ref);
2015-08-11 21:24:31 +03:00
var schema, v;
if (res) {
2015-08-11 21:24:31 +03:00
schema = res.schema;
root = res.root;
}
if (schema instanceof SchemaObject)
2015-08-20 00:54:05 +03:00
v = schema.validate || compile.call(this, schema.schema, root);
else if (schema)
2015-08-20 00:54:05 +03:00
v = this.opts.inlineRefs !== false && inlineRef(schema)
? schema
: compile.call(this, schema, root);
return v;
2015-08-11 21:24:31 +03:00
}
2015-06-06 18:18:52 +03:00
function _resolve(root, ref) {
2015-08-11 21:24:31 +03:00
/* jshint validthis: true */
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 (refVal instanceof SchemaObject) {
if (!refVal.validate) this._compile(refVal);
root = refVal;
} else {
refVal = this._schemas[id];
if (refVal instanceof SchemaObject) {
if (!refVal.validate) this._compile(refVal);
if (id == normalizeId(ref)) return { schema: refVal, root: root };
root = refVal;
}
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) {
2015-08-11 21:24:31 +03:00
/* jshint validthis: true */
2015-06-17 00:19:00 +03:00
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);
var res = _resolve.call(this, root, $ref);
if (res) {
schema = res.schema;
root = res.root;
}
2015-06-07 23:55:40 +03:00
}
}
}
if (schema && schema != root.schema) return { schema: schema, root: root };
}
2015-08-20 00:54:05 +03:00
// won't inline complex keywords and those that use schema reference at "validation-time"
var NOT_INLINED = util.toHash([
'id', '$ref', 'anyOf', 'oneOf', 'allOf', 'not',
'properties', 'patternProperties', 'additionalProperties',
'items', 'additionalItems', 'enum', 'dependencies', 'required'
]);
var LIMIT_INLINED = util.toHash([
'type', 'format', 'pattern',
'maxLength', 'minLength',
'maxProperties', 'minProperties',
'maxItems', 'minItems',
'maximum', 'minimum',
'uniqueItems', 'multipleOf'
]);
function inlineRef(schema) {
var count = 0;
for (var key in schema) {
if (NOT_INLINED[key]) return false;
if (LIMIT_INLINED[key]) count++;
}
return count <= 3;
}
function unescapeFragment(str) {
return decodeURIComponent(str)
.replace(/~1/g, '/')
.replace(/~0/g, '~');
2015-05-30 13:50:18 +03:00
}
function escapeFragment(str) {
2015-08-11 21:24:31 +03:00
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) {
2015-08-11 21:24:31 +03:00
/* jshint validthis: true */
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) {
2015-08-11 21:24:31 +03:00
/* jshint validthis: true */
2015-06-24 03:28:40 +03:00
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);
}
}
}