@redocly/openapi-core 1.1.0 → 1.2.1

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.
Files changed (77) hide show
  1. package/CHANGELOG.md +20 -0
  2. package/README.md +250 -7
  3. package/lib/bundle.d.ts +12 -6
  4. package/lib/bundle.js +34 -24
  5. package/lib/config/builtIn.js +4 -0
  6. package/lib/config/config-resolvers.js +19 -6
  7. package/lib/config/config.d.ts +9 -9
  8. package/lib/config/config.js +32 -17
  9. package/lib/config/load.d.ts +3 -2
  10. package/lib/config/load.js +2 -2
  11. package/lib/config/rules.d.ts +2 -2
  12. package/lib/config/types.d.ts +10 -3
  13. package/lib/index.d.ts +4 -3
  14. package/lib/index.js +10 -6
  15. package/lib/lint.js +10 -16
  16. package/lib/oas-types.d.ts +16 -10
  17. package/lib/oas-types.js +52 -26
  18. package/lib/rules/async2/channels-kebab-case.d.ts +2 -0
  19. package/lib/rules/async2/channels-kebab-case.js +19 -0
  20. package/lib/rules/async2/index.d.ts +12 -0
  21. package/lib/rules/async2/index.js +22 -0
  22. package/lib/rules/async2/no-channel-trailing-slash.d.ts +2 -0
  23. package/lib/rules/async2/no-channel-trailing-slash.js +16 -0
  24. package/lib/rules/common/assertions/asserts.js +12 -4
  25. package/lib/rules/common/scalar-property-missing-example.js +1 -1
  26. package/lib/rules/common/spec.d.ts +2 -2
  27. package/lib/rules/common/spec.js +3 -3
  28. package/lib/rules/oas2/index.js +1 -1
  29. package/lib/rules/oas3/index.js +1 -1
  30. package/lib/rules/oas3/no-server-example.com.js +3 -2
  31. package/lib/types/asyncapi.d.ts +2 -0
  32. package/lib/types/asyncapi.js +1034 -0
  33. package/lib/types/oas3_1.js +8 -1
  34. package/lib/types/redocly-yaml.js +3 -0
  35. package/lib/typings/asyncapi.d.ts +21 -0
  36. package/lib/typings/asyncapi.js +2 -0
  37. package/lib/visitors.d.ts +12 -0
  38. package/lib/walk.d.ts +3 -3
  39. package/package.json +4 -2
  40. package/src/__tests__/__snapshots__/bundle.test.ts.snap +1 -1
  41. package/src/__tests__/bundle.test.ts +65 -25
  42. package/src/__tests__/fixtures/lint/openapi.yaml +10 -0
  43. package/src/__tests__/fixtures/redocly.yaml +2 -0
  44. package/src/__tests__/lint.test.ts +67 -8
  45. package/src/bundle.ts +62 -35
  46. package/src/config/__tests__/__snapshots__/config.test.ts.snap +24 -0
  47. package/src/config/__tests__/config.test.ts +15 -4
  48. package/src/config/__tests__/load.test.ts +35 -2
  49. package/src/config/builtIn.ts +4 -0
  50. package/src/config/config-resolvers.ts +25 -6
  51. package/src/config/config.ts +51 -27
  52. package/src/config/load.ts +11 -4
  53. package/src/config/rules.ts +2 -2
  54. package/src/config/types.ts +17 -4
  55. package/src/index.ts +10 -2
  56. package/src/lint.ts +13 -22
  57. package/src/oas-types.ts +59 -21
  58. package/src/rules/async2/__tests__/channels-kebab-case.test.ts +141 -0
  59. package/src/rules/async2/__tests__/no-channel-trailing-slash.test.ts +97 -0
  60. package/src/rules/async2/channels-kebab-case.ts +18 -0
  61. package/src/rules/async2/index.ts +22 -0
  62. package/src/rules/async2/no-channel-trailing-slash.ts +15 -0
  63. package/src/rules/common/assertions/asserts.ts +16 -4
  64. package/src/rules/common/scalar-property-missing-example.ts +2 -2
  65. package/src/rules/common/spec.ts +2 -2
  66. package/src/rules/oas2/index.ts +2 -2
  67. package/src/rules/oas3/__tests__/no-server-example.com.test.ts +36 -1
  68. package/src/rules/oas3/__tests__/spec/spec.test.ts +1 -1
  69. package/src/rules/oas3/index.ts +2 -2
  70. package/src/rules/oas3/no-server-example.com.ts +3 -2
  71. package/src/types/asyncapi.ts +1142 -0
  72. package/src/types/oas3_1.ts +7 -1
  73. package/src/types/redocly-yaml.ts +3 -0
  74. package/src/typings/asyncapi.ts +26 -0
  75. package/src/visitors.ts +22 -0
  76. package/src/walk.ts +3 -3
  77. package/tsconfig.tsbuildinfo +1 -1
@@ -1,4 +1,4 @@
1
- import { OasVersion } from '../../oas-types';
1
+ import { SpecVersion } from '../../oas-types';
2
2
  import { Config, StyleguideConfig } from '../config';
3
3
  import { getMergedConfig } from '../utils';
4
4
  import { doesYamlFileExist } from '../../utils';
@@ -117,6 +117,7 @@ describe('getMergedConfig', () => {
117
117
  "_usedVersions": Set {},
118
118
  "configFile": "redocly.yaml",
119
119
  "decorators": Object {
120
+ "async2": Object {},
120
121
  "oas2": Object {},
121
122
  "oas3_0": Object {},
122
123
  "oas3_1": Object {},
@@ -127,6 +128,7 @@ describe('getMergedConfig', () => {
127
128
  "pluginPaths": Array [],
128
129
  "plugins": Array [],
129
130
  "preprocessors": Object {
131
+ "async2": Object {},
130
132
  "oas2": Object {},
131
133
  "oas3_0": Object {},
132
134
  "oas3_1": Object {},
@@ -140,6 +142,9 @@ describe('getMergedConfig', () => {
140
142
  },
141
143
  "recommendedFallback": false,
142
144
  "rules": Object {
145
+ "async2": Object {
146
+ "operation-summary": "warn",
147
+ },
143
148
  "oas2": Object {
144
149
  "operation-summary": "warn",
145
150
  },
@@ -217,6 +222,7 @@ describe('getMergedConfig', () => {
217
222
  "_usedVersions": Set {},
218
223
  "configFile": "redocly.yaml",
219
224
  "decorators": Object {
225
+ "async2": Object {},
220
226
  "oas2": Object {},
221
227
  "oas3_0": Object {},
222
228
  "oas3_1": Object {},
@@ -227,6 +233,7 @@ describe('getMergedConfig', () => {
227
233
  "pluginPaths": Array [],
228
234
  "plugins": Array [],
229
235
  "preprocessors": Object {
236
+ "async2": Object {},
230
237
  "oas2": Object {},
231
238
  "oas3_0": Object {},
232
239
  "oas3_1": Object {},
@@ -242,6 +249,10 @@ describe('getMergedConfig', () => {
242
249
  },
243
250
  "recommendedFallback": false,
244
251
  "rules": Object {
252
+ "async2": Object {
253
+ "no-empty-servers": "error",
254
+ "operation-summary": "error",
255
+ },
245
256
  "oas2": Object {
246
257
  "no-empty-servers": "error",
247
258
  "operation-summary": "error",
@@ -279,19 +290,19 @@ describe('StyleguideConfig.extendTypes', () => {
279
290
  };
280
291
  it('should call only oas3 types extension', () => {
281
292
  const styleguideConfig = new StyleguideConfig(testRawConfigStyleguide);
282
- styleguideConfig.extendTypes({}, OasVersion.Version3_0);
293
+ styleguideConfig.extendTypes({}, SpecVersion.OAS3_0);
283
294
  expect(oas3).toHaveBeenCalledTimes(1);
284
295
  expect(oas2).toHaveBeenCalledTimes(0);
285
296
  });
286
297
  it('should call only oas2 types extension', () => {
287
298
  const styleguideConfig = new StyleguideConfig(testRawConfigStyleguide);
288
- styleguideConfig.extendTypes({}, OasVersion.Version2);
299
+ styleguideConfig.extendTypes({}, SpecVersion.OAS2);
289
300
  expect(oas3).toHaveBeenCalledTimes(0);
290
301
  expect(oas2).toHaveBeenCalledTimes(1);
291
302
  });
292
303
  it('should throw error if for oas version different from 2 and 3', () => {
293
304
  const styleguideConfig = new StyleguideConfig(testRawConfigStyleguide);
294
- expect(() => styleguideConfig.extendTypes({}, 'something else' as OasVersion)).toThrowError(
305
+ expect(() => styleguideConfig.extendTypes({}, 'something else' as SpecVersion)).toThrowError(
295
306
  'Not implemented'
296
307
  );
297
308
  });
@@ -73,8 +73,8 @@ describe('findConfig', () => {
73
73
  .spyOn(fs, 'existsSync')
74
74
  .mockImplementation((name) => name === 'redocly.yaml' || name === '.redocly.yaml');
75
75
  expect(findConfig).toThrow(`
76
- Multiple configuration files are not allowed.
77
- Found the following files: redocly.yaml, .redocly.yaml.
76
+ Multiple configuration files are not allowed.
77
+ Found the following files: redocly.yaml, .redocly.yaml.
78
78
  Please use 'redocly.yaml' instead.
79
79
  `);
80
80
  });
@@ -124,6 +124,39 @@ describe('createConfig', () => {
124
124
  overridesRules: rawConfig.rules as Record<string, RuleConfig>,
125
125
  });
126
126
  });
127
+
128
+ it('should create config from object with a custom plugin', async () => {
129
+ const testCustomRule = jest.fn();
130
+ const rawConfig: FlatRawConfig = {
131
+ extends: [],
132
+ plugins: [
133
+ {
134
+ id: 'my-plugin',
135
+ rules: {
136
+ oas3: {
137
+ 'test-rule': testCustomRule,
138
+ },
139
+ },
140
+ },
141
+ ],
142
+ rules: {
143
+ 'my-plugin/test-rule': 'error',
144
+ },
145
+ };
146
+ const config = await createConfig(rawConfig);
147
+
148
+ expect(config.styleguide.plugins[0]).toEqual({
149
+ id: 'my-plugin',
150
+ rules: {
151
+ oas3: {
152
+ 'my-plugin/test-rule': testCustomRule,
153
+ },
154
+ },
155
+ });
156
+ expect(config.styleguide.rules.oas3_0).toEqual({
157
+ 'my-plugin/test-rule': 'error',
158
+ });
159
+ });
127
160
  });
128
161
 
129
162
  function verifyExtendedConfig(
@@ -3,6 +3,7 @@ import all from './all';
3
3
  import minimal from './minimal';
4
4
  import { rules as oas3Rules } from '../rules/oas3';
5
5
  import { rules as oas2Rules } from '../rules/oas2';
6
+ import { rules as async2Rules } from '../rules/async2';
6
7
  import { preprocessors as oas3Preprocessors } from '../rules/oas3';
7
8
  import { preprocessors as oas2Preprocessors } from '../rules/oas2';
8
9
  import { decorators as oas3Decorators } from '../decorators/oas3';
@@ -24,14 +25,17 @@ export const defaultPlugin: Plugin = {
24
25
  rules: {
25
26
  oas3: oas3Rules,
26
27
  oas2: oas2Rules,
28
+ async2: async2Rules,
27
29
  } as CustomRulesConfig,
28
30
  preprocessors: {
29
31
  oas3: oas3Preprocessors,
30
32
  oas2: oas2Preprocessors,
33
+ async2: {},
31
34
  },
32
35
  decorators: {
33
36
  oas3: oas3Decorators,
34
37
  oas2: oas2Decorators,
38
+ async2: {},
35
39
  },
36
40
  configs: builtInConfigs,
37
41
  };
@@ -141,8 +141,8 @@ export function resolvePlugins(
141
141
  };
142
142
 
143
143
  if (pluginModule.rules) {
144
- if (!pluginModule.rules.oas3 && !pluginModule.rules.oas2) {
145
- throw new Error(`Plugin rules must have \`oas3\` or \`oas2\` rules "${p}.`);
144
+ if (!pluginModule.rules.oas3 && !pluginModule.rules.oas2 && !pluginModule.rules.async2) {
145
+ throw new Error(`Plugin rules must have \`oas3\`, \`oas2\` or \`async2\` rules "${p}.`);
146
146
  }
147
147
  plugin.rules = {};
148
148
  if (pluginModule.rules.oas3) {
@@ -151,11 +151,18 @@ export function resolvePlugins(
151
151
  if (pluginModule.rules.oas2) {
152
152
  plugin.rules.oas2 = prefixRules(pluginModule.rules.oas2, id);
153
153
  }
154
+ if (pluginModule.rules.async2) {
155
+ plugin.rules.async2 = prefixRules(pluginModule.rules.async2, id);
156
+ }
154
157
  }
155
158
  if (pluginModule.preprocessors) {
156
- if (!pluginModule.preprocessors.oas3 && !pluginModule.preprocessors.oas2) {
159
+ if (
160
+ !pluginModule.preprocessors.oas3 &&
161
+ !pluginModule.preprocessors.oas2 &&
162
+ !pluginModule.preprocessors.async2
163
+ ) {
157
164
  throw new Error(
158
- `Plugin \`preprocessors\` must have \`oas3\` or \`oas2\` preprocessors "${p}.`
165
+ `Plugin \`preprocessors\` must have \`oas3\`, \`oas2\` or \`async2\` preprocessors "${p}.`
159
166
  );
160
167
  }
161
168
  plugin.preprocessors = {};
@@ -165,11 +172,20 @@ export function resolvePlugins(
165
172
  if (pluginModule.preprocessors.oas2) {
166
173
  plugin.preprocessors.oas2 = prefixRules(pluginModule.preprocessors.oas2, id);
167
174
  }
175
+ if (pluginModule.preprocessors.async2) {
176
+ plugin.preprocessors.async2 = prefixRules(pluginModule.preprocessors.async2, id);
177
+ }
168
178
  }
169
179
 
170
180
  if (pluginModule.decorators) {
171
- if (!pluginModule.decorators.oas3 && !pluginModule.decorators.oas2) {
172
- throw new Error(`Plugin \`decorators\` must have \`oas3\` or \`oas2\` decorators "${p}.`);
181
+ if (
182
+ !pluginModule.decorators.oas3 &&
183
+ !pluginModule.decorators.oas2 &&
184
+ !pluginModule.decorators.async2
185
+ ) {
186
+ throw new Error(
187
+ `Plugin \`decorators\` must have \`oas3\`, \`oas2\` or \`async2\` decorators "${p}.`
188
+ );
173
189
  }
174
190
  plugin.decorators = {};
175
191
  if (pluginModule.decorators.oas3) {
@@ -178,6 +194,9 @@ export function resolvePlugins(
178
194
  if (pluginModule.decorators.oas2) {
179
195
  plugin.decorators.oas2 = prefixRules(pluginModule.decorators.oas2, id);
180
196
  }
197
+ if (pluginModule.decorators.async2) {
198
+ plugin.decorators.async2 = prefixRules(pluginModule.decorators.async2, id);
199
+ }
181
200
  }
182
201
 
183
202
  if (pluginModule.assertions) {
@@ -3,7 +3,13 @@ import * as path from 'path';
3
3
  import { parseYaml, stringifyYaml } from '../js-yaml';
4
4
  import { slash, doesYamlFileExist } from '../utils';
5
5
  import { NormalizedProblem } from '../walk';
6
- import { OasVersion, OasMajorVersion, Oas2RuleSet, Oas3RuleSet } from '../oas-types';
6
+ import {
7
+ SpecVersion,
8
+ SpecMajorVersion,
9
+ Oas2RuleSet,
10
+ Oas3RuleSet,
11
+ Async2RuleSet,
12
+ } from '../oas-types';
7
13
  import { isBrowser, env } from '../env';
8
14
 
9
15
  import type { NodeType } from '../types';
@@ -65,12 +71,12 @@ export class StyleguideConfig {
65
71
  plugins: Plugin[];
66
72
  ignore: Record<string, Record<string, Set<string>>> = {};
67
73
  doNotResolveExamples: boolean;
68
- rules: Record<OasVersion, Record<string, RuleConfig>>;
69
- preprocessors: Record<OasVersion, Record<string, PreprocessorConfig>>;
70
- decorators: Record<OasVersion, Record<string, DecoratorConfig>>;
74
+ rules: Record<SpecVersion, Record<string, RuleConfig>>;
75
+ preprocessors: Record<SpecVersion, Record<string, PreprocessorConfig>>;
76
+ decorators: Record<SpecVersion, Record<string, DecoratorConfig>>;
71
77
 
72
78
  private _usedRules: Set<string> = new Set();
73
- private _usedVersions: Set<OasVersion> = new Set();
79
+ private _usedVersions: Set<SpecVersion> = new Set();
74
80
 
75
81
  recommendedFallback: boolean;
76
82
 
@@ -83,21 +89,24 @@ export class StyleguideConfig {
83
89
  this.recommendedFallback = rawConfig.recommendedFallback || false;
84
90
 
85
91
  this.rules = {
86
- [OasVersion.Version2]: { ...rawConfig.rules, ...rawConfig.oas2Rules },
87
- [OasVersion.Version3_0]: { ...rawConfig.rules, ...rawConfig.oas3_0Rules },
88
- [OasVersion.Version3_1]: { ...rawConfig.rules, ...rawConfig.oas3_1Rules },
92
+ [SpecVersion.OAS2]: { ...rawConfig.rules, ...rawConfig.oas2Rules },
93
+ [SpecVersion.OAS3_0]: { ...rawConfig.rules, ...rawConfig.oas3_0Rules },
94
+ [SpecVersion.OAS3_1]: { ...rawConfig.rules, ...rawConfig.oas3_1Rules },
95
+ [SpecVersion.Async2]: { ...rawConfig.rules, ...rawConfig.async2Rules },
89
96
  };
90
97
 
91
98
  this.preprocessors = {
92
- [OasVersion.Version2]: { ...rawConfig.preprocessors, ...rawConfig.oas2Preprocessors },
93
- [OasVersion.Version3_0]: { ...rawConfig.preprocessors, ...rawConfig.oas3_0Preprocessors },
94
- [OasVersion.Version3_1]: { ...rawConfig.preprocessors, ...rawConfig.oas3_1Preprocessors },
99
+ [SpecVersion.OAS2]: { ...rawConfig.preprocessors, ...rawConfig.oas2Preprocessors },
100
+ [SpecVersion.OAS3_0]: { ...rawConfig.preprocessors, ...rawConfig.oas3_0Preprocessors },
101
+ [SpecVersion.OAS3_1]: { ...rawConfig.preprocessors, ...rawConfig.oas3_1Preprocessors },
102
+ [SpecVersion.Async2]: { ...rawConfig.preprocessors, ...rawConfig.async2Preprocessors },
95
103
  };
96
104
 
97
105
  this.decorators = {
98
- [OasVersion.Version2]: { ...rawConfig.decorators, ...rawConfig.oas2Decorators },
99
- [OasVersion.Version3_0]: { ...rawConfig.decorators, ...rawConfig.oas3_0Decorators },
100
- [OasVersion.Version3_1]: { ...rawConfig.decorators, ...rawConfig.oas3_1Decorators },
106
+ [SpecVersion.OAS2]: { ...rawConfig.decorators, ...rawConfig.oas2Decorators },
107
+ [SpecVersion.OAS3_0]: { ...rawConfig.decorators, ...rawConfig.oas3_0Decorators },
108
+ [SpecVersion.OAS3_1]: { ...rawConfig.decorators, ...rawConfig.oas3_1Decorators },
109
+ [SpecVersion.Async2]: { ...rawConfig.decorators, ...rawConfig.async2Decorators },
101
110
  };
102
111
 
103
112
  this.extendPaths = rawConfig.extendPaths || [];
@@ -173,20 +182,24 @@ export class StyleguideConfig {
173
182
  : problem;
174
183
  }
175
184
 
176
- extendTypes(types: Record<string, NodeType>, version: OasVersion) {
185
+ extendTypes(types: Record<string, NodeType>, version: SpecVersion) {
177
186
  let extendedTypes = types;
178
187
  for (const plugin of this.plugins) {
179
188
  if (plugin.typeExtension !== undefined) {
180
189
  switch (version) {
181
- case OasVersion.Version3_0:
182
- case OasVersion.Version3_1:
190
+ case SpecVersion.OAS3_0:
191
+ case SpecVersion.OAS3_1:
183
192
  if (!plugin.typeExtension.oas3) continue;
184
193
  extendedTypes = plugin.typeExtension.oas3(extendedTypes, version);
185
194
  break;
186
- case OasVersion.Version2:
195
+ case SpecVersion.OAS2:
187
196
  if (!plugin.typeExtension.oas2) continue;
188
197
  extendedTypes = plugin.typeExtension.oas2(extendedTypes, version);
189
198
  break;
199
+ case SpecVersion.Async2:
200
+ if (!plugin.typeExtension.async2) continue;
201
+ extendedTypes = plugin.typeExtension.async2(extendedTypes, version);
202
+ break;
190
203
  default:
191
204
  throw new Error('Not implemented');
192
205
  }
@@ -195,7 +208,7 @@ export class StyleguideConfig {
195
208
  return extendedTypes;
196
209
  }
197
210
 
198
- getRuleSettings(ruleId: string, oasVersion: OasVersion): RuleSettings {
211
+ getRuleSettings(ruleId: string, oasVersion: SpecVersion): RuleSettings {
199
212
  this._usedRules.add(ruleId);
200
213
  this._usedVersions.add(oasVersion);
201
214
  const settings = this.rules[oasVersion][ruleId] || 'off';
@@ -208,7 +221,7 @@ export class StyleguideConfig {
208
221
  }
209
222
  }
210
223
 
211
- getPreprocessorSettings(ruleId: string, oasVersion: OasVersion): RuleSettings {
224
+ getPreprocessorSettings(ruleId: string, oasVersion: SpecVersion): RuleSettings {
212
225
  this._usedRules.add(ruleId);
213
226
  this._usedVersions.add(oasVersion);
214
227
 
@@ -222,7 +235,7 @@ export class StyleguideConfig {
222
235
  }
223
236
  }
224
237
 
225
- getDecoratorSettings(ruleId: string, oasVersion: OasVersion): RuleSettings {
238
+ getDecoratorSettings(ruleId: string, oasVersion: SpecVersion): RuleSettings {
226
239
  this._usedRules.add(ruleId);
227
240
  this._usedVersions.add(oasVersion);
228
241
  const settings = this.decorators[oasVersion][ruleId] || 'off';
@@ -259,28 +272,39 @@ export class StyleguideConfig {
259
272
  };
260
273
  }
261
274
 
262
- getRulesForOasVersion(version: OasMajorVersion) {
275
+ getRulesForOasVersion(version: SpecMajorVersion) {
263
276
  switch (version) {
264
- case OasMajorVersion.Version3:
277
+ case SpecMajorVersion.OAS3:
265
278
  // eslint-disable-next-line no-case-declarations
266
279
  const oas3Rules: Oas3RuleSet[] = []; // default ruleset
267
280
  this.plugins.forEach((p) => p.preprocessors?.oas3 && oas3Rules.push(p.preprocessors.oas3));
268
281
  this.plugins.forEach((p) => p.rules?.oas3 && oas3Rules.push(p.rules.oas3));
269
282
  this.plugins.forEach((p) => p.decorators?.oas3 && oas3Rules.push(p.decorators.oas3));
270
283
  return oas3Rules;
271
- case OasMajorVersion.Version2:
284
+ case SpecMajorVersion.OAS2:
272
285
  // eslint-disable-next-line no-case-declarations
273
286
  const oas2Rules: Oas2RuleSet[] = []; // default ruleset
274
287
  this.plugins.forEach((p) => p.preprocessors?.oas2 && oas2Rules.push(p.preprocessors.oas2));
275
288
  this.plugins.forEach((p) => p.rules?.oas2 && oas2Rules.push(p.rules.oas2));
276
289
  this.plugins.forEach((p) => p.decorators?.oas2 && oas2Rules.push(p.decorators.oas2));
277
290
  return oas2Rules;
291
+ case SpecMajorVersion.Async2:
292
+ // eslint-disable-next-line no-case-declarations
293
+ const asyncApiRules: Async2RuleSet[] = []; // default ruleset
294
+ this.plugins.forEach(
295
+ (p) => p.preprocessors?.async2 && asyncApiRules.push(p.preprocessors.async2)
296
+ );
297
+ this.plugins.forEach((p) => p.rules?.async2 && asyncApiRules.push(p.rules.async2));
298
+ this.plugins.forEach(
299
+ (p) => p.decorators?.async2 && asyncApiRules.push(p.decorators.async2)
300
+ );
301
+ return asyncApiRules;
278
302
  }
279
303
  }
280
304
 
281
305
  skipRules(rules?: string[]) {
282
306
  for (const ruleId of rules || []) {
283
- for (const version of Object.values(OasVersion)) {
307
+ for (const version of Object.values(SpecVersion)) {
284
308
  if (this.rules[version][ruleId]) {
285
309
  this.rules[version][ruleId] = 'off';
286
310
  }
@@ -290,7 +314,7 @@ export class StyleguideConfig {
290
314
 
291
315
  skipPreprocessors(preprocessors?: string[]) {
292
316
  for (const preprocessorId of preprocessors || []) {
293
- for (const version of Object.values(OasVersion)) {
317
+ for (const version of Object.values(SpecVersion)) {
294
318
  if (this.preprocessors[version][preprocessorId]) {
295
319
  this.preprocessors[version][preprocessorId] = 'off';
296
320
  }
@@ -300,7 +324,7 @@ export class StyleguideConfig {
300
324
 
301
325
  skipDecorators(decorators?: string[]) {
302
326
  for (const decoratorId of decorators || []) {
303
- for (const version of Object.values(OasVersion)) {
327
+ for (const version of Object.values(SpecVersion)) {
304
328
  if (this.decorators[version][decoratorId]) {
305
329
  this.decorators[version][decoratorId] = 'off';
306
330
  }
@@ -7,7 +7,13 @@ import { Config, DOMAINS } from './config';
7
7
  import { transformConfig } from './utils';
8
8
  import { resolveConfig } from './config-resolvers';
9
9
 
10
- import type { DeprecatedInRawConfig, FlatRawConfig, RawConfig, Region } from './types';
10
+ import type {
11
+ DeprecatedInRawConfig,
12
+ FlatRawConfig,
13
+ RawConfig,
14
+ RawUniversalConfig,
15
+ Region,
16
+ } from './types';
11
17
  import { RegionalTokenWithValidity } from '../redocly/redocly-client-types';
12
18
 
13
19
  async function addConfigMetadata({
@@ -101,8 +107,8 @@ export function findConfig(dir?: string): string | undefined {
101
107
  ).filter(fs.existsSync);
102
108
  if (existingConfigFiles.length > 1) {
103
109
  throw new Error(`
104
- Multiple configuration files are not allowed.
105
- Found the following files: ${existingConfigFiles.join(', ')}.
110
+ Multiple configuration files are not allowed.
111
+ Found the following files: ${existingConfigFiles.join(', ')}.
106
112
  Please use 'redocly.yaml' instead.
107
113
  `);
108
114
  }
@@ -129,10 +135,11 @@ export async function getConfig(
129
135
  type CreateConfigOptions = {
130
136
  extends?: string[];
131
137
  tokens?: RegionalTokenWithValidity[];
138
+ configPath?: string;
132
139
  };
133
140
 
134
141
  export async function createConfig(
135
- config: string | RawConfig,
142
+ config: string | RawUniversalConfig,
136
143
  options?: CreateConfigOptions
137
144
  ): Promise<Config> {
138
145
  return addConfigMetadata({
@@ -1,4 +1,4 @@
1
- import { RuleSet, OasVersion } from '../oas-types';
1
+ import { RuleSet, SpecVersion } from '../oas-types';
2
2
  import { StyleguideConfig } from './config';
3
3
  import { isDefined } from '../utils';
4
4
  import type { ProblemSeverity } from '../walk';
@@ -13,7 +13,7 @@ export function initRules<T extends Function, P extends RuleSet<T>>(
13
13
  rules: P[],
14
14
  config: StyleguideConfig,
15
15
  type: 'rules' | 'preprocessors' | 'decorators',
16
- oasVersion: OasVersion
16
+ oasVersion: SpecVersion
17
17
  ): InitializedRule[] {
18
18
  return rules
19
19
  .flatMap((ruleset) =>
@@ -1,14 +1,18 @@
1
1
  import type { ProblemSeverity, UserContext } from '../walk';
2
2
  import type {
3
3
  Oas3PreprocessorsSet,
4
- OasMajorVersion,
4
+ SpecMajorVersion,
5
5
  Oas3DecoratorsSet,
6
6
  Oas2RuleSet,
7
7
  Oas2PreprocessorsSet,
8
8
  Oas2DecoratorsSet,
9
9
  Oas3RuleSet,
10
- OasVersion,
10
+ SpecVersion,
11
+ Async2PreprocessorsSet,
12
+ Async2DecoratorsSet,
13
+ Async2RuleSet,
11
14
  } from '../oas-types';
15
+
12
16
  import type { NodeType } from '../types';
13
17
  import { Location } from '../ref-utils';
14
18
  import type { SkipFunctionContext } from '../visitors';
@@ -43,16 +47,19 @@ export type StyleguideRawConfig = {
43
47
  oas2Rules?: Record<string, RuleConfig>;
44
48
  oas3_0Rules?: Record<string, RuleConfig>;
45
49
  oas3_1Rules?: Record<string, RuleConfig>;
50
+ async2Rules?: Record<string, RuleConfig>;
46
51
 
47
52
  preprocessors?: Record<string, PreprocessorConfig>;
48
53
  oas2Preprocessors?: Record<string, PreprocessorConfig>;
49
54
  oas3_0Preprocessors?: Record<string, PreprocessorConfig>;
50
55
  oas3_1Preprocessors?: Record<string, PreprocessorConfig>;
56
+ async2Preprocessors?: Record<string, PreprocessorConfig>;
51
57
 
52
58
  decorators?: Record<string, DecoratorConfig>;
53
59
  oas2Decorators?: Record<string, DecoratorConfig>;
54
60
  oas3_0Decorators?: Record<string, DecoratorConfig>;
55
61
  oas3_1Decorators?: Record<string, DecoratorConfig>;
62
+ async2Decorators?: Record<string, DecoratorConfig>;
56
63
  };
57
64
 
58
65
  export type ApiStyleguideRawConfig = Omit<StyleguideRawConfig, 'plugins'>;
@@ -68,23 +75,26 @@ export type ResolvedStyleguideConfig = PluginStyleguideConfig & {
68
75
  export type PreprocessorsConfig = {
69
76
  oas3?: Oas3PreprocessorsSet;
70
77
  oas2?: Oas2PreprocessorsSet;
78
+ async2?: Async2PreprocessorsSet;
71
79
  };
72
80
 
73
81
  export type DecoratorsConfig = {
74
82
  oas3?: Oas3DecoratorsSet;
75
83
  oas2?: Oas2DecoratorsSet;
84
+ async2?: Async2DecoratorsSet;
76
85
  };
77
86
 
78
87
  export type TypesExtensionFn = (
79
88
  types: Record<string, NodeType>,
80
- oasVersion: OasVersion
89
+ oasVersion: SpecVersion
81
90
  ) => Record<string, NodeType>;
82
91
 
83
- export type TypeExtensionsConfig = Partial<Record<OasMajorVersion, TypesExtensionFn>>;
92
+ export type TypeExtensionsConfig = Partial<Record<SpecMajorVersion, TypesExtensionFn>>;
84
93
 
85
94
  export type CustomRulesConfig = {
86
95
  oas3?: Oas3RuleSet;
87
96
  oas2?: Oas2RuleSet;
97
+ async2?: Async2RuleSet;
88
98
  };
89
99
 
90
100
  export type AssertionContext = Partial<UserContext> & SkipFunctionContext & { node: any };
@@ -175,6 +185,9 @@ export type RawConfig = {
175
185
  telemetry?: Telemetry;
176
186
  } & ThemeConfig;
177
187
 
188
+ // RawConfig is legacy, use RawUniversalConfig in public APIs
189
+ export type RawUniversalConfig = Omit<RawConfig, 'styleguide'> & StyleguideRawConfig;
190
+
178
191
  export type FlatApi = Omit<Api, 'styleguide'> &
179
192
  Omit<ApiStyleguideRawConfig, 'doNotResolveExamples'>;
180
193
 
package/src/index.ts CHANGED
@@ -2,6 +2,7 @@ export { BundleOutputFormat, readFileFromUrl, slash, doesYamlFileExist, isTruthy
2
2
  export { Oas3_1Types } from './types/oas3_1';
3
3
  export { Oas3Types } from './types/oas3';
4
4
  export { Oas2Types } from './types/oas2';
5
+ export { AsyncApi2Types } from './types/asyncapi';
5
6
  export { ConfigTypes } from './types/redocly-yaml';
6
7
  export type {
7
8
  Oas3Definition,
@@ -26,6 +27,7 @@ export {
26
27
  Config,
27
28
  StyleguideConfig,
28
29
  RawConfig,
30
+ RawUniversalConfig,
29
31
  IGNORE_FILE,
30
32
  Region,
31
33
  getMergedConfig,
@@ -52,7 +54,13 @@ export {
52
54
  } from './resolve';
53
55
  export { parseYaml, stringifyYaml } from './js-yaml';
54
56
  export { unescapePointer, isRef, isAbsoluteUrl } from './ref-utils';
55
- export { detectOpenAPI, OasMajorVersion, openAPIMajor, OasVersion } from './oas-types';
57
+ export {
58
+ SpecMajorVersion,
59
+ getMajorSpecVersion,
60
+ SpecVersion,
61
+ detectSpec,
62
+ getTypes,
63
+ } from './oas-types';
56
64
  export { normalizeVisitors } from './visitors';
57
65
 
58
66
  export {
@@ -68,4 +76,4 @@ export {
68
76
  export { getAstNodeByPointer, getLineColLocation } from './format/codeframes';
69
77
  export { formatProblems, OutputFormat, getTotals, Totals } from './format/format';
70
78
  export { lint, lint as validate, lintDocument, lintFromString, lintConfig } from './lint';
71
- export { bundle, bundleDocument, mapTypeToComponent } from './bundle';
79
+ export { bundle, bundleDocument, mapTypeToComponent, bundleFromString } from './bundle';
package/src/lint.ts CHANGED
@@ -1,16 +1,13 @@
1
1
  import { BaseResolver, resolveDocument, Document, makeDocumentFromString } from './resolve';
2
2
  import { normalizeVisitors } from './visitors';
3
- import { Oas3_1Types } from './types/oas3_1';
4
- import { Oas3Types } from './types/oas3';
5
- import { Oas2Types } from './types/oas2';
6
3
  import { NodeType } from './types';
7
4
  import { ProblemSeverity, WalkContext, walkDocument } from './walk';
8
5
  import { StyleguideConfig, Config, initRules, defaultPlugin, resolvePlugins } from './config';
9
6
  import { normalizeTypes } from './types';
10
7
  import { releaseAjvInstance } from './rules/ajv';
11
- import { detectOpenAPI, Oas3RuleSet, OasMajorVersion, OasVersion, openAPIMajor } from './oas-types';
8
+ import { Oas3RuleSet, SpecVersion, getMajorSpecVersion, detectSpec, getTypes } from './oas-types';
12
9
  import { ConfigTypes } from './types/redocly-yaml';
13
- import { OasSpec } from './rules/common/spec';
10
+ import { Spec } from './rules/common/spec';
14
11
 
15
12
  export async function lint(opts: {
16
13
  ref: string;
@@ -54,29 +51,22 @@ export async function lintDocument(opts: {
54
51
  releaseAjvInstance(); // FIXME: preprocessors can modify nodes which are then cached to ajv-instance by absolute path
55
52
 
56
53
  const { document, customTypes, externalRefResolver, config } = opts;
57
- const oasVersion = detectOpenAPI(document.parsed);
58
- const oasMajorVersion = openAPIMajor(oasVersion);
59
- const rules = config.getRulesForOasVersion(oasMajorVersion);
54
+ const specVersion = detectSpec(document.parsed);
55
+ const specMajorVersion = getMajorSpecVersion(specVersion);
56
+ const rules = config.getRulesForOasVersion(specMajorVersion);
60
57
  const types = normalizeTypes(
61
- config.extendTypes(
62
- customTypes ?? oasMajorVersion === OasMajorVersion.Version3
63
- ? oasVersion === OasVersion.Version3_1
64
- ? Oas3_1Types
65
- : Oas3Types
66
- : Oas2Types,
67
- oasVersion
68
- ),
58
+ config.extendTypes(customTypes ?? getTypes(specVersion), specVersion),
69
59
  config
70
60
  );
71
61
 
72
62
  const ctx: WalkContext = {
73
63
  problems: [],
74
- oasVersion: oasVersion,
64
+ oasVersion: specVersion,
75
65
  visitorsData: {},
76
66
  };
77
67
 
78
- const preprocessors = initRules(rules as any, config, 'preprocessors', oasVersion);
79
- const regularRules = initRules(rules as Oas3RuleSet[], config, 'rules', oasVersion);
68
+ const preprocessors = initRules(rules as any, config, 'preprocessors', specVersion);
69
+ const regularRules = initRules(rules as Oas3RuleSet[], config, 'rules', specVersion);
80
70
 
81
71
  let resolvedRefMap = await resolveDocument({
82
72
  rootDocument: document,
@@ -117,7 +107,7 @@ export async function lintConfig(opts: { document: Document; severity?: ProblemS
117
107
 
118
108
  const ctx: WalkContext = {
119
109
  problems: [],
120
- oasVersion: OasVersion.Version3_0,
110
+ oasVersion: SpecVersion.OAS3_0,
121
111
  visitorsData: {},
122
112
  };
123
113
  const plugins = resolvePlugins([defaultPlugin]);
@@ -131,10 +121,11 @@ export async function lintConfig(opts: { document: Document; severity?: ProblemS
131
121
  {
132
122
  severity: severity || 'error',
133
123
  ruleId: 'configuration spec',
134
- visitor: OasSpec({ severity: 'error' }),
124
+ visitor: Spec({ severity: 'error' }),
135
125
  },
136
126
  ];
137
- const normalizedVisitors = normalizeVisitors(rules, types);
127
+ // TODO: check why any is needed
128
+ const normalizedVisitors = normalizeVisitors(rules as any, types);
138
129
 
139
130
  walkDocument({
140
131
  document,