options.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* eslint lines-around-comment: [2, {beforeBlockComment: false}] */
  2. "use strict";
  3. const jsonParser = require("./parsers/json");
  4. const yamlParser = require("./parsers/yaml");
  5. const textParser = require("./parsers/text");
  6. const binaryParser = require("./parsers/binary");
  7. const fileResolver = require("./resolvers/file");
  8. const httpResolver = require("./resolvers/http");
  9. module.exports = $RefParserOptions;
  10. /**
  11. * Options that determine how JSON schemas are parsed, resolved, and dereferenced.
  12. *
  13. * @param {object|$RefParserOptions} [options] - Overridden options
  14. * @constructor
  15. */
  16. function $RefParserOptions (options) {
  17. merge(this, $RefParserOptions.defaults);
  18. merge(this, options);
  19. }
  20. $RefParserOptions.defaults = {
  21. /**
  22. * Determines how different types of files will be parsed.
  23. *
  24. * You can add additional parsers of your own, replace an existing one with
  25. * your own implementation, or disable any parser by setting it to false.
  26. */
  27. parse: {
  28. json: jsonParser,
  29. yaml: yamlParser,
  30. text: textParser,
  31. binary: binaryParser,
  32. },
  33. /**
  34. * Determines how JSON References will be resolved.
  35. *
  36. * You can add additional resolvers of your own, replace an existing one with
  37. * your own implementation, or disable any resolver by setting it to false.
  38. */
  39. resolve: {
  40. file: fileResolver,
  41. http: httpResolver,
  42. /**
  43. * Determines whether external $ref pointers will be resolved.
  44. * If this option is disabled, then none of above resolvers will be called.
  45. * Instead, external $ref pointers will simply be ignored.
  46. *
  47. * @type {boolean}
  48. */
  49. external: true,
  50. },
  51. /**
  52. * By default, JSON Schema $Ref Parser throws the first error it encounters. Setting `continueOnError` to `true`
  53. * causes it to keep processing as much as possible and then throw a single error that contains all errors
  54. * that were encountered.
  55. */
  56. continueOnError: false,
  57. /**
  58. * Determines the types of JSON references that are allowed.
  59. */
  60. dereference: {
  61. /**
  62. * Dereference circular (recursive) JSON references?
  63. * If false, then a {@link ReferenceError} will be thrown if a circular reference is found.
  64. * If "ignore", then circular references will not be dereferenced.
  65. *
  66. * @type {boolean|string}
  67. */
  68. circular: true,
  69. /**
  70. * A function, called for each path, which can return true to stop this path and all
  71. * subpaths from being dereferenced further. This is useful in schemas where some
  72. * subpaths contain literal $ref keys that should not be dereferenced.
  73. *
  74. * @type {function}
  75. */
  76. excludedPathMatcher: () => false
  77. },
  78. };
  79. /**
  80. * Merges the properties of the source object into the target object.
  81. *
  82. * @param {object} target - The object that we're populating
  83. * @param {?object} source - The options that are being merged
  84. * @returns {object}
  85. */
  86. function merge (target, source) {
  87. if (isMergeable(source)) {
  88. let keys = Object.keys(source);
  89. for (let i = 0; i < keys.length; i++) {
  90. let key = keys[i];
  91. let sourceSetting = source[key];
  92. let targetSetting = target[key];
  93. if (isMergeable(sourceSetting)) {
  94. // It's a nested object, so merge it recursively
  95. target[key] = merge(targetSetting || {}, sourceSetting);
  96. }
  97. else if (sourceSetting !== undefined) {
  98. // It's a scalar value, function, or array. No merging necessary. Just overwrite the target value.
  99. target[key] = sourceSetting;
  100. }
  101. }
  102. }
  103. return target;
  104. }
  105. /**
  106. * Determines whether the given value can be merged,
  107. * or if it is a scalar value that should just override the target value.
  108. *
  109. * @param {*} val
  110. * @returns {Boolean}
  111. */
  112. function isMergeable (val) {
  113. return val &&
  114. (typeof val === "object") &&
  115. !Array.isArray(val) &&
  116. !(val instanceof RegExp) &&
  117. !(val instanceof Date);
  118. }