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