You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

270 lines
7.7 KiB

4 years ago
  1. 'use strict';
  2. var URI = require('uri-js')
  3. , equal = require('fast-deep-equal')
  4. , util = require('./util')
  5. , SchemaObject = require('./schema_obj')
  6. , traverse = require('json-schema-traverse');
  7. module.exports = resolve;
  8. resolve.normalizeId = normalizeId;
  9. resolve.fullPath = getFullPath;
  10. resolve.url = resolveUrl;
  11. resolve.ids = resolveIds;
  12. resolve.inlineRef = inlineRef;
  13. resolve.schema = resolveSchema;
  14. /**
  15. * [resolve and compile the references ($ref)]
  16. * @this Ajv
  17. * @param {Function} compile reference to schema compilation funciton (localCompile)
  18. * @param {Object} root object with information about the root schema for the current schema
  19. * @param {String} ref reference to resolve
  20. * @return {Object|Function} schema object (if the schema can be inlined) or validation function
  21. */
  22. function resolve(compile, root, ref) {
  23. /* jshint validthis: true */
  24. var refVal = this._refs[ref];
  25. if (typeof refVal == 'string') {
  26. if (this._refs[refVal]) refVal = this._refs[refVal];
  27. else return resolve.call(this, compile, root, refVal);
  28. }
  29. refVal = refVal || this._schemas[ref];
  30. if (refVal instanceof SchemaObject) {
  31. return inlineRef(refVal.schema, this._opts.inlineRefs)
  32. ? refVal.schema
  33. : refVal.validate || this._compile(refVal);
  34. }
  35. var res = resolveSchema.call(this, root, ref);
  36. var schema, v, baseId;
  37. if (res) {
  38. schema = res.schema;
  39. root = res.root;
  40. baseId = res.baseId;
  41. }
  42. if (schema instanceof SchemaObject) {
  43. v = schema.validate || compile.call(this, schema.schema, root, undefined, baseId);
  44. } else if (schema !== undefined) {
  45. v = inlineRef(schema, this._opts.inlineRefs)
  46. ? schema
  47. : compile.call(this, schema, root, undefined, baseId);
  48. }
  49. return v;
  50. }
  51. /**
  52. * Resolve schema, its root and baseId
  53. * @this Ajv
  54. * @param {Object} root root object with properties schema, refVal, refs
  55. * @param {String} ref reference to resolve
  56. * @return {Object} object with properties schema, root, baseId
  57. */
  58. function resolveSchema(root, ref) {
  59. /* jshint validthis: true */
  60. var p = URI.parse(ref)
  61. , refPath = _getFullPath(p)
  62. , baseId = getFullPath(this._getId(root.schema));
  63. if (Object.keys(root.schema).length === 0 || refPath !== baseId) {
  64. var id = normalizeId(refPath);
  65. var refVal = this._refs[id];
  66. if (typeof refVal == 'string') {
  67. return resolveRecursive.call(this, root, refVal, p);
  68. } else if (refVal instanceof SchemaObject) {
  69. if (!refVal.validate) this._compile(refVal);
  70. root = refVal;
  71. } else {
  72. refVal = this._schemas[id];
  73. if (refVal instanceof SchemaObject) {
  74. if (!refVal.validate) this._compile(refVal);
  75. if (id == normalizeId(ref))
  76. return { schema: refVal, root: root, baseId: baseId };
  77. root = refVal;
  78. } else {
  79. return;
  80. }
  81. }
  82. if (!root.schema) return;
  83. baseId = getFullPath(this._getId(root.schema));
  84. }
  85. return getJsonPointer.call(this, p, baseId, root.schema, root);
  86. }
  87. /* @this Ajv */
  88. function resolveRecursive(root, ref, parsedRef) {
  89. /* jshint validthis: true */
  90. var res = resolveSchema.call(this, root, ref);
  91. if (res) {
  92. var schema = res.schema;
  93. var baseId = res.baseId;
  94. root = res.root;
  95. var id = this._getId(schema);
  96. if (id) baseId = resolveUrl(baseId, id);
  97. return getJsonPointer.call(this, parsedRef, baseId, schema, root);
  98. }
  99. }
  100. var PREVENT_SCOPE_CHANGE = util.toHash(['properties', 'patternProperties', 'enum', 'dependencies', 'definitions']);
  101. /* @this Ajv */
  102. function getJsonPointer(parsedRef, baseId, schema, root) {
  103. /* jshint validthis: true */
  104. parsedRef.fragment = parsedRef.fragment || '';
  105. if (parsedRef.fragment.slice(0,1) != '/') return;
  106. var parts = parsedRef.fragment.split('/');
  107. for (var i = 1; i < parts.length; i++) {
  108. var part = parts[i];
  109. if (part) {
  110. part = util.unescapeFragment(part);
  111. schema = schema[part];
  112. if (schema === undefined) break;
  113. var id;
  114. if (!PREVENT_SCOPE_CHANGE[part]) {
  115. id = this._getId(schema);
  116. if (id) baseId = resolveUrl(baseId, id);
  117. if (schema.$ref) {
  118. var $ref = resolveUrl(baseId, schema.$ref);
  119. var res = resolveSchema.call(this, root, $ref);
  120. if (res) {
  121. schema = res.schema;
  122. root = res.root;
  123. baseId = res.baseId;
  124. }
  125. }
  126. }
  127. }
  128. }
  129. if (schema !== undefined && schema !== root.schema)
  130. return { schema: schema, root: root, baseId: baseId };
  131. }
  132. var SIMPLE_INLINED = util.toHash([
  133. 'type', 'format', 'pattern',
  134. 'maxLength', 'minLength',
  135. 'maxProperties', 'minProperties',
  136. 'maxItems', 'minItems',
  137. 'maximum', 'minimum',
  138. 'uniqueItems', 'multipleOf',
  139. 'required', 'enum'
  140. ]);
  141. function inlineRef(schema, limit) {
  142. if (limit === false) return false;
  143. if (limit === undefined || limit === true) return checkNoRef(schema);
  144. else if (limit) return countKeys(schema) <= limit;
  145. }
  146. function checkNoRef(schema) {
  147. var item;
  148. if (Array.isArray(schema)) {
  149. for (var i=0; i<schema.length; i++) {
  150. item = schema[i];
  151. if (typeof item == 'object' && !checkNoRef(item)) return false;
  152. }
  153. } else {
  154. for (var key in schema) {
  155. if (key == '$ref') return false;
  156. item = schema[key];
  157. if (typeof item == 'object' && !checkNoRef(item)) return false;
  158. }
  159. }
  160. return true;
  161. }
  162. function countKeys(schema) {
  163. var count = 0, item;
  164. if (Array.isArray(schema)) {
  165. for (var i=0; i<schema.length; i++) {
  166. item = schema[i];
  167. if (typeof item == 'object') count += countKeys(item);
  168. if (count == Infinity) return Infinity;
  169. }
  170. } else {
  171. for (var key in schema) {
  172. if (key == '$ref') return Infinity;
  173. if (SIMPLE_INLINED[key]) {
  174. count++;
  175. } else {
  176. item = schema[key];
  177. if (typeof item == 'object') count += countKeys(item) + 1;
  178. if (count == Infinity) return Infinity;
  179. }
  180. }
  181. }
  182. return count;
  183. }
  184. function getFullPath(id, normalize) {
  185. if (normalize !== false) id = normalizeId(id);
  186. var p = URI.parse(id);
  187. return _getFullPath(p);
  188. }
  189. function _getFullPath(p) {
  190. return URI.serialize(p).split('#')[0] + '#';
  191. }
  192. var TRAILING_SLASH_HASH = /#\/?$/;
  193. function normalizeId(id) {
  194. return id ? id.replace(TRAILING_SLASH_HASH, '') : '';
  195. }
  196. function resolveUrl(baseId, id) {
  197. id = normalizeId(id);
  198. return URI.resolve(baseId, id);
  199. }
  200. /* @this Ajv */
  201. function resolveIds(schema) {
  202. var schemaId = normalizeId(this._getId(schema));
  203. var baseIds = {'': schemaId};
  204. var fullPaths = {'': getFullPath(schemaId, false)};
  205. var localRefs = {};
  206. var self = this;
  207. traverse(schema, {allKeys: true}, function(sch, jsonPtr, rootSchema, parentJsonPtr, parentKeyword, parentSchema, keyIndex) {
  208. if (jsonPtr === '') return;
  209. var id = self._getId(sch);
  210. var baseId = baseIds[parentJsonPtr];
  211. var fullPath = fullPaths[parentJsonPtr] + '/' + parentKeyword;
  212. if (keyIndex !== undefined)
  213. fullPath += '/' + (typeof keyIndex == 'number' ? keyIndex : util.escapeFragment(keyIndex));
  214. if (typeof id == 'string') {
  215. id = baseId = normalizeId(baseId ? URI.resolve(baseId, id) : id);
  216. var refVal = self._refs[id];
  217. if (typeof refVal == 'string') refVal = self._refs[refVal];
  218. if (refVal && refVal.schema) {
  219. if (!equal(sch, refVal.schema))
  220. throw new Error('id "' + id + '" resolves to more than one schema');
  221. } else if (id != normalizeId(fullPath)) {
  222. if (id[0] == '#') {
  223. if (localRefs[id] && !equal(sch, localRefs[id]))
  224. throw new Error('id "' + id + '" resolves to more than one schema');
  225. localRefs[id] = sch;
  226. } else {
  227. self._refs[id] = fullPath;
  228. }
  229. }
  230. }
  231. baseIds[jsonPtr] = baseId;
  232. fullPaths[jsonPtr] = fullPath;
  233. });
  234. return localRefs;
  235. }