|
5 | 5 | * using the ApiDOM visitor pattern. |
6 | 6 | * |
7 | 7 | * Plugin structure: |
8 | | - * - Must export a default function that accepts a context object with: |
| 8 | + * - Must export a default function that accepts a toolbox object with: |
9 | 9 | * - diagnostics: Array to collect validation diagnostics |
| 10 | + * - deps: External dependencies (vscode-languageserver-types, @speclynx/apidom-reference) |
10 | 11 | * - Returns an object with pre/post hooks and a visitor property |
11 | 12 | * - The visitor contains methods named after ApiDOM element types |
12 | | - * - Each visitor method receives a path object with information about the current element |
| 13 | + * - Each visitor method receives the element being visited |
13 | 14 | * |
14 | | - * @param {Object} context - Plugin context |
15 | | - * @param {Array} context.diagnostics - Array to collect validation diagnostics |
| 15 | + * @param {Object} toolbox - Plugin toolbox |
| 16 | + * @param {Array} toolbox.diagnostics - Array to collect validation diagnostics |
| 17 | + * @param {Object} toolbox.deps - External dependencies |
16 | 18 | */ |
17 | 19 |
|
18 | | -import { toValue } from '@speclynx/apidom-core'; |
19 | | -import { DiagnosticSeverity } from 'vscode-languageserver-types'; |
| 20 | +export default (toolbox) => { |
| 21 | + const {diagnostics, deps} = toolbox; |
| 22 | + const {DiagnosticSeverity} = deps['vscode-languageserver-types']; |
| 23 | + const {toValue} = deps['@speclynx/apidom-core']; |
20 | 24 |
|
21 | | -export default ({diagnostics}) => () => ({ |
22 | | - pre() { |
23 | | - console.log('Pre-validation hook'); |
24 | | - }, |
25 | | - post() { |
26 | | - console.log('Post-validation hook'); |
27 | | - }, |
28 | | - visitor: { |
29 | | - /** |
30 | | - * Visit all InfoElement nodes in the OpenAPI document |
31 | | - * @param {Object} path - Contains information about the current element |
32 | | - */ |
33 | | - InfoElement(path) { |
34 | | - const info = path.node; |
| 25 | + return { |
| 26 | + pre() { |
| 27 | + console.log('Pre-validation hook'); |
| 28 | + }, |
| 29 | + post() { |
| 30 | + console.log('Post-validation hook'); |
| 31 | + }, |
| 32 | + visitor: { |
| 33 | + /** |
| 34 | + * Visit all InfoElement nodes in the OpenAPI document |
| 35 | + * @param {Object} path - Contains information about the current element |
| 36 | + */ |
| 37 | + InfoElement(path) { |
| 38 | + const info = path.node; |
35 | 39 |
|
36 | | - // Example validation: Check if info.title exists |
37 | | - if (!info.get('title')) { |
38 | | - diagnostics.push({ |
39 | | - severity: DiagnosticSeverity.Error, |
40 | | - message: 'OpenAPI document is missing info.title', |
41 | | - code: 'missing-info-title', |
42 | | - range: getRange(info), |
43 | | - data: {path: path.getPathKeys()} |
44 | | - }); |
45 | | - } |
| 40 | + // Example validation: Check if info.title exists |
| 41 | + if (!info.get('title')) { |
| 42 | + diagnostics.push({ |
| 43 | + severity: DiagnosticSeverity.Error, |
| 44 | + message: 'OpenAPI document is missing info.title', |
| 45 | + code: 'missing-info-title', |
| 46 | + range: getRange(info), |
| 47 | + data: {path: path.getPathKeys()} |
| 48 | + }); |
| 49 | + } |
46 | 50 |
|
47 | | - // Example validation: Check if info.version exists |
48 | | - if (!info.get('version')) { |
49 | | - diagnostics.push({ |
50 | | - severity: DiagnosticSeverity.Warning, |
51 | | - message: 'OpenAPI document is missing info.version', |
52 | | - code: 'missing-info-version', |
53 | | - range: getRange(info), |
54 | | - data: {path: path.getPathKeys()} |
55 | | - }); |
56 | | - } |
57 | | - }, |
| 51 | + // Example validation: Check if info.version exists |
| 52 | + if (!info.get('version')) { |
| 53 | + diagnostics.push({ |
| 54 | + severity: DiagnosticSeverity.Warning, |
| 55 | + message: 'OpenAPI document is missing info.version', |
| 56 | + code: 'missing-info-version', |
| 57 | + range: getRange(info), |
| 58 | + data: {path: path.getPathKeys()} |
| 59 | + }); |
| 60 | + } |
| 61 | + }, |
58 | 62 |
|
59 | | - /** |
60 | | - * Visit all OperationElement nodes (HTTP operations like GET, POST, etc.) |
61 | | - * @param {Object} path - Contains information about the current element |
62 | | - */ |
63 | | - OperationElement(path) { |
64 | | - const operation = path.node; |
| 63 | + /** |
| 64 | + * Visit all OperationElement nodes (HTTP operations like GET, POST, etc.) |
| 65 | + * @param {Object} path - Contains information about the current element |
| 66 | + */ |
| 67 | + OperationElement(path) { |
| 68 | + const operation = path.node; |
65 | 69 |
|
66 | | - // Example validation: Check if operation has a summary |
67 | | - if (!operation.get('summary')) { |
68 | | - diagnostics.push({ |
69 | | - severity: DiagnosticSeverity.Warning, |
70 | | - message: 'Operation is missing a summary', |
71 | | - code: 'missing-operation-summary', |
72 | | - range: getRange(operation), |
73 | | - data: {path: path.getPathKeys()} |
74 | | - }); |
75 | | - } |
76 | | - }, |
| 70 | + // Example validation: Check if operation has a summary |
| 71 | + if (!operation.get('summary')) { |
| 72 | + diagnostics.push({ |
| 73 | + severity: DiagnosticSeverity.Warning, |
| 74 | + message: 'Operation is missing a summary', |
| 75 | + code: 'missing-operation-summary', |
| 76 | + range: getRange(operation), |
| 77 | + data: {path: path.getPathKeys()} |
| 78 | + }); |
| 79 | + } |
| 80 | + }, |
77 | 81 |
|
78 | | - /** |
79 | | - * Visit all SchemaElement nodes (JSON Schema definitions) |
80 | | - * @param {Object} path - Contains information about the current element |
81 | | - */ |
82 | | - SchemaElement(path) { |
83 | | - const schema = path.node; |
| 82 | + /** |
| 83 | + * Visit all SchemaElement nodes (JSON Schema definitions) |
| 84 | + * @param {Object} path - Contains information about the current element |
| 85 | + */ |
| 86 | + SchemaElement(path) { |
| 87 | + const schema = path.node; |
84 | 88 |
|
85 | | - // Example validation: Check for schemas without descriptions |
86 | | - if (!schema.get('description') && schema.get('type')) { |
87 | | - diagnostics.push({ |
88 | | - severity: DiagnosticSeverity.Information, |
89 | | - message: `Schema of type "${toValue(schema.get('type'))}" has no description`, |
90 | | - code: 'schema-missing-description', |
91 | | - range: getRange(schema), |
92 | | - data: {path: path.getPathKeys()} |
93 | | - }); |
| 89 | + // Example validation: Check for schemas without descriptions |
| 90 | + if (!schema.get('description') && schema.get('type')) { |
| 91 | + diagnostics.push({ |
| 92 | + severity: DiagnosticSeverity.Information, |
| 93 | + message: `Schema of type "${toValue(schema.get('type'))}" has no description`, |
| 94 | + code: 'schema-missing-description', |
| 95 | + range: getRange(schema), |
| 96 | + data: {path: path.getPathKeys()} |
| 97 | + }); |
| 98 | + } |
94 | 99 | } |
95 | 100 | } |
96 | | - } |
97 | | -}); |
| 101 | + }; |
| 102 | +}; |
98 | 103 |
|
99 | 104 | /** |
100 | 105 | * Extract LSP range from ApiDOM element |
|
0 commit comments