source: node_modules/@swagger-api/apidom-ns-json-schema-draft-4/README.md@ d24f17c

main
Last change on this file since d24f17c was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 7.0 KB
Line 
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
9You 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
17JSON 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
18of [primitive ones](https://github.com/refractproject/minim/tree/master/lib/primitives).
19
20```js
21import { createNamespace } from '@swagger-api/apidom-core';
22import jsonShemaDraft4Namespace from '@swagger-api/apidom-ns-json-schema-draft-4';
23
24const namespace = createNamespace(jsonShemaDraft4Namespace);
25
26const objectElement = new namespace.elements.Object();
27const jsonSchemaElement = new namespace.elements.JSONSchemaDraft4();
28```
29
30When namespace instance is created in this way, it will extend the base namespace
31with the namespace provided as an argument.
32
33Elements from the namespace can also be used directly by importing them.
34
35```js
36import { JSONSchemaElement, JSONReferenceElement, LinkDescriptionElement, MediaElement } from '@swagger-api/apidom-ns-json-schema-draft-4';
37
38const jsonSchemaElement = new JSONSchemaElement();
39const jsonReferenceElement = new JSONReferenceElement();
40const linkDescriptionElement = new LinkDescriptionElement();
41const mediaElement = new MediaElement();
42```
43
44## Predicates
45
46This package exposes [predicates](https://github.com/swagger-api/apidom/blob/main/packages/apidom-ns-json-schema-draft-4/src/predicates.ts)
47for all higher order elements that are part of this namespace.
48
49```js
50import { isJSONSchemaElement, JSONSchemaElement } from '@swagger-api/apidom-ns-json-schema-draft-4';
51
52const jsonSchemaElement = new JSONSchemaElement();
53
54isJSONSchemaElement(jsonSchemaElement); // => true
55```
56
57## Traversal
58
59Traversing ApiDOM in this namespace is possible by using `visit` function from `apidom` package.
60This 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).
61To 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
64import { visit } from '@swagger-api/apidom-core';
65import { JSONSchemaElement, keyMap, getNodeType } from '@swagger-api/apidom-ns-json-schema-draft-4';
66
67const element = new JSONSchemaElement();
68
69const visitor = {
70 JSONSchemaDraft4Element(jsonSchemaElement) {
71 console.dir(jsonSchemaElement);
72 },
73};
74
75visit(element, visitor, { keyMap, nodeTypeGetter: getNodeType });
76```
77
78## Refractors
79
80Refractor is a special layer inside the namespace that can transform either JavaScript structures
81or generic ApiDOM structures into structures built from elements of this namespace.
82
83**Refracting JavaScript structures**:
84
85```js
86import { MediaElement } from '@swagger-api/apidom-ns-json-schema-draft-4';
87
88const object = {
89 binaryEncoding: 'base64',
90 type: 'image/png',
91};
92
93MediaElement.refract(object); // => MediaElement({ binaryEncoding, type })
94```
95
96**Refracting generic ApiDOM structures**:
97
98```js
99import { ObjectElement } from '@swagger-api/apidom-core';
100import { MediaElement } from '@swagger-api/apidom-ns-json-schema-draft-4';
101
102const objectElement = new ObjectElement({
103 binaryEncoding: 'base64',
104 type: 'image/png',
105});
106
107MediaElement.refract(objectElement); // => MediaElement({ binaryEncoding = 'base64', type = 'image/png' })
108```
109
110### Refractor plugins
111
112Refractors can accept plugins as a second argument of refract static method.
113
114```js
115import { ObjectElement } from '@swagger-api/apidom-core';
116import { MediaElement } from '@swagger-api/apidom-ns-json-schema-draft-4';
117
118const objectElement = new ObjectElement({
119 binaryEncoding: 'base64',
120 type: 'image/png',
121});
122
123const 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
138MediaElement.refract(objectElement, { plugins: [plugin] }); // => MediaElement({ binaryEncoding = 'base64', type = 'image/gif' })
139```
140
141You can define as many plugins as needed to enhance the resulting namespaced ApiDOM structure.
142If multiple plugins with the same visitor method are defined, they run in parallel (just like in Babel).
143
144#### Replace Empty Element plugin
145
146This plugin is specific to YAML 1.2 format, which allows defining key-value pairs with empty key,
147empty value, or both. If the value is not provided in YAML format, this plugin compensates for
148this missing value with the most appropriate semantic element type.
149
150```js
151import { parse } from '@swagger-api/apidom-parser-adapter-yaml-1-2';
152import { refractorPluginReplaceEmptyElement, JSONSchemaElement } from '@swagger-api/apidom-ns-json-schema-draft-4';
153
154const yamlDefinition = `
155$schema: 'http://json-schema.org/draft-04/schema#'
156additionalProperties:
157`;
158const apiDOM = await parse(yamlDefinition);
159const 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
184Only 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)
Note: See TracBrowser for help on using the repository browser.