UNPKG

3.91 kBJavaScriptView Raw
1"use strict";
2
3exports.__esModule = true;
4exports.default = void 0;
5
6var _helpers = require("../helpers");
7
8function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
9
10function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
11
12function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
13
14var _default = {
15 key: 'allOf',
16 plugin: (val, key, fullPath, specmap, patch) => {
17 // Ignore replace patches created by $ref because the changes will
18 // occur in the original "add" patch and we don't want this plugin
19 // to redundantly processes those "relace" patches.
20 if (patch.meta && patch.meta.$$ref) {
21 return undefined;
22 }
23
24 const parent = fullPath.slice(0, -1);
25
26 if ((0, _helpers.isFreelyNamed)(parent)) {
27 return undefined;
28 }
29
30 if (!Array.isArray(val)) {
31 const err = new TypeError('allOf must be an array');
32 err.fullPath = fullPath; // This is an array
33
34 return err;
35 }
36
37 let alreadyAddError = false; // Find the original definition from the `patch.value` object
38 // Remove the `allOf` property so it doesn't get added to the result of the `allOf` plugin
39
40 let originalDefinitionObj = patch.value;
41 parent.forEach(part => {
42 if (!originalDefinitionObj) return; // bail out if we've lost sight of our target
43
44 originalDefinitionObj = originalDefinitionObj[part];
45 });
46 originalDefinitionObj = _objectSpread({}, originalDefinitionObj);
47 delete originalDefinitionObj.allOf;
48 const patches = []; // remove existing content
49
50 patches.push(specmap.replace(parent, {}));
51 val.forEach((toMerge, i) => {
52 if (!specmap.isObject(toMerge)) {
53 if (alreadyAddError) {
54 return null;
55 }
56
57 alreadyAddError = true;
58 const err = new TypeError('Elements in allOf must be objects');
59 err.fullPath = fullPath; // This is an array
60
61 return patches.push(err);
62 } // Deeply merge the member's contents onto the parent location
63
64
65 patches.push(specmap.mergeDeep(parent, toMerge)); // Generate patches that migrate $ref values based on ContextTree information
66 // remove ["allOf"], which will not be present when these patches are applied
67
68 const collapsedFullPath = fullPath.slice(0, -1);
69 const absoluteRefPatches = (0, _helpers.generateAbsoluteRefPatches)(toMerge, collapsedFullPath, {
70 getBaseUrlForNodePath: nodePath => {
71 return specmap.getContext([...fullPath, i, ...nodePath]).baseDoc;
72 },
73 specmap
74 });
75 patches.push(...absoluteRefPatches);
76 return undefined;
77 }); // Merge back the values from the original definition
78
79 patches.push(specmap.mergeDeep(parent, originalDefinitionObj)); // If there was not an original $$ref value, make sure to remove
80 // any $$ref value that may exist from the result of `allOf` merges
81
82 if (!originalDefinitionObj.$$ref) {
83 patches.push(specmap.remove([].concat(parent, '$$ref')));
84 }
85
86 return patches;
87 }
88};
89exports.default = _default;
\No newline at end of file