dereference.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.initialBaseURI = exports.ignoredKeyword = exports.schemaMapKeyword = exports.schemaArrayKeyword = exports.schemaKeyword = void 0;
  4. exports.dereference = dereference;
  5. const pointer_js_1 = require("./pointer.js");
  6. exports.schemaKeyword = {
  7. additionalItems: true,
  8. unevaluatedItems: true,
  9. items: true,
  10. contains: true,
  11. additionalProperties: true,
  12. unevaluatedProperties: true,
  13. propertyNames: true,
  14. not: true,
  15. if: true,
  16. then: true,
  17. else: true
  18. };
  19. exports.schemaArrayKeyword = {
  20. prefixItems: true,
  21. items: true,
  22. allOf: true,
  23. anyOf: true,
  24. oneOf: true
  25. };
  26. exports.schemaMapKeyword = {
  27. $defs: true,
  28. definitions: true,
  29. properties: true,
  30. patternProperties: true,
  31. dependentSchemas: true
  32. };
  33. exports.ignoredKeyword = {
  34. id: true,
  35. $id: true,
  36. $ref: true,
  37. $schema: true,
  38. $anchor: true,
  39. $vocabulary: true,
  40. $comment: true,
  41. default: true,
  42. enum: true,
  43. const: true,
  44. required: true,
  45. type: true,
  46. maximum: true,
  47. minimum: true,
  48. exclusiveMaximum: true,
  49. exclusiveMinimum: true,
  50. multipleOf: true,
  51. maxLength: true,
  52. minLength: true,
  53. pattern: true,
  54. format: true,
  55. maxItems: true,
  56. minItems: true,
  57. uniqueItems: true,
  58. maxProperties: true,
  59. minProperties: true
  60. };
  61. exports.initialBaseURI = typeof self !== 'undefined' &&
  62. self.location &&
  63. self.location.origin !== 'null'
  64. ?
  65. new URL(self.location.origin + self.location.pathname + location.search)
  66. : new URL('https://github.com/cfworker');
  67. function dereference(schema, lookup = Object.create(null), baseURI = exports.initialBaseURI, basePointer = '') {
  68. if (schema && typeof schema === 'object' && !Array.isArray(schema)) {
  69. const id = schema.$id || schema.id;
  70. if (id) {
  71. const url = new URL(id, baseURI.href);
  72. if (url.hash.length > 1) {
  73. lookup[url.href] = schema;
  74. }
  75. else {
  76. url.hash = '';
  77. if (basePointer === '') {
  78. baseURI = url;
  79. }
  80. else {
  81. dereference(schema, lookup, baseURI);
  82. }
  83. }
  84. }
  85. }
  86. else if (schema !== true && schema !== false) {
  87. return lookup;
  88. }
  89. const schemaURI = baseURI.href + (basePointer ? '#' + basePointer : '');
  90. if (lookup[schemaURI] !== undefined) {
  91. throw new Error(`Duplicate schema URI "${schemaURI}".`);
  92. }
  93. lookup[schemaURI] = schema;
  94. if (schema === true || schema === false) {
  95. return lookup;
  96. }
  97. if (schema.__absolute_uri__ === undefined) {
  98. Object.defineProperty(schema, '__absolute_uri__', {
  99. enumerable: false,
  100. value: schemaURI
  101. });
  102. }
  103. if (schema.$ref && schema.__absolute_ref__ === undefined) {
  104. const url = new URL(schema.$ref, baseURI.href);
  105. url.hash = url.hash;
  106. Object.defineProperty(schema, '__absolute_ref__', {
  107. enumerable: false,
  108. value: url.href
  109. });
  110. }
  111. if (schema.$recursiveRef && schema.__absolute_recursive_ref__ === undefined) {
  112. const url = new URL(schema.$recursiveRef, baseURI.href);
  113. url.hash = url.hash;
  114. Object.defineProperty(schema, '__absolute_recursive_ref__', {
  115. enumerable: false,
  116. value: url.href
  117. });
  118. }
  119. if (schema.$anchor) {
  120. const url = new URL('#' + schema.$anchor, baseURI.href);
  121. lookup[url.href] = schema;
  122. }
  123. for (let key in schema) {
  124. if (exports.ignoredKeyword[key]) {
  125. continue;
  126. }
  127. const keyBase = `${basePointer}/${(0, pointer_js_1.encodePointer)(key)}`;
  128. const subSchema = schema[key];
  129. if (Array.isArray(subSchema)) {
  130. if (exports.schemaArrayKeyword[key]) {
  131. const length = subSchema.length;
  132. for (let i = 0; i < length; i++) {
  133. dereference(subSchema[i], lookup, baseURI, `${keyBase}/${i}`);
  134. }
  135. }
  136. }
  137. else if (exports.schemaMapKeyword[key]) {
  138. for (let subKey in subSchema) {
  139. dereference(subSchema[subKey], lookup, baseURI, `${keyBase}/${(0, pointer_js_1.encodePointer)(subKey)}`);
  140. }
  141. }
  142. else {
  143. dereference(subSchema, lookup, baseURI, keyBase);
  144. }
  145. }
  146. return lookup;
  147. }