ajv/lib/compile/resolve.js

268 lines
7.6 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;
resolve.schema = resolveSchema;
2016-02-02 21:55:02 +03:00
/**
* [resolve and compile the references ($ref)]
2016-02-05 01:24:59 +03:00
* @this Ajv
* @param {Function} compile reference to schema compilation funciton (localCompile)
2016-02-02 21:55:02 +03:00
* @param {Object} root object with information about the root schema for the current schema
* @param {String} ref reference to resolve
* @return {Object|Function} schema object (if the schema can be inlined) or validation function
*/
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);
}
2016-02-26 23:25:15 +03:00
refVal = refVal || this._schemas[ref];
2016-02-02 21:55:02 +03:00
if (refVal instanceof SchemaObject) {
return inlineRef(refVal.schema, this._opts.inlineRefs)
2015-08-23 23:16:36 +03:00
? refVal.schema
: refVal.validate || this._compile(refVal);
2016-02-02 21:55:02 +03:00
}
var res = resolveSchema.call(this, root, ref);
var schema, v, baseId;
if (res) {
2015-08-11 21:24:31 +03:00
schema = res.schema;
root = res.root;
baseId = res.baseId;
}
2016-02-02 21:55:02 +03:00
if (schema instanceof SchemaObject) {
v = schema.validate || compile.call(this, schema.schema, root, undefined, baseId);
2016-02-02 21:55:02 +03:00
} else if (schema) {
v = inlineRef(schema, this._opts.inlineRefs)
2015-08-20 00:54:05 +03:00
? schema
: compile.call(this, schema, root, undefined, baseId);
2016-02-02 21:55:02 +03:00
}
return v;
2015-08-11 21:24:31 +03:00
}
2015-06-06 18:18:52 +03:00
/**
* Resolve schema, its root and baseId
* @this Ajv
* @param {Object} root root object with properties schema, refVal, refs
* @param {String} ref reference to resolve
* @return {Object} object with properties schema, root, baseId
*/
function resolveSchema(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];
if (typeof refVal == 'string') {
2015-10-25 03:49:07 +03:00
return resolveRecursive.call(this, root, refVal, p);
} else 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);
2015-10-25 03:49:07 +03:00
if (id == normalizeId(ref))
return { schema: refVal, root: root, baseId: baseId };
root = refVal;
} else {
return;
}
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
}
return getJsonPointer.call(this, p, baseId, root.schema, root);
2015-06-17 00:19:00 +03:00
}
2016-02-05 01:24:59 +03:00
/* @this Ajv */
2015-10-25 03:49:07 +03:00
function resolveRecursive(root, ref, parsedRef) {
/* jshint validthis: true */
var res = resolveSchema.call(this, root, ref);
2015-10-25 03:49:07 +03:00
if (res) {
var schema = res.schema;
var baseId = res.baseId;
root = res.root;
if (schema.id) baseId = resolveUrl(baseId, schema.id);
return getJsonPointer.call(this, parsedRef, baseId, schema, root);
}
}
var PREVENT_SCOPE_CHANGE = util.toHash(['properties', 'patternProperties', 'enum', 'dependencies', 'definitions']);
2016-02-05 01:24:59 +03:00
/* @this Ajv */
function getJsonPointer(parsedRef, baseId, schema, 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('/');
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) {
2015-12-19 13:52:39 +03:00
part = util.unescapeFragment(part);
schema = schema[part];
2015-06-07 23:55:40 +03:00
if (!schema) break;
if (schema.id && !PREVENT_SCOPE_CHANGE[part]) baseId = resolveUrl(baseId, schema.id);
2015-06-07 23:55:40 +03:00
if (schema.$ref) {
var $ref = resolveUrl(baseId, schema.$ref);
var res = resolveSchema.call(this, root, $ref);
if (res) {
schema = res.schema;
root = res.root;
baseId = res.baseId;
}
2015-06-07 23:55:40 +03:00
}
}
}
if (schema && schema != root.schema)
return { schema: schema, root: root, baseId: baseId };
}
var SIMPLE_INLINED = util.toHash([
2015-08-20 00:54:05 +03:00
'type', 'format', 'pattern',
'maxLength', 'minLength',
'maxProperties', 'minProperties',
'maxItems', 'minItems',
'maximum', 'minimum',
'uniqueItems', 'multipleOf',
2016-02-26 23:25:15 +03:00
'required', 'enum'
2015-08-20 00:54:05 +03:00
]);
2015-08-23 23:16:36 +03:00
function inlineRef(schema, limit) {
if (limit === false) return false;
if (limit === undefined || limit === true) return checkNoRef(schema);
2015-08-23 23:16:36 +03:00
else if (limit) return countKeys(schema) <= limit;
}
function checkNoRef(schema) {
var item;
if (Array.isArray(schema)) {
for (var i=0; i<schema.length; i++) {
item = schema[i];
if (typeof item == 'object' && !checkNoRef(item)) return false;
}
} else {
for (var key in schema) {
if (key == '$ref') return false;
2016-02-02 21:55:02 +03:00
item = schema[key];
if (typeof item == 'object' && !checkNoRef(item)) return false;
2015-08-23 23:16:36 +03:00
}
}
return true;
}
function countKeys(schema) {
var count = 0, item;
if (Array.isArray(schema)) {
for (var i=0; i<schema.length; i++) {
item = schema[i];
if (typeof item == 'object') count += countKeys(item);
2015-08-23 23:16:36 +03:00
if (count == Infinity) return Infinity;
}
} else {
for (var key in schema) {
if (key == '$ref') return Infinity;
2016-02-02 21:55:02 +03:00
if (SIMPLE_INLINED[key]) {
count++;
} else {
item = schema[key];
if (typeof item == 'object') count += countKeys(item) + 1;
if (count == Infinity) return Infinity;
2015-08-27 22:55:44 +03:00
}
}
2015-08-20 00:54:05 +03:00
}
return count;
2015-08-20 00:54:05 +03:00
}
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) {
var protocolSeparator = p.protocol || p.href.slice(0,2) == '//' ? '//' : '';
return (p.protocol||'') + protocolSeparator + (p.host||'') + (p.path||'') + '#';
}
2015-06-07 23:55:40 +03:00
var TRAILING_SLASH_HASH = /#\/?$/;
function normalizeId(id) {
2016-02-02 21:55:02 +03:00
return id ? id.replace(TRAILING_SLASH_HASH, '') : '';
}
function resolveUrl(baseId, id) {
id = normalizeId(id);
return url.resolve(baseId, id);
}
2016-02-05 01:24:59 +03:00
/* @this Ajv */
function resolveIds(schema) {
2016-02-05 01:24:59 +03:00
/* eslint no-shadow: 0 */
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;
2016-05-16 22:48:57 +03:00
/* @this Ajv */
2015-06-24 03:28:40 +03:00
function _resolveIds(schema, fullPath, baseId) {
2015-08-11 21:24:31 +03:00
/* jshint validthis: true */
2016-02-02 21:55:02 +03:00
if (Array.isArray(schema)) {
2015-06-24 03:28:40 +03:00
for (var i=0; i<schema.length; i++)
_resolveIds.call(this, schema[i], fullPath+'/'+i, baseId);
2016-02-02 21:55:02 +03:00
} else if (schema && typeof schema == 'object') {
2015-06-24 03:28:40 +03:00
if (typeof schema.id == 'string') {
var id = baseId = baseId
? url.resolve(baseId, schema.id)
2016-01-16 17:33:46 +03:00
: schema.id;
id = normalizeId(id);
2015-06-24 03:28:40 +03:00
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;
2016-02-02 21:55:02 +03:00
} else {
2015-06-24 03:28:40 +03:00
this._refs[id] = fullPath;
2016-02-02 21:55:02 +03:00
}
2015-06-24 03:28:40 +03:00
}
}
for (var key in schema)
2015-12-19 13:52:39 +03:00
_resolveIds.call(this, schema[key], fullPath+'/'+util.escapeFragment(key), baseId);
}
}
}