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