1 | "use strict";
|
---|
2 |
|
---|
3 | var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault").default;
|
---|
4 | exports.__esModule = true;
|
---|
5 | exports.default = void 0;
|
---|
6 | var _apidomCore = require("@swagger-api/apidom-core");
|
---|
7 | var _JSONSchema = _interopRequireDefault(require("../../elements/JSONSchema.cjs"));
|
---|
8 | var _LinkDescription = _interopRequireDefault(require("../../elements/LinkDescription.cjs"));
|
---|
9 | var _Media = _interopRequireDefault(require("../../elements/Media.cjs"));
|
---|
10 | var _visitor = require("../../traversal/visitor.cjs");
|
---|
11 | /**
|
---|
12 | * JSON Schema Draft 4 specification elements.
|
---|
13 | */
|
---|
14 |
|
---|
15 | /**
|
---|
16 | * This plugin is specific to YAML 1.2 format, which allows defining key-value pairs
|
---|
17 | * with empty key, empty value, or both. If the value is not provided in YAML format,
|
---|
18 | * this plugin compensates for this missing value with the most appropriate semantic element type.
|
---|
19 | *
|
---|
20 | * https://yaml.org/spec/1.2.2/#72-empty-nodes
|
---|
21 | *
|
---|
22 | * @example
|
---|
23 | *
|
---|
24 | * ```yaml
|
---|
25 | * $schema: http://json-schema.org/draft-04/schema#
|
---|
26 | * items:
|
---|
27 | * ```
|
---|
28 | * Refracting result without this plugin:
|
---|
29 | *
|
---|
30 | * (JSONSchemaElement
|
---|
31 | * (MemberElement
|
---|
32 | * (StringElement)
|
---|
33 | * (StringElement))
|
---|
34 | * (MemberElement
|
---|
35 | * (StringElement)
|
---|
36 | * (StringElement))
|
---|
37 | *
|
---|
38 | * Refracting result with this plugin:
|
---|
39 | *
|
---|
40 | * (JSONSchemaElement
|
---|
41 | * (MemberElement
|
---|
42 | * (StringElement)
|
---|
43 | * (StringElement))
|
---|
44 | * (MemberElement
|
---|
45 | * (StringElement)
|
---|
46 | * (JSONSchemaElement))
|
---|
47 | */
|
---|
48 |
|
---|
49 | const isEmptyElement = element => (0, _apidomCore.isStringElement)(element) && (0, _apidomCore.includesClasses)(['yaml-e-node', 'yaml-e-scalar'], element);
|
---|
50 | const schema = {
|
---|
51 | JSONSchemaDraft4Element: {
|
---|
52 | additionalItems(...args) {
|
---|
53 | return new _JSONSchema.default(...args);
|
---|
54 | },
|
---|
55 | items(...args) {
|
---|
56 | return new _JSONSchema.default(...args);
|
---|
57 | },
|
---|
58 | required(...args) {
|
---|
59 | const element = new _apidomCore.ArrayElement(...args);
|
---|
60 | element.classes.push('json-schema-required');
|
---|
61 | return element;
|
---|
62 | },
|
---|
63 | properties(...args) {
|
---|
64 | const element = new _apidomCore.ObjectElement(...args);
|
---|
65 | element.classes.push('json-schema-properties');
|
---|
66 | return element;
|
---|
67 | },
|
---|
68 | additionalProperties(...args) {
|
---|
69 | return new _JSONSchema.default(...args);
|
---|
70 | },
|
---|
71 | patternProperties(...args) {
|
---|
72 | const element = new _apidomCore.ObjectElement(...args);
|
---|
73 | element.classes.push('json-schema-patternProperties');
|
---|
74 | return element;
|
---|
75 | },
|
---|
76 | dependencies(...args) {
|
---|
77 | const element = new _apidomCore.ObjectElement(...args);
|
---|
78 | element.classes.push('json-schema-dependencies');
|
---|
79 | return element;
|
---|
80 | },
|
---|
81 | enum(...args) {
|
---|
82 | const element = new _apidomCore.ArrayElement(...args);
|
---|
83 | element.classes.push('json-schema-enum');
|
---|
84 | return element;
|
---|
85 | },
|
---|
86 | allOf(...args) {
|
---|
87 | const element = new _apidomCore.ArrayElement(...args);
|
---|
88 | element.classes.push('json-schema-allOf');
|
---|
89 | return element;
|
---|
90 | },
|
---|
91 | anyOf(...args) {
|
---|
92 | const element = new _apidomCore.ArrayElement(...args);
|
---|
93 | element.classes.push('json-schema-anyOf');
|
---|
94 | return element;
|
---|
95 | },
|
---|
96 | oneOf(...args) {
|
---|
97 | const element = new _apidomCore.ArrayElement(...args);
|
---|
98 | element.classes.push('json-schema-oneOf');
|
---|
99 | return element;
|
---|
100 | },
|
---|
101 | not(...args) {
|
---|
102 | return new _JSONSchema.default(...args);
|
---|
103 | },
|
---|
104 | definitions(...args) {
|
---|
105 | const element = new _apidomCore.ObjectElement(...args);
|
---|
106 | element.classes.push('json-schema-definitions');
|
---|
107 | return element;
|
---|
108 | },
|
---|
109 | links(...args) {
|
---|
110 | const element = new _apidomCore.ArrayElement(...args);
|
---|
111 | element.classes.push('json-schema-links');
|
---|
112 | return element;
|
---|
113 | },
|
---|
114 | media(...args) {
|
---|
115 | return new _Media.default(...args);
|
---|
116 | }
|
---|
117 | },
|
---|
118 | LinkDescriptionElement: {
|
---|
119 | targetSchema(...args) {
|
---|
120 | return new _JSONSchema.default(...args);
|
---|
121 | },
|
---|
122 | schema(...args) {
|
---|
123 | return new _JSONSchema.default(...args);
|
---|
124 | }
|
---|
125 | },
|
---|
126 | 'json-schema-properties': {
|
---|
127 | '[key: *]': function key(...args) {
|
---|
128 | return new _JSONSchema.default(...args);
|
---|
129 | }
|
---|
130 | },
|
---|
131 | 'json-schema-patternProperties': {
|
---|
132 | '[key: *]': function key(...args) {
|
---|
133 | return new _JSONSchema.default(...args);
|
---|
134 | }
|
---|
135 | },
|
---|
136 | 'json-schema-dependencies': {
|
---|
137 | '[key: *]': function key(...args) {
|
---|
138 | return new _JSONSchema.default(...args);
|
---|
139 | }
|
---|
140 | },
|
---|
141 | 'json-schema-allOf': {
|
---|
142 | '<*>': function asterisk(...args) {
|
---|
143 | return new _JSONSchema.default(...args);
|
---|
144 | }
|
---|
145 | },
|
---|
146 | 'json-schema-anyOf': {
|
---|
147 | '<*>': function asterisk(...args) {
|
---|
148 | return new _JSONSchema.default(...args);
|
---|
149 | }
|
---|
150 | },
|
---|
151 | 'json-schema-oneOf': {
|
---|
152 | '<*>': function asterisk(...args) {
|
---|
153 | return new _JSONSchema.default(...args);
|
---|
154 | }
|
---|
155 | },
|
---|
156 | 'json-schema-definitions': {
|
---|
157 | '[key: *]': function key(...args) {
|
---|
158 | return new _JSONSchema.default(...args);
|
---|
159 | }
|
---|
160 | },
|
---|
161 | 'json-schema-links': {
|
---|
162 | '<*>': function asterisk(...args) {
|
---|
163 | return new _LinkDescription.default(...args);
|
---|
164 | }
|
---|
165 | }
|
---|
166 | };
|
---|
167 | const findElementFactory = (ancestor, keyName) => {
|
---|
168 | const elementType = (0, _visitor.getNodeType)(ancestor); // @ts-ignore
|
---|
169 | const keyMapping = schema[elementType] || schema[(0, _apidomCore.toValue)(ancestor.classes.first)];
|
---|
170 | return typeof keyMapping === 'undefined' ? undefined : Object.prototype.hasOwnProperty.call(keyMapping, '[key: *]') ? keyMapping['[key: *]'] : keyMapping[keyName];
|
---|
171 | };
|
---|
172 | const plugin = () => () => ({
|
---|
173 | visitor: {
|
---|
174 | StringElement(element, key, parent, path, ancestors) {
|
---|
175 | if (!isEmptyElement(element)) return undefined;
|
---|
176 | const lineage = [...ancestors, parent].filter(_apidomCore.isElement);
|
---|
177 | const parentElement = lineage[lineage.length - 1]; // @TODO(vladimir.gorej@gmail.com): can be replaced by Array.prototype.at in future
|
---|
178 | let elementFactory;
|
---|
179 | let context;
|
---|
180 | if ((0, _apidomCore.isArrayElement)(parentElement)) {
|
---|
181 | context = element;
|
---|
182 | elementFactory = findElementFactory(parentElement, '<*>');
|
---|
183 | } else if ((0, _apidomCore.isMemberElement)(parentElement)) {
|
---|
184 | context = lineage[lineage.length - 2]; // @TODO(vladimir.gorej@gmail.com): can be replaced by Array.prototype.at in future
|
---|
185 | elementFactory = findElementFactory(context, (0, _apidomCore.toValue)(parentElement.key));
|
---|
186 | }
|
---|
187 |
|
---|
188 | // no element factory found
|
---|
189 | if (typeof elementFactory !== 'function') return undefined;
|
---|
190 | return elementFactory.call({
|
---|
191 | context
|
---|
192 | }, undefined, (0, _apidomCore.cloneDeep)(element.meta), (0, _apidomCore.cloneDeep)(element.attributes));
|
---|
193 | }
|
---|
194 | }
|
---|
195 | });
|
---|
196 | var _default = exports.default = plugin; |
---|