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