@speclynx/apidom-ns-json-schema-draft-7 4.0.3 → 4.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,90 +0,0 @@
1
- import { resolveSpecification, dispatchRefractorPlugins } from '@speclynx/apidom-core';
2
- import { traverse } from '@speclynx/apidom-traverse';
3
- import { Element, refract as baseRefract } from '@speclynx/apidom-datamodel';
4
- import { path } from 'ramda';
5
- import { Visitor as VisitorClass } from '@speclynx/apidom-ns-json-schema-draft-6';
6
-
7
- import specification from './specification.ts';
8
- import createToolbox, { type Toolbox } from './toolbox.ts';
9
- import type JSONSchemaElement from '../elements/JSONSchema.ts';
10
- import type LinkDescriptionElement from '../elements/LinkDescription.ts';
11
-
12
- /**
13
- * @public
14
- */
15
- export type RefractorPlugin = (toolbox: Toolbox) => {
16
- visitor?: object;
17
- pre?: () => void;
18
- post?: () => void;
19
- };
20
-
21
- /**
22
- * @public
23
- */
24
- export interface RefractorOptions {
25
- readonly element?: string;
26
- readonly plugins?: RefractorPlugin[];
27
- readonly specificationObj?: typeof specification;
28
- readonly consume?: boolean;
29
- }
30
-
31
- /**
32
- * @public
33
- */
34
- const refract = <T extends Element>(
35
- value: unknown,
36
- {
37
- element = 'jSONSchemaDraft7',
38
- plugins = [],
39
- specificationObj = specification,
40
- consume = false,
41
- }: RefractorOptions = {},
42
- ): T => {
43
- const genericElement = baseRefract(value);
44
- const resolvedSpec = resolveSpecification(specificationObj);
45
- const elementMap = resolvedSpec.elementMap as Record<string, string[]>;
46
- const specPath = elementMap[element];
47
-
48
- if (!specPath) {
49
- throw new Error(`Unknown element type: "${element}"`);
50
- }
51
-
52
- /**
53
- * This is where generic ApiDOM becomes semantic (namespace applied).
54
- * We don't allow consumers to hook into this translation.
55
- * Though we allow consumers to define their own plugins on already transformed ApiDOM.
56
- */
57
- const RootVisitorClass = path(specPath, resolvedSpec) as new (
58
- options: Record<string, unknown>,
59
- ) => InstanceType<typeof VisitorClass>;
60
- const rootVisitor = new RootVisitorClass({ specObj: resolvedSpec, consume });
61
-
62
- traverse(genericElement, rootVisitor);
63
-
64
- /**
65
- * Running plugins visitors means extra single traversal === performance hit.
66
- */
67
- return dispatchRefractorPlugins(rootVisitor.element, plugins, {
68
- toolboxCreator: createToolbox,
69
- }) as T;
70
- };
71
-
72
- /**
73
- * Refracts a value into a JSONSchemaElement.
74
- * @public
75
- */
76
- export const refractJSONSchema = <T extends Element = JSONSchemaElement>(
77
- value: unknown,
78
- options: Omit<RefractorOptions, 'element'> = {},
79
- ): T => refract(value, { ...options, element: 'jSONSchemaDraft7' });
80
-
81
- /**
82
- * Refracts a value into a LinkDescriptionElement.
83
- * @public
84
- */
85
- export const refractLinkDescription = <T extends Element = LinkDescriptionElement>(
86
- value: unknown,
87
- options: Omit<RefractorOptions, 'element'> = {},
88
- ): T => refract(value, { ...options, element: 'linkDescription' });
89
-
90
- export default refract;
@@ -1,51 +0,0 @@
1
- import { resolveSpecification, type ResolvedSpecification } from '@speclynx/apidom-core';
2
- import { isPlainObject } from 'ramda-adjunct';
3
- import { JSONReferenceElement } from '@speclynx/apidom-ns-json-schema-draft-6';
4
-
5
- import JSONSchemaElement from '../elements/JSONSchema.ts';
6
- import LinkDescriptionElement from '../elements/LinkDescription.ts';
7
- import specification from './specification.ts';
8
-
9
- /**
10
- * @public
11
- */
12
- export interface FixedField {
13
- name: string;
14
- alias?: string;
15
- $visitor: unknown;
16
- }
17
-
18
- interface ResolvedSpec extends ResolvedSpecification {
19
- visitors: {
20
- document: {
21
- objects: Record<string, { fixedFields: Record<string, unknown> }>;
22
- };
23
- };
24
- }
25
-
26
- // Resolve specification to dereference $ref pointers
27
- const resolvedSpec = resolveSpecification<ResolvedSpec>(specification);
28
-
29
- // Extract fixed fields as list of { name, alias?, $visitor }
30
- const getFixedFields = (fixedFieldsSpec: Record<string, unknown>): FixedField[] => {
31
- return Object.entries(fixedFieldsSpec).map(([name, fieldSpec]) => {
32
- if (isPlainObject(fieldSpec)) {
33
- return { name, ...fieldSpec } as FixedField;
34
- }
35
- return { name, $visitor: fieldSpec };
36
- });
37
- };
38
-
39
- // Define lazy getters for fixedFields on element classes
40
- // Note: JSONReferenceElement inherits fixedFields from draft-6/draft-4
41
- Object.defineProperty(JSONSchemaElement, 'fixedFields', {
42
- get: () => getFixedFields(resolvedSpec.visitors.document.objects.JSONSchema.fixedFields),
43
- enumerable: true,
44
- });
45
-
46
- Object.defineProperty(LinkDescriptionElement, 'fixedFields', {
47
- get: () => getFixedFields(resolvedSpec.visitors.document.objects.LinkDescription.fixedFields),
48
- enumerable: true,
49
- });
50
-
51
- export { JSONSchemaElement, JSONReferenceElement, LinkDescriptionElement };
@@ -1,260 +0,0 @@
1
- import {
2
- ArrayElement,
3
- ObjectElement,
4
- StringElement,
5
- isArrayElement,
6
- isElement,
7
- isMemberElement,
8
- isStringElement,
9
- includesClasses,
10
- cloneDeep,
11
- SourceMapElement,
12
- StyleElement,
13
- } from '@speclynx/apidom-datamodel';
14
- import { toValue } from '@speclynx/apidom-core';
15
- import { Path, getNodeType } from '@speclynx/apidom-traverse';
16
-
17
- import JSONSchemaElement from '../../elements/JSONSchema.ts';
18
- import LinkDescriptionElement from '../../elements/LinkDescription.ts';
19
-
20
- /**
21
- * This plugin is specific to YAML 1.2 format, which allows defining key-value pairs
22
- * with empty key, empty value, or both. If the value is not provided in YAML format,
23
- * this plugin compensates for this missing value with the most appropriate semantic element type.
24
- *
25
- * https://yaml.org/spec/1.2.2/#72-empty-nodes
26
- *
27
- * @example
28
- *
29
- * ```yaml
30
- * $schema: http://json-schema.org/draft-07/schema#
31
- * items:
32
- * ```
33
- * Refracting result without this plugin:
34
- *
35
- * (JSONSchemaElement
36
- * (MemberElement
37
- * (StringElement)
38
- * (StringElement))
39
- * (MemberElement
40
- * (StringElement)
41
- * (StringElement))
42
- *
43
- * Refracting result with this plugin:
44
- *
45
- * (JSONSchemaElement
46
- * (MemberElement
47
- * (StringElement)
48
- * (StringElement))
49
- * (MemberElement
50
- * (StringElement)
51
- * (JSONSchemaElement))
52
- */
53
-
54
- const isEmptyElement = (element: any) =>
55
- isStringElement(element) && includesClasses(element, ['yaml-e-node', 'yaml-e-scalar']);
56
-
57
- const schema = {
58
- JSONSchemaDraft7Element: {
59
- additionalItems(...args: any[]) {
60
- return new JSONSchemaElement(...args);
61
- },
62
- items(...args: any[]) {
63
- return new JSONSchemaElement(...args);
64
- },
65
- contains(...args: any[]) {
66
- return new JSONSchemaElement(...args);
67
- },
68
- required(...args: any[]) {
69
- const element = new ArrayElement(...args);
70
- element.classes.push('json-schema-required');
71
- return element;
72
- },
73
- properties(...args: any[]) {
74
- const element = new ObjectElement(...args);
75
- element.classes.push('json-schema-properties');
76
- return element;
77
- },
78
- additionalProperties(...args: any[]) {
79
- return new JSONSchemaElement(...args);
80
- },
81
- patternProperties(...args: any[]) {
82
- const element = new ObjectElement(...args);
83
- element.classes.push('json-schema-patternProperties');
84
- return element;
85
- },
86
- dependencies(...args: any[]) {
87
- const element = new ObjectElement(...args);
88
- element.classes.push('json-schema-dependencies');
89
- return element;
90
- },
91
- propertyNames(...args: any[]) {
92
- return new JSONSchemaElement(...args);
93
- },
94
- enum(...args: any[]) {
95
- const element = new ArrayElement(...args);
96
- element.classes.push('json-schema-enum');
97
- return element;
98
- },
99
- allOf(...args: any[]) {
100
- const element = new ArrayElement(...args);
101
- element.classes.push('json-schema-allOf');
102
- return element;
103
- },
104
- anyOf(...args: any[]) {
105
- const element = new ArrayElement(...args);
106
- element.classes.push('json-schema-anyOf');
107
- return element;
108
- },
109
- oneOf(...args: any[]) {
110
- const element = new ArrayElement(...args);
111
- element.classes.push('json-schema-oneOf');
112
- return element;
113
- },
114
- if(...args: any[]) {
115
- return new JSONSchemaElement(...args);
116
- },
117
- then(...args: any[]) {
118
- return new JSONSchemaElement(...args);
119
- },
120
- else(...args: any[]) {
121
- return new JSONSchemaElement(...args);
122
- },
123
- not(...args: any[]) {
124
- return new JSONSchemaElement(...args);
125
- },
126
- definitions(...args: any[]) {
127
- const element = new ObjectElement(...args);
128
- element.classes.push('json-schema-definitions');
129
- return element;
130
- },
131
- examples(...args: any[]) {
132
- const element = new ArrayElement(...args);
133
- element.classes.push('json-schema-examples');
134
- return element;
135
- },
136
- links(...args: any[]) {
137
- const element = new ArrayElement(...args);
138
- element.classes.push('json-schema-links');
139
- return element;
140
- },
141
- },
142
- LinkDescriptionElement: {
143
- hrefSchema(...args: any[]) {
144
- return new JSONSchemaElement(...args);
145
- },
146
- targetSchema(...args: any[]) {
147
- return new JSONSchemaElement(...args);
148
- },
149
- submissionSchema(...args: any[]) {
150
- return new JSONSchemaElement(...args);
151
- },
152
- templatePointers(...args: any[]) {
153
- return new ObjectElement(...args);
154
- },
155
- templateRequired(...args: any[]) {
156
- return new ArrayElement(...args);
157
- },
158
- targetHints(...args: any[]) {
159
- return new ObjectElement(...args);
160
- },
161
- headerSchema(...args: any[]) {
162
- return new JSONSchemaElement(...args);
163
- },
164
- },
165
- 'json-schema-properties': {
166
- '[key: *]': function key(...args: any[]) {
167
- return new JSONSchemaElement(...args);
168
- },
169
- },
170
- 'json-schema-patternProperties': {
171
- '[key: *]': function key(...args: any[]) {
172
- return new JSONSchemaElement(...args);
173
- },
174
- },
175
- 'json-schema-dependencies': {
176
- '[key: *]': function key(...args: any[]) {
177
- return new JSONSchemaElement(...args);
178
- },
179
- },
180
- 'json-schema-allOf': {
181
- '<*>': function asterisk(...args: any[]) {
182
- return new JSONSchemaElement(...args);
183
- },
184
- },
185
- 'json-schema-anyOf': {
186
- '<*>': function asterisk(...args: any[]) {
187
- return new JSONSchemaElement(...args);
188
- },
189
- },
190
- 'json-schema-oneOf': {
191
- '<*>': function asterisk(...args: any[]) {
192
- return new JSONSchemaElement(...args);
193
- },
194
- },
195
- 'json-schema-definitions': {
196
- '[key: *]': function key(...args: any[]) {
197
- return new JSONSchemaElement(...args);
198
- },
199
- },
200
- 'json-schema-links': {
201
- '<*>': function asterisk(...args: any[]) {
202
- return new LinkDescriptionElement(...args);
203
- },
204
- },
205
- };
206
-
207
- const findElementFactory = (ancestor: any, keyName: string) => {
208
- const elementType = getNodeType(ancestor);
209
- const classType = ancestor.isMetaEmpty ? undefined : ancestor.classes.at(0); // @ts-ignore
210
- const keyMapping = schema[elementType] || schema[classType];
211
-
212
- return typeof keyMapping === 'undefined'
213
- ? undefined
214
- : Object.hasOwn(keyMapping, '[key: *]')
215
- ? keyMapping['[key: *]']
216
- : keyMapping[keyName];
217
- };
218
-
219
- /**
220
- * @public
221
- */
222
- const plugin = () => () => ({
223
- visitor: {
224
- StringElement(path: Path<StringElement>) {
225
- const element = path.node;
226
-
227
- if (!isEmptyElement(element)) return;
228
-
229
- // getAncestorNodes() returns [parent, grandparent, ..., root], so reverse to get [root, ..., parent]
230
- const lineage = path.getAncestorNodes().reverse().filter(isElement);
231
- const parentElement = lineage.at(-1);
232
- let elementFactory;
233
- let context;
234
-
235
- if (isArrayElement(parentElement)) {
236
- context = element;
237
- elementFactory = findElementFactory(parentElement, '<*>');
238
- } else if (isMemberElement(parentElement)) {
239
- context = lineage.at(-2);
240
- elementFactory = findElementFactory(context, toValue(parentElement.key) as string);
241
- }
242
-
243
- // no element factory found
244
- if (typeof elementFactory !== 'function') return;
245
-
246
- const replacement = elementFactory.call(
247
- { context },
248
- undefined,
249
- element.isMetaEmpty ? undefined : element.meta.cloneDeep(),
250
- element.isAttributesEmpty ? undefined : cloneDeep(element.attributes),
251
- );
252
-
253
- SourceMapElement.transfer(element, replacement);
254
- StyleElement.transfer(element, replacement);
255
- path.replaceWith(replacement);
256
- },
257
- },
258
- });
259
-
260
- export default plugin;
@@ -1,88 +0,0 @@
1
- import { pipe, assocPath, dissocPath } from 'ramda';
2
- import { specificationObj } from '@speclynx/apidom-ns-json-schema-draft-6';
3
-
4
- import JSONSchemaVisitor from './visitors/json-schema/index.ts';
5
- import LinkDescriptionVisitor from './visitors/json-schema/link-description/index.ts';
6
-
7
- const specification = pipe(
8
- // JSON Schema object modifications
9
- assocPath(['visitors', 'document', 'objects', 'JSONSchema', 'element'], 'jSONSchemaDraft7'),
10
- assocPath(['visitors', 'document', 'objects', 'JSONSchema', '$visitor'], JSONSchemaVisitor),
11
- assocPath(
12
- ['visitors', 'document', 'objects', 'JSONSchema', 'fixedFields', '$comment'],
13
- specificationObj.visitors.value,
14
- ),
15
- assocPath(
16
- ['visitors', 'document', 'objects', 'JSONSchema', 'fixedFields', 'if'],
17
- specificationObj.visitors.JSONSchemaOrJSONReferenceVisitor,
18
- ),
19
- assocPath(
20
- ['visitors', 'document', 'objects', 'JSONSchema', 'fixedFields', 'then'],
21
- specificationObj.visitors.JSONSchemaOrJSONReferenceVisitor,
22
- ),
23
- assocPath(
24
- ['visitors', 'document', 'objects', 'JSONSchema', 'fixedFields', 'else'],
25
- specificationObj.visitors.JSONSchemaOrJSONReferenceVisitor,
26
- ),
27
- dissocPath(['visitors', 'document', 'objects', 'JSONSchema', 'fixedFields', 'media']),
28
- assocPath(
29
- ['visitors', 'document', 'objects', 'JSONSchema', 'fixedFields', 'contentEncoding'],
30
- specificationObj.visitors.value,
31
- ),
32
- assocPath(
33
- ['visitors', 'document', 'objects', 'JSONSchema', 'fixedFields', 'contentMediaType'],
34
- specificationObj.visitors.value,
35
- ),
36
- assocPath(
37
- ['visitors', 'document', 'objects', 'JSONSchema', 'fixedFields', 'writeOnly'],
38
- specificationObj.visitors.value,
39
- ),
40
- // Link Description object modifications
41
- assocPath(
42
- ['visitors', 'document', 'objects', 'LinkDescription', '$visitor'],
43
- LinkDescriptionVisitor,
44
- ),
45
- assocPath(
46
- ['visitors', 'document', 'objects', 'LinkDescription', 'fixedFields', 'anchor'],
47
- specificationObj.visitors.value,
48
- ),
49
- assocPath(
50
- ['visitors', 'document', 'objects', 'LinkDescription', 'fixedFields', 'anchorPointer'],
51
- specificationObj.visitors.value,
52
- ),
53
- dissocPath(['visitors', 'document', 'objects', 'LinkDescription', 'fixedFields', 'mediaType']),
54
- assocPath(
55
- ['visitors', 'document', 'objects', 'LinkDescription', 'fixedFields', 'targetMediaType'],
56
- specificationObj.visitors.value,
57
- ),
58
- assocPath(
59
- ['visitors', 'document', 'objects', 'LinkDescription', 'fixedFields', 'targetHints'],
60
- specificationObj.visitors.value,
61
- ),
62
- assocPath(
63
- ['visitors', 'document', 'objects', 'LinkDescription', 'fixedFields', 'description'],
64
- specificationObj.visitors.value,
65
- ),
66
- assocPath(
67
- ['visitors', 'document', 'objects', 'LinkDescription', 'fixedFields', '$comment'],
68
- specificationObj.visitors.value,
69
- ),
70
- assocPath(
71
- ['visitors', 'document', 'objects', 'LinkDescription', 'fixedFields', 'headerSchema'],
72
- specificationObj.visitors.JSONSchemaOrJSONReferenceVisitor,
73
- ),
74
- dissocPath([
75
- 'visitors',
76
- 'document',
77
- 'objects',
78
- 'LinkDescription',
79
- 'fixedFields',
80
- 'submissionEncType',
81
- ]),
82
- assocPath(
83
- ['visitors', 'document', 'objects', 'LinkDescription', 'fixedFields', 'submissionMediaType'],
84
- specificationObj.visitors.value,
85
- ),
86
- )(specificationObj);
87
-
88
- export default specification as typeof specificationObj;
@@ -1,23 +0,0 @@
1
- import { isStringElement, Namespace } from '@speclynx/apidom-datamodel';
2
-
3
- import * as jsonSchemaDraft7Predicates from '../predicates.ts';
4
- import jsonSchemaDraft7Namespace from '../namespace.ts';
5
-
6
- /**
7
- * @public
8
- */
9
- export interface Toolbox {
10
- predicates: Record<string, (...args: any[]) => boolean>;
11
- namespace: Namespace;
12
- }
13
-
14
- const createToolbox = (): Toolbox => {
15
- const namespace = new Namespace();
16
- const predicates = { ...jsonSchemaDraft7Predicates, isStringElement };
17
-
18
- namespace.use(jsonSchemaDraft7Namespace);
19
-
20
- return { predicates, namespace };
21
- };
22
-
23
- export default createToolbox;
@@ -1,27 +0,0 @@
1
- import { BooleanElement } from '@speclynx/apidom-datamodel';
2
- import {
3
- JSONSchemaVisitor as JSONSchemaDraft6Visitor,
4
- JSONSchemaVisitorOptions,
5
- } from '@speclynx/apidom-ns-json-schema-draft-6';
6
-
7
- import JSONSchemaElement from '../../../elements/JSONSchema.ts';
8
-
9
- export type { JSONSchemaVisitorOptions };
10
-
11
- /**
12
- * @public
13
- */
14
- class JSONSchemaVisitor extends JSONSchemaDraft6Visitor {
15
- declare public element: JSONSchemaElement | BooleanElement;
16
-
17
- constructor(options: JSONSchemaVisitorOptions) {
18
- super(options);
19
- this.element = new JSONSchemaElement();
20
- }
21
-
22
- get defaultDialectIdentifier(): string {
23
- return 'http://json-schema.org/draft-07/schema#';
24
- }
25
- }
26
-
27
- export default JSONSchemaVisitor;
@@ -1,22 +0,0 @@
1
- import {
2
- LinkDescriptionVisitor as JSONSchemaDraft6LinkDescriptionVisitor,
3
- LinkDescriptionVisitorOptions,
4
- } from '@speclynx/apidom-ns-json-schema-draft-6';
5
-
6
- import LinkDescriptionElement from '../../../../elements/LinkDescription.ts';
7
-
8
- export type { LinkDescriptionVisitorOptions };
9
-
10
- /**
11
- * @public
12
- */
13
- class LinkDescriptionVisitor extends JSONSchemaDraft6LinkDescriptionVisitor {
14
- declare public readonly element: LinkDescriptionElement;
15
-
16
- constructor(options: LinkDescriptionVisitorOptions) {
17
- super(options);
18
- this.element = new LinkDescriptionElement();
19
- }
20
- }
21
-
22
- export default LinkDescriptionVisitor;