|
| 1 | +# @swagger-api/apidom-ns-json-schema-2020-12 |
| 2 | + |
| 3 | +`@swagger-api/apidom-ns-json-schema-2020-12` contains ApiDOM namespace specific to [JSON Schema 2020-12](https://json-schema.org/draft/2020-12/draft-bhutton-json-schema-01) specification. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +You can install this package via [npm CLI](https://docs.npmjs.com/cli) by running the following command: |
| 8 | + |
| 9 | +```sh |
| 10 | + $ npm install @swagger-api/apidom-ns-json-schema-2020-12 |
| 11 | +``` |
| 12 | + |
| 13 | +## JSON Schema 2020-12 namespace |
| 14 | + |
| 15 | +JSON Schema 2020-12 namespace consists of [number of elements](https://github.com/swagger-api/apidom/tree/main/packages/apidom-ns-json-schema-2020-12/src/elements) implemented on top |
| 16 | +of [primitive ones](https://github.com/refractproject/minim/tree/master/lib/primitives). |
| 17 | + |
| 18 | +```js |
| 19 | +import { createNamespace } from '@swagger-api/apidom-core'; |
| 20 | +import jsonShema202012Namespace from '@swagger-api/apidom-ns-json-schema-2020-12'; |
| 21 | + |
| 22 | +const namespace = createNamespace(jsonShema202012Namespace); |
| 23 | + |
| 24 | +const objectElement = new namespace.elements.Object(); |
| 25 | +const jsonSchemaElement = new namespace.elements.JSONSchema202012(); |
| 26 | +``` |
| 27 | + |
| 28 | +When namespace instance is created in this way, it will extend the base namespace |
| 29 | +with the namespace provided as an argument. |
| 30 | + |
| 31 | +Elements from the namespace can also be used directly by importing them. |
| 32 | + |
| 33 | +```js |
| 34 | +import { JSONSchemaElement, LinkDescriptionElement } from '@swagger-api/apidom-ns-json-schema-2020-12'; |
| 35 | + |
| 36 | +const jsonSchemaElement = new JSONSchemaElement(); |
| 37 | +const linkDescriptionElement = new LinkDescriptionElement(); |
| 38 | +``` |
| 39 | + |
| 40 | +## Predicates |
| 41 | + |
| 42 | +This package exposes [predicates](https://github.com/swagger-api/apidom/blob/main/packages/apidom-ns-json-schema-2020-12/src/predicates.ts) |
| 43 | +for all higher order elements that are part of this namespace. |
| 44 | + |
| 45 | +```js |
| 46 | +import { isJSONSchemaElement, JSONSchemaElement } from '@swagger-api/apidom-ns-json-schema-2020-12'; |
| 47 | + |
| 48 | +const jsonSchemaElement = new JSONSchemaElement(); |
| 49 | + |
| 50 | +isJSONSchemaElement(jsonSchemaElement); // => true |
| 51 | +``` |
| 52 | + |
| 53 | +## Traversal |
| 54 | + |
| 55 | +Traversing ApiDOM in this namespace is possible by using `visit` function from `apidom` package. |
| 56 | +This package comes with its own [keyMap](https://github.com/swagger-api/apidom/blob/main/packages/apidom-ns-json-schema-2020-12/src/traversal/visitor.ts#L11) and [nodeTypeGetter](https://github.com/swagger-api/apidom/blob/main/packages/apidom-ns-json-schema-2020-12/src/traversal/visitor.ts#L4). |
| 57 | +To learn more about these `visit` configuration options please refer to [@swagger-api/apidom-ast documentation](https://github.com/swagger-api/apidom/blob/main/packages/apidom-ast/README.md#visit). |
| 58 | + |
| 59 | +```js |
| 60 | +import { visit } from '@swagger-api/apidom-core'; |
| 61 | +import { JSONSchemaElement, keyMap, getNodeType } from '@swagger-api/apidom-ns-json-schema-2020-12'; |
| 62 | + |
| 63 | +const element = new JSONSchemaElement(); |
| 64 | + |
| 65 | +const visitor = { |
| 66 | + JSONSchema202012Element(jsonSchemaElement) { |
| 67 | + console.dir(jsonSchemaElement); |
| 68 | + }, |
| 69 | +}; |
| 70 | + |
| 71 | +visit(element, visitor, { keyMap, nodeTypeGetter: getNodeType }); |
| 72 | +``` |
| 73 | + |
| 74 | +## Refractors |
| 75 | + |
| 76 | +Refractor is a special layer inside the namespace that can transform either JavaScript structures |
| 77 | +or generic ApiDOM structures into structures built from elements of this namespace. |
| 78 | + |
| 79 | +**Refracting JavaScript structures**: |
| 80 | + |
| 81 | +```js |
| 82 | +import { LinkDescriptionElement } from '@swagger-api/apidom-ns-json-schema-2020-12'; |
| 83 | + |
| 84 | +const object = { |
| 85 | + anchor: 'nodes/{thisNodeId}', |
| 86 | + anchorPointer: '#/relative/json/pointer', |
| 87 | +}; |
| 88 | + |
| 89 | +LinkDescriptionElement.refract(object); // => LinkDescriptionElement({ anchor, anchorPointer }) |
| 90 | +``` |
| 91 | + |
| 92 | +**Refracting generic ApiDOM structures**: |
| 93 | + |
| 94 | +```js |
| 95 | +import { ObjectElement } from '@swagger-api/apidom-core'; |
| 96 | +import { LinkDescriptionElement } from '@swagger-api/apidom-ns-json-schema-2020-12'; |
| 97 | + |
| 98 | +const objectElement = new ObjectElement({ |
| 99 | + anchor: 'nodes/{thisNodeId}', |
| 100 | + anchorPointer: '#/relative/json/pointer', |
| 101 | +}); |
| 102 | + |
| 103 | +LinkDescriptionElement.refract(objectElement); // => LinkDescriptionElement({ anchor = 'nodes/{thisNodeId}', anchorPointer = '#/relative/json/pointer' }) |
| 104 | +``` |
| 105 | + |
| 106 | +### Refractor plugins |
| 107 | + |
| 108 | +Refractors can accept plugins as a second argument of refract static method. |
| 109 | + |
| 110 | +```js |
| 111 | +import { ObjectElement } from '@swagger-api/apidom-core'; |
| 112 | +import { LinkDescriptionElement } from '@swagger-api/apidom-ns-json-schema-2020-12'; |
| 113 | + |
| 114 | +const objectElement = new ObjectElement({ |
| 115 | + anchor: 'nodes/{thisNodeId}', |
| 116 | + anchorPointer: '#/relative/json/pointer', |
| 117 | +}); |
| 118 | + |
| 119 | +const plugin = ({ predicates, namespace }) => ({ |
| 120 | + name: 'plugin', |
| 121 | + pre() { |
| 122 | + console.dir('runs before traversal'); |
| 123 | + }, |
| 124 | + visitor: { |
| 125 | + LinkDescriptionElement(linkDescriptionElement) { |
| 126 | + linkDescriptionElement.anchorPointer = '#/relative/json/pointer/x'; |
| 127 | + }, |
| 128 | + }, |
| 129 | + post() { |
| 130 | + console.dir('runs after traversal'); |
| 131 | + }, |
| 132 | +}); |
| 133 | + |
| 134 | +LinkDescriptionElement.refract(objectElement, { plugins: [plugin] }); // => LinkDescriptionElement({ anchor = 'nodes/{thisNodeId}', anchorPointer = '#/relative/json/pointer/x' }) |
| 135 | +``` |
| 136 | + |
| 137 | +You can define as many plugins as needed to enhance the resulting namespaced ApiDOM structure. |
| 138 | +If multiple plugins with the same visitor method are defined, they run in parallel (just like in Babel). |
| 139 | + |
| 140 | +#### Replace Empty Element plugin |
| 141 | + |
| 142 | +This plugin is specific to YAML 1.2 format, which allows defining key-value pairs with empty key, |
| 143 | +empty value, or both. If the value is not provided in YAML format, this plugin compensates for |
| 144 | +this missing value with the most appropriate semantic element type. |
| 145 | + |
| 146 | +```js |
| 147 | +import { parse } from '@swagger-api/apidom-parser-adapter-yaml-1-2'; |
| 148 | +import { refractorPluginReplaceEmptyElement, JSONSchemaElement } from '@swagger-api/apidom-ns-json-schema-2020-12'; |
| 149 | + |
| 150 | +const yamlDefinition = ` |
| 151 | +$schema: 'https://json-schema.org/draft/2020-12/schema' |
| 152 | +if: |
| 153 | +`; |
| 154 | +const apiDOM = await parse(yamlDefinition); |
| 155 | +const jsonSchemaElement = JSONSchemaElement.refract(apiDOM.result, { |
| 156 | + plugins: [refractorPluginReplaceEmptyElement()], |
| 157 | +}); |
| 158 | + |
| 159 | +// => |
| 160 | +// (JSONSchema202012Element |
| 161 | +// (MemberElement |
| 162 | +// (StringElement) |
| 163 | +// (StringElement)) |
| 164 | +// (MemberElement |
| 165 | +// (StringElement) |
| 166 | +// (JSONSchema202012Element))) |
| 167 | + |
| 168 | +// => without the plugin the result would be as follows: |
| 169 | +// (JSONSchema202012Element |
| 170 | +// (MemberElement |
| 171 | +// (StringElement) |
| 172 | +// (StringElement)) |
| 173 | +// (MemberElement |
| 174 | +// (StringElement) |
| 175 | +// (StringElement))) |
| 176 | +``` |
| 177 | + |
| 178 | +## Implementation progress |
| 179 | + |
| 180 | +Only fully implemented specification objects should be checked here. |
| 181 | + |
| 182 | +- [x] [JSON Schema Object](https://json-schema.org/draft/2020-12/json-schema-core) |
| 183 | +- [x] [Link Description Object](https://json-schema.org/draft/2019-09/draft-handrews-json-schema-hyperschema-02#rfc.section.6) |
| 184 | + |
| 185 | + |
| 186 | + |
0 commit comments