@redocly/openapi-core
Advanced tools
Comparing version 1.1.0 to 1.2.0
# @redocly/openapi-core | ||
## 1.2.0 | ||
### Minor Changes | ||
- Added support for linting AsyncAPI v2 files, so that a wider range of API descriptions can use the Redocly linting workflow. | ||
### Patch Changes | ||
- Renamed API definition to API description for consistency. | ||
## 1.1.0 | ||
@@ -4,0 +14,0 @@ |
@@ -5,3 +5,3 @@ import { BaseResolver, Document } from './resolve'; | ||
import { NormalizedProblem } from './walk'; | ||
import { OasMajorVersion } from './oas-types'; | ||
import { SpecMajorVersion } from './oas-types'; | ||
import type { Config, StyleguideConfig } from './config'; | ||
@@ -43,2 +43,2 @@ export declare type Oas3RuleSet = Record<string, Oas3Rule>; | ||
}): Promise<BundleResult>; | ||
export declare function mapTypeToComponent(typeName: string, version: OasMajorVersion): "parameters" | "examples" | "headers" | "schemas" | "responses" | "requestBodies" | "securitySchemes" | "links" | "callbacks" | "definitions" | null; | ||
export declare function mapTypeToComponent(typeName: string, version: SpecMajorVersion): "responses" | "parameters" | "examples" | "headers" | "schemas" | "requestBodies" | "securitySchemes" | "links" | "callbacks" | "definitions" | null; |
@@ -16,5 +16,2 @@ "use strict"; | ||
const visitors_1 = require("./visitors"); | ||
const oas3_1 = require("./types/oas3"); | ||
const oas2_1 = require("./types/oas2"); | ||
const oas3_1_1 = require("./types/oas3_1"); | ||
const types_1 = require("./types"); | ||
@@ -53,15 +50,11 @@ const walk_1 = require("./walk"); | ||
const { document, config, customTypes, externalRefResolver, dereference = false, skipRedoclyRegistryRefs = false, removeUnusedComponents = false, keepUrlRefs = false, } = opts; | ||
const oasVersion = oas_types_1.detectOpenAPI(document.parsed); | ||
const oasMajorVersion = oas_types_1.openAPIMajor(oasVersion); | ||
const rules = config.getRulesForOasVersion(oasMajorVersion); | ||
const types = types_1.normalizeTypes(config.extendTypes((customTypes !== null && customTypes !== void 0 ? customTypes : oasMajorVersion === oas_types_1.OasMajorVersion.Version3) | ||
? oasVersion === OasVersion.Version3_1 | ||
? oas3_1_1.Oas3_1Types | ||
: oas3_1.Oas3Types | ||
: oas2_1.Oas2Types, oasVersion), config); | ||
const preprocessors = rules_1.initRules(rules, config, 'preprocessors', oasVersion); | ||
const decorators = rules_1.initRules(rules, config, 'decorators', oasVersion); | ||
const specVersion = oas_types_1.detectSpec(document.parsed); | ||
const specMajorVersion = oas_types_1.getMajorSpecVersion(specVersion); | ||
const rules = config.getRulesForOasVersion(specMajorVersion); | ||
const types = types_1.normalizeTypes(config.extendTypes(customTypes !== null && customTypes !== void 0 ? customTypes : oas_types_1.getTypes(specVersion), specVersion), config); | ||
const preprocessors = rules_1.initRules(rules, config, 'preprocessors', specVersion); | ||
const decorators = rules_1.initRules(rules, config, 'decorators', specVersion); | ||
const ctx = { | ||
problems: [], | ||
oasVersion: oasVersion, | ||
oasVersion: specVersion, | ||
refTypes: new Map(), | ||
@@ -74,3 +67,3 @@ visitorsData: {}, | ||
ruleId: 'remove-unused-components', | ||
visitor: oasMajorVersion === oas_types_1.OasMajorVersion.Version2 | ||
visitor: specMajorVersion === oas_types_1.SpecMajorVersion.OAS2 | ||
? remove_unused_components_1.RemoveUnusedComponents({}) | ||
@@ -104,3 +97,3 @@ : remove_unused_components_2.RemoveUnusedComponents({}), | ||
ruleId: 'bundler', | ||
visitor: makeBundleVisitor(oasMajorVersion, dereference, skipRedoclyRegistryRefs, document, resolvedRefMap, keepUrlRefs), | ||
visitor: makeBundleVisitor(specMajorVersion, dereference, skipRedoclyRegistryRefs, document, resolvedRefMap, keepUrlRefs), | ||
}, | ||
@@ -129,3 +122,3 @@ ...decorators, | ||
switch (version) { | ||
case oas_types_1.OasMajorVersion.Version3: | ||
case oas_types_1.SpecMajorVersion.OAS3: | ||
switch (typeName) { | ||
@@ -153,3 +146,3 @@ case 'Schema': | ||
} | ||
case oas_types_1.OasMajorVersion.Version2: | ||
case oas_types_1.SpecMajorVersion.OAS2: | ||
switch (typeName) { | ||
@@ -165,2 +158,11 @@ case 'Schema': | ||
} | ||
case oas_types_1.SpecMajorVersion.Async2: | ||
switch (typeName) { | ||
case 'Schema': | ||
return 'schemas'; | ||
case 'Parameter': | ||
return 'parameters'; | ||
default: | ||
return null; | ||
} | ||
} | ||
@@ -212,6 +214,6 @@ } | ||
rootLocation = ctx.location; | ||
if (version === oas_types_1.OasMajorVersion.Version3) { | ||
if (version === oas_types_1.SpecMajorVersion.OAS3) { | ||
components = root.components = root.components || {}; | ||
} | ||
else if (version === oas_types_1.OasMajorVersion.Version2) { | ||
else if (version === oas_types_1.SpecMajorVersion.OAS2) { | ||
components = root; | ||
@@ -222,3 +224,3 @@ } | ||
}; | ||
if (version === oas_types_1.OasMajorVersion.Version3) { | ||
if (version === oas_types_1.SpecMajorVersion.OAS3) { | ||
visitor.DiscriminatorMapping = { | ||
@@ -271,3 +273,3 @@ leave(mapping, ctx) { | ||
components[componentType][name] = target.node; | ||
if (version === oas_types_1.OasMajorVersion.Version3) { | ||
if (version === oas_types_1.SpecMajorVersion.OAS3) { | ||
return `#/components/${componentType}/${name}`; | ||
@@ -274,0 +276,0 @@ } |
@@ -9,2 +9,3 @@ "use strict"; | ||
const oas2_1 = require("../rules/oas2"); | ||
const async2_1 = require("../rules/async2"); | ||
const oas3_2 = require("../rules/oas3"); | ||
@@ -27,2 +28,3 @@ const oas2_2 = require("../rules/oas2"); | ||
oas2: oas2_1.rules, | ||
async2: async2_1.rules, | ||
}, | ||
@@ -32,2 +34,3 @@ preprocessors: { | ||
oas2: oas2_2.preprocessors, | ||
async2: {}, | ||
}, | ||
@@ -37,4 +40,5 @@ decorators: { | ||
oas2: oas2_3.decorators, | ||
async2: {}, | ||
}, | ||
configs: exports.builtInConfigs, | ||
}; |
@@ -108,4 +108,4 @@ "use strict"; | ||
if (pluginModule.rules) { | ||
if (!pluginModule.rules.oas3 && !pluginModule.rules.oas2) { | ||
throw new Error(`Plugin rules must have \`oas3\` or \`oas2\` rules "${p}.`); | ||
if (!pluginModule.rules.oas3 && !pluginModule.rules.oas2 && !pluginModule.rules.async2) { | ||
throw new Error(`Plugin rules must have \`oas3\`, \`oas2\` or \`async2\` rules "${p}.`); | ||
} | ||
@@ -119,6 +119,11 @@ plugin.rules = {}; | ||
} | ||
if (pluginModule.rules.async2) { | ||
plugin.rules.async2 = utils_2.prefixRules(pluginModule.rules.async2, id); | ||
} | ||
} | ||
if (pluginModule.preprocessors) { | ||
if (!pluginModule.preprocessors.oas3 && !pluginModule.preprocessors.oas2) { | ||
throw new Error(`Plugin \`preprocessors\` must have \`oas3\` or \`oas2\` preprocessors "${p}.`); | ||
if (!pluginModule.preprocessors.oas3 && | ||
!pluginModule.preprocessors.oas2 && | ||
!pluginModule.preprocessors.async2) { | ||
throw new Error(`Plugin \`preprocessors\` must have \`oas3\`, \`oas2\` or \`async2\` preprocessors "${p}.`); | ||
} | ||
@@ -132,6 +137,11 @@ plugin.preprocessors = {}; | ||
} | ||
if (pluginModule.preprocessors.async2) { | ||
plugin.preprocessors.async2 = utils_2.prefixRules(pluginModule.preprocessors.async2, id); | ||
} | ||
} | ||
if (pluginModule.decorators) { | ||
if (!pluginModule.decorators.oas3 && !pluginModule.decorators.oas2) { | ||
throw new Error(`Plugin \`decorators\` must have \`oas3\` or \`oas2\` decorators "${p}.`); | ||
if (!pluginModule.decorators.oas3 && | ||
!pluginModule.decorators.oas2 && | ||
!pluginModule.decorators.async2) { | ||
throw new Error(`Plugin \`decorators\` must have \`oas3\`, \`oas2\` or \`async2\` decorators "${p}.`); | ||
} | ||
@@ -145,2 +155,5 @@ plugin.decorators = {}; | ||
} | ||
if (pluginModule.decorators.async2) { | ||
plugin.decorators.async2 = utils_2.prefixRules(pluginModule.decorators.async2, id); | ||
} | ||
} | ||
@@ -147,0 +160,0 @@ if (pluginModule.assertions) { |
import { NormalizedProblem } from '../walk'; | ||
import { OasVersion, OasMajorVersion, Oas2RuleSet, Oas3RuleSet } from '../oas-types'; | ||
import { SpecVersion, SpecMajorVersion, Oas2RuleSet, Oas3RuleSet, Async2RuleSet } from '../oas-types'; | ||
import type { NodeType } from '../types'; | ||
@@ -18,5 +18,5 @@ import type { DecoratorConfig, Plugin, PreprocessorConfig, Region, ResolveConfig, ResolvedApi, ResolvedConfig, ResolvedStyleguideConfig, RuleConfig, RuleSettings, Telemetry, ThemeRawConfig } from './types'; | ||
doNotResolveExamples: boolean; | ||
rules: Record<OasVersion, Record<string, RuleConfig>>; | ||
preprocessors: Record<OasVersion, Record<string, PreprocessorConfig>>; | ||
decorators: Record<OasVersion, Record<string, DecoratorConfig>>; | ||
rules: Record<SpecVersion, Record<string, RuleConfig>>; | ||
preprocessors: Record<SpecVersion, Record<string, PreprocessorConfig>>; | ||
decorators: Record<SpecVersion, Record<string, DecoratorConfig>>; | ||
private _usedRules; | ||
@@ -32,6 +32,6 @@ private _usedVersions; | ||
addProblemToIgnore(problem: NormalizedProblem): NormalizedProblem; | ||
extendTypes(types: Record<string, NodeType>, version: OasVersion): Record<string, NodeType>; | ||
getRuleSettings(ruleId: string, oasVersion: OasVersion): RuleSettings; | ||
getPreprocessorSettings(ruleId: string, oasVersion: OasVersion): RuleSettings; | ||
getDecoratorSettings(ruleId: string, oasVersion: OasVersion): RuleSettings; | ||
extendTypes(types: Record<string, NodeType>, version: SpecVersion): Record<string, NodeType>; | ||
getRuleSettings(ruleId: string, oasVersion: SpecVersion): RuleSettings; | ||
getPreprocessorSettings(ruleId: string, oasVersion: SpecVersion): RuleSettings; | ||
getDecoratorSettings(ruleId: string, oasVersion: SpecVersion): RuleSettings; | ||
getUnusedRules(): { | ||
@@ -42,3 +42,3 @@ rules: string[]; | ||
}; | ||
getRulesForOasVersion(version: OasMajorVersion): Oas3RuleSet[] | Oas2RuleSet[]; | ||
getRulesForOasVersion(version: SpecMajorVersion): Oas3RuleSet[] | Oas2RuleSet[] | Async2RuleSet[]; | ||
skipRules(rules?: string[]): void; | ||
@@ -45,0 +45,0 @@ skipPreprocessors(preprocessors?: string[]): void; |
@@ -54,15 +54,18 @@ "use strict"; | ||
this.rules = { | ||
[oas_types_1.OasVersion.Version2]: Object.assign(Object.assign({}, rawConfig.rules), rawConfig.oas2Rules), | ||
[oas_types_1.OasVersion.Version3_0]: Object.assign(Object.assign({}, rawConfig.rules), rawConfig.oas3_0Rules), | ||
[oas_types_1.OasVersion.Version3_1]: Object.assign(Object.assign({}, rawConfig.rules), rawConfig.oas3_1Rules), | ||
[oas_types_1.SpecVersion.OAS2]: Object.assign(Object.assign({}, rawConfig.rules), rawConfig.oas2Rules), | ||
[oas_types_1.SpecVersion.OAS3_0]: Object.assign(Object.assign({}, rawConfig.rules), rawConfig.oas3_0Rules), | ||
[oas_types_1.SpecVersion.OAS3_1]: Object.assign(Object.assign({}, rawConfig.rules), rawConfig.oas3_1Rules), | ||
[oas_types_1.SpecVersion.Async2]: Object.assign(Object.assign({}, rawConfig.rules), rawConfig.async2Rules), | ||
}; | ||
this.preprocessors = { | ||
[oas_types_1.OasVersion.Version2]: Object.assign(Object.assign({}, rawConfig.preprocessors), rawConfig.oas2Preprocessors), | ||
[oas_types_1.OasVersion.Version3_0]: Object.assign(Object.assign({}, rawConfig.preprocessors), rawConfig.oas3_0Preprocessors), | ||
[oas_types_1.OasVersion.Version3_1]: Object.assign(Object.assign({}, rawConfig.preprocessors), rawConfig.oas3_1Preprocessors), | ||
[oas_types_1.SpecVersion.OAS2]: Object.assign(Object.assign({}, rawConfig.preprocessors), rawConfig.oas2Preprocessors), | ||
[oas_types_1.SpecVersion.OAS3_0]: Object.assign(Object.assign({}, rawConfig.preprocessors), rawConfig.oas3_0Preprocessors), | ||
[oas_types_1.SpecVersion.OAS3_1]: Object.assign(Object.assign({}, rawConfig.preprocessors), rawConfig.oas3_1Preprocessors), | ||
[oas_types_1.SpecVersion.Async2]: Object.assign(Object.assign({}, rawConfig.preprocessors), rawConfig.async2Preprocessors), | ||
}; | ||
this.decorators = { | ||
[oas_types_1.OasVersion.Version2]: Object.assign(Object.assign({}, rawConfig.decorators), rawConfig.oas2Decorators), | ||
[oas_types_1.OasVersion.Version3_0]: Object.assign(Object.assign({}, rawConfig.decorators), rawConfig.oas3_0Decorators), | ||
[oas_types_1.OasVersion.Version3_1]: Object.assign(Object.assign({}, rawConfig.decorators), rawConfig.oas3_1Decorators), | ||
[oas_types_1.SpecVersion.OAS2]: Object.assign(Object.assign({}, rawConfig.decorators), rawConfig.oas2Decorators), | ||
[oas_types_1.SpecVersion.OAS3_0]: Object.assign(Object.assign({}, rawConfig.decorators), rawConfig.oas3_0Decorators), | ||
[oas_types_1.SpecVersion.OAS3_1]: Object.assign(Object.assign({}, rawConfig.decorators), rawConfig.oas3_1Decorators), | ||
[oas_types_1.SpecVersion.Async2]: Object.assign(Object.assign({}, rawConfig.decorators), rawConfig.async2Decorators), | ||
}; | ||
@@ -128,4 +131,4 @@ this.extendPaths = rawConfig.extendPaths || []; | ||
switch (version) { | ||
case oas_types_1.OasVersion.Version3_0: | ||
case oas_types_1.OasVersion.Version3_1: | ||
case oas_types_1.SpecVersion.OAS3_0: | ||
case oas_types_1.SpecVersion.OAS3_1: | ||
if (!plugin.typeExtension.oas3) | ||
@@ -135,3 +138,3 @@ continue; | ||
break; | ||
case oas_types_1.OasVersion.Version2: | ||
case oas_types_1.SpecVersion.OAS2: | ||
if (!plugin.typeExtension.oas2) | ||
@@ -141,2 +144,7 @@ continue; | ||
break; | ||
case oas_types_1.SpecVersion.Async2: | ||
if (!plugin.typeExtension.async2) | ||
continue; | ||
extendedTypes = plugin.typeExtension.async2(extendedTypes, version); | ||
break; | ||
default: | ||
@@ -205,3 +213,3 @@ throw new Error('Not implemented'); | ||
switch (version) { | ||
case oas_types_1.OasMajorVersion.Version3: | ||
case oas_types_1.SpecMajorVersion.OAS3: | ||
// eslint-disable-next-line no-case-declarations | ||
@@ -213,3 +221,3 @@ const oas3Rules = []; // default ruleset | ||
return oas3Rules; | ||
case oas_types_1.OasMajorVersion.Version2: | ||
case oas_types_1.SpecMajorVersion.OAS2: | ||
// eslint-disable-next-line no-case-declarations | ||
@@ -221,2 +229,9 @@ const oas2Rules = []; // default ruleset | ||
return oas2Rules; | ||
case oas_types_1.SpecMajorVersion.Async2: | ||
// eslint-disable-next-line no-case-declarations | ||
const asyncApiRules = []; // default ruleset | ||
this.plugins.forEach((p) => { var _a; return ((_a = p.preprocessors) === null || _a === void 0 ? void 0 : _a.async2) && asyncApiRules.push(p.preprocessors.async2); }); | ||
this.plugins.forEach((p) => { var _a; return ((_a = p.rules) === null || _a === void 0 ? void 0 : _a.async2) && asyncApiRules.push(p.rules.async2); }); | ||
this.plugins.forEach((p) => { var _a; return ((_a = p.decorators) === null || _a === void 0 ? void 0 : _a.async2) && asyncApiRules.push(p.decorators.async2); }); | ||
return asyncApiRules; | ||
} | ||
@@ -226,3 +241,3 @@ } | ||
for (const ruleId of rules || []) { | ||
for (const version of Object.values(oas_types_1.OasVersion)) { | ||
for (const version of Object.values(oas_types_1.SpecVersion)) { | ||
if (this.rules[version][ruleId]) { | ||
@@ -236,3 +251,3 @@ this.rules[version][ruleId] = 'off'; | ||
for (const preprocessorId of preprocessors || []) { | ||
for (const version of Object.values(oas_types_1.OasVersion)) { | ||
for (const version of Object.values(oas_types_1.SpecVersion)) { | ||
if (this.preprocessors[version][preprocessorId]) { | ||
@@ -246,3 +261,3 @@ this.preprocessors[version][preprocessorId] = 'off'; | ||
for (const decoratorId of decorators || []) { | ||
for (const version of Object.values(oas_types_1.OasVersion)) { | ||
for (const version of Object.values(oas_types_1.SpecVersion)) { | ||
if (this.decorators[version][decoratorId]) { | ||
@@ -249,0 +264,0 @@ this.decorators[version][decoratorId] = 'off'; |
@@ -1,2 +0,2 @@ | ||
import { RuleSet, OasVersion } from '../oas-types'; | ||
import { RuleSet, SpecVersion } from '../oas-types'; | ||
import { StyleguideConfig } from './config'; | ||
@@ -9,3 +9,3 @@ import type { ProblemSeverity } from '../walk'; | ||
}; | ||
export declare function initRules<T extends Function, P extends RuleSet<T>>(rules: P[], config: StyleguideConfig, type: 'rules' | 'preprocessors' | 'decorators', oasVersion: OasVersion): InitializedRule[]; | ||
export declare function initRules<T extends Function, P extends RuleSet<T>>(rules: P[], config: StyleguideConfig, type: 'rules' | 'preprocessors' | 'decorators', oasVersion: SpecVersion): InitializedRule[]; | ||
export {}; |
import type { ProblemSeverity, UserContext } from '../walk'; | ||
import type { Oas3PreprocessorsSet, OasMajorVersion, Oas3DecoratorsSet, Oas2RuleSet, Oas2PreprocessorsSet, Oas2DecoratorsSet, Oas3RuleSet, OasVersion } from '../oas-types'; | ||
import type { Oas3PreprocessorsSet, SpecMajorVersion, Oas3DecoratorsSet, Oas2RuleSet, Oas2PreprocessorsSet, Oas2DecoratorsSet, Oas3RuleSet, SpecVersion, Async2PreprocessorsSet, Async2DecoratorsSet, Async2RuleSet } from '../oas-types'; | ||
import type { NodeType } from '../types'; | ||
@@ -27,2 +27,3 @@ import { Location } from '../ref-utils'; | ||
oas3_1Rules?: Record<string, RuleConfig>; | ||
async2Rules?: Record<string, RuleConfig>; | ||
preprocessors?: Record<string, PreprocessorConfig>; | ||
@@ -32,2 +33,3 @@ oas2Preprocessors?: Record<string, PreprocessorConfig>; | ||
oas3_1Preprocessors?: Record<string, PreprocessorConfig>; | ||
async2Preprocessors?: Record<string, PreprocessorConfig>; | ||
decorators?: Record<string, DecoratorConfig>; | ||
@@ -37,2 +39,3 @@ oas2Decorators?: Record<string, DecoratorConfig>; | ||
oas3_1Decorators?: Record<string, DecoratorConfig>; | ||
async2Decorators?: Record<string, DecoratorConfig>; | ||
}; | ||
@@ -50,2 +53,3 @@ export declare type ApiStyleguideRawConfig = Omit<StyleguideRawConfig, 'plugins'>; | ||
oas2?: Oas2PreprocessorsSet; | ||
async2?: Async2PreprocessorsSet; | ||
}; | ||
@@ -55,8 +59,10 @@ export declare type DecoratorsConfig = { | ||
oas2?: Oas2DecoratorsSet; | ||
async2?: Async2DecoratorsSet; | ||
}; | ||
export declare type TypesExtensionFn = (types: Record<string, NodeType>, oasVersion: OasVersion) => Record<string, NodeType>; | ||
export declare type TypeExtensionsConfig = Partial<Record<OasMajorVersion, TypesExtensionFn>>; | ||
export declare type TypesExtensionFn = (types: Record<string, NodeType>, oasVersion: SpecVersion) => Record<string, NodeType>; | ||
export declare type TypeExtensionsConfig = Partial<Record<SpecMajorVersion, TypesExtensionFn>>; | ||
export declare type CustomRulesConfig = { | ||
oas3?: Oas3RuleSet; | ||
oas2?: Oas2RuleSet; | ||
async2?: Async2RuleSet; | ||
}; | ||
@@ -63,0 +69,0 @@ export declare type AssertionContext = Partial<UserContext> & SkipFunctionContext & { |
@@ -16,3 +16,3 @@ export { BundleOutputFormat, readFileFromUrl, slash, doesYamlFileExist, isTruthy } from './utils'; | ||
export { unescapePointer, isRef, isAbsoluteUrl } from './ref-utils'; | ||
export { detectOpenAPI, OasMajorVersion, openAPIMajor, OasVersion } from './oas-types'; | ||
export { SpecMajorVersion, getMajorSpecVersion, SpecVersion, detectSpec, getTypes, } from './oas-types'; | ||
export { normalizeVisitors } from './visitors'; | ||
@@ -19,0 +19,0 @@ export { WalkContext, walkDocument, NormalizedProblem, ProblemSeverity, LineColLocationObject, LocationObject, Loc, } from './walk'; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.bundleDocument = exports.bundle = exports.lintConfig = exports.lintFromString = exports.lintDocument = exports.validate = exports.lint = exports.getTotals = exports.formatProblems = exports.getLineColLocation = exports.getAstNodeByPointer = exports.walkDocument = exports.normalizeVisitors = exports.OasVersion = exports.openAPIMajor = exports.OasMajorVersion = exports.detectOpenAPI = exports.isAbsoluteUrl = exports.isRef = exports.unescapePointer = exports.stringifyYaml = exports.parseYaml = exports.makeDocumentFromString = exports.YamlParseError = exports.ResolveError = exports.resolveDocument = exports.BaseResolver = exports.Source = exports.isRedoclyRegistryURL = exports.RedoclyClient = exports.createConfig = exports.CONFIG_FILE_NAMES = exports.findConfig = exports.getConfig = exports.loadConfig = exports.transformConfig = exports.getMergedConfig = exports.IGNORE_FILE = exports.StyleguideConfig = exports.Config = exports.Stats = exports.normalizeTypes = exports.ConfigTypes = exports.Oas2Types = exports.Oas3Types = exports.Oas3_1Types = exports.isTruthy = exports.doesYamlFileExist = exports.slash = exports.readFileFromUrl = void 0; | ||
exports.mapTypeToComponent = void 0; | ||
exports.bundle = exports.lintConfig = exports.lintFromString = exports.lintDocument = exports.validate = exports.lint = exports.getTotals = exports.formatProblems = exports.getLineColLocation = exports.getAstNodeByPointer = exports.walkDocument = exports.normalizeVisitors = exports.getTypes = exports.detectSpec = exports.SpecVersion = exports.getMajorSpecVersion = exports.SpecMajorVersion = exports.isAbsoluteUrl = exports.isRef = exports.unescapePointer = exports.stringifyYaml = exports.parseYaml = exports.makeDocumentFromString = exports.YamlParseError = exports.ResolveError = exports.resolveDocument = exports.BaseResolver = exports.Source = exports.isRedoclyRegistryURL = exports.RedoclyClient = exports.createConfig = exports.CONFIG_FILE_NAMES = exports.findConfig = exports.getConfig = exports.loadConfig = exports.transformConfig = exports.getMergedConfig = exports.IGNORE_FILE = exports.StyleguideConfig = exports.Config = exports.Stats = exports.normalizeTypes = exports.ConfigTypes = exports.Oas2Types = exports.Oas3Types = exports.Oas3_1Types = exports.isTruthy = exports.doesYamlFileExist = exports.slash = exports.readFileFromUrl = void 0; | ||
exports.mapTypeToComponent = exports.bundleDocument = void 0; | ||
var utils_1 = require("./utils"); | ||
@@ -51,6 +51,7 @@ Object.defineProperty(exports, "readFileFromUrl", { enumerable: true, get: function () { return utils_1.readFileFromUrl; } }); | ||
var oas_types_1 = require("./oas-types"); | ||
Object.defineProperty(exports, "detectOpenAPI", { enumerable: true, get: function () { return oas_types_1.detectOpenAPI; } }); | ||
Object.defineProperty(exports, "OasMajorVersion", { enumerable: true, get: function () { return oas_types_1.OasMajorVersion; } }); | ||
Object.defineProperty(exports, "openAPIMajor", { enumerable: true, get: function () { return oas_types_1.openAPIMajor; } }); | ||
Object.defineProperty(exports, "OasVersion", { enumerable: true, get: function () { return oas_types_1.OasVersion; } }); | ||
Object.defineProperty(exports, "SpecMajorVersion", { enumerable: true, get: function () { return oas_types_1.SpecMajorVersion; } }); | ||
Object.defineProperty(exports, "getMajorSpecVersion", { enumerable: true, get: function () { return oas_types_1.getMajorSpecVersion; } }); | ||
Object.defineProperty(exports, "SpecVersion", { enumerable: true, get: function () { return oas_types_1.SpecVersion; } }); | ||
Object.defineProperty(exports, "detectSpec", { enumerable: true, get: function () { return oas_types_1.detectSpec; } }); | ||
Object.defineProperty(exports, "getTypes", { enumerable: true, get: function () { return oas_types_1.getTypes; } }); | ||
var visitors_1 = require("./visitors"); | ||
@@ -57,0 +58,0 @@ Object.defineProperty(exports, "normalizeVisitors", { enumerable: true, get: function () { return visitors_1.normalizeVisitors; } }); |
@@ -15,5 +15,2 @@ "use strict"; | ||
const visitors_1 = require("./visitors"); | ||
const oas3_1_1 = require("./types/oas3_1"); | ||
const oas3_1 = require("./types/oas3"); | ||
const oas2_1 = require("./types/oas2"); | ||
const walk_1 = require("./walk"); | ||
@@ -46,17 +43,13 @@ const config_1 = require("./config"); | ||
const { document, customTypes, externalRefResolver, config } = opts; | ||
const oasVersion = oas_types_1.detectOpenAPI(document.parsed); | ||
const oasMajorVersion = oas_types_1.openAPIMajor(oasVersion); | ||
const rules = config.getRulesForOasVersion(oasMajorVersion); | ||
const types = types_1.normalizeTypes(config.extendTypes((customTypes !== null && customTypes !== void 0 ? customTypes : oasMajorVersion === oas_types_1.OasMajorVersion.Version3) | ||
? oasVersion === oas_types_1.OasVersion.Version3_1 | ||
? oas3_1_1.Oas3_1Types | ||
: oas3_1.Oas3Types | ||
: oas2_1.Oas2Types, oasVersion), config); | ||
const specVersion = oas_types_1.detectSpec(document.parsed); | ||
const specMajorVersion = oas_types_1.getMajorSpecVersion(specVersion); | ||
const rules = config.getRulesForOasVersion(specMajorVersion); | ||
const types = types_1.normalizeTypes(config.extendTypes(customTypes !== null && customTypes !== void 0 ? customTypes : oas_types_1.getTypes(specVersion), specVersion), config); | ||
const ctx = { | ||
problems: [], | ||
oasVersion: oasVersion, | ||
oasVersion: specVersion, | ||
visitorsData: {}, | ||
}; | ||
const preprocessors = config_1.initRules(rules, config, 'preprocessors', oasVersion); | ||
const regularRules = config_1.initRules(rules, config, 'rules', oasVersion); | ||
const preprocessors = config_1.initRules(rules, config, 'preprocessors', specVersion); | ||
const regularRules = config_1.initRules(rules, config, 'rules', specVersion); | ||
let resolvedRefMap = yield resolve_1.resolveDocument({ | ||
@@ -99,3 +92,3 @@ rootDocument: document, | ||
problems: [], | ||
oasVersion: oas_types_1.OasVersion.Version3_0, | ||
oasVersion: oas_types_1.SpecVersion.OAS3_0, | ||
visitorsData: {}, | ||
@@ -113,5 +106,6 @@ }; | ||
ruleId: 'configuration spec', | ||
visitor: spec_1.OasSpec({ severity: 'error' }), | ||
visitor: spec_1.Spec({ severity: 'error' }), | ||
}, | ||
]; | ||
// TODO: check why any is needed | ||
const normalizedVisitors = visitors_1.normalizeVisitors(rules, types); | ||
@@ -118,0 +112,0 @@ walk_1.walkDocument({ |
@@ -1,19 +0,25 @@ | ||
import { Oas3Rule, Oas3Preprocessor, Oas2Rule, Oas2Preprocessor } from './visitors'; | ||
import { Oas3Rule, Oas3Preprocessor, Oas2Rule, Oas2Preprocessor, Async2Preprocessor, Async2Rule } from './visitors'; | ||
export declare type RuleSet<T> = Record<string, T>; | ||
export declare enum OasVersion { | ||
Version2 = "oas2", | ||
Version3_0 = "oas3_0", | ||
Version3_1 = "oas3_1" | ||
export declare enum SpecVersion { | ||
OAS2 = "oas2", | ||
OAS3_0 = "oas3_0", | ||
OAS3_1 = "oas3_1", | ||
Async2 = "async2" | ||
} | ||
export declare enum OasMajorVersion { | ||
Version2 = "oas2", | ||
Version3 = "oas3" | ||
export declare enum SpecMajorVersion { | ||
OAS2 = "oas2", | ||
OAS3 = "oas3", | ||
Async2 = "async2" | ||
} | ||
export declare type Oas3RuleSet = Record<string, Oas3Rule>; | ||
export declare type Oas2RuleSet = Record<string, Oas2Rule>; | ||
export declare type Async2RuleSet = Record<string, Async2Rule>; | ||
export declare type Oas3PreprocessorsSet = Record<string, Oas3Preprocessor>; | ||
export declare type Oas2PreprocessorsSet = Record<string, Oas2Preprocessor>; | ||
export declare type Async2PreprocessorsSet = Record<string, Async2Preprocessor>; | ||
export declare type Oas3DecoratorsSet = Record<string, Oas3Preprocessor>; | ||
export declare type Oas2DecoratorsSet = Record<string, Oas2Preprocessor>; | ||
export declare function detectOpenAPI(root: any): OasVersion; | ||
export declare function openAPIMajor(version: OasVersion): OasMajorVersion; | ||
export declare type Async2DecoratorsSet = Record<string, Async2Preprocessor>; | ||
export declare function detectSpec(root: any): SpecVersion; | ||
export declare function getMajorSpecVersion(version: SpecVersion): SpecMajorVersion; | ||
export declare function getTypes(spec: SpecVersion): Record<string, import("./types").NodeType>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.openAPIMajor = exports.detectOpenAPI = exports.OasMajorVersion = exports.OasVersion = void 0; | ||
var OasVersion; | ||
(function (OasVersion) { | ||
OasVersion["Version2"] = "oas2"; | ||
OasVersion["Version3_0"] = "oas3_0"; | ||
OasVersion["Version3_1"] = "oas3_1"; | ||
})(OasVersion = exports.OasVersion || (exports.OasVersion = {})); | ||
var OasMajorVersion; | ||
(function (OasMajorVersion) { | ||
OasMajorVersion["Version2"] = "oas2"; | ||
OasMajorVersion["Version3"] = "oas3"; | ||
})(OasMajorVersion = exports.OasMajorVersion || (exports.OasMajorVersion = {})); | ||
function detectOpenAPI(root) { | ||
exports.getTypes = exports.getMajorSpecVersion = exports.detectSpec = exports.SpecMajorVersion = exports.SpecVersion = void 0; | ||
const oas2_1 = require("./types/oas2"); | ||
const oas3_1 = require("./types/oas3"); | ||
const oas3_1_1 = require("./types/oas3_1"); | ||
const asyncapi_1 = require("./types/asyncapi"); | ||
var SpecVersion; | ||
(function (SpecVersion) { | ||
SpecVersion["OAS2"] = "oas2"; | ||
SpecVersion["OAS3_0"] = "oas3_0"; | ||
SpecVersion["OAS3_1"] = "oas3_1"; | ||
SpecVersion["Async2"] = "async2"; | ||
})(SpecVersion = exports.SpecVersion || (exports.SpecVersion = {})); | ||
var SpecMajorVersion; | ||
(function (SpecMajorVersion) { | ||
SpecMajorVersion["OAS2"] = "oas2"; | ||
SpecMajorVersion["OAS3"] = "oas3"; | ||
SpecMajorVersion["Async2"] = "async2"; | ||
})(SpecMajorVersion = exports.SpecMajorVersion || (exports.SpecMajorVersion = {})); | ||
const typesMap = { | ||
[SpecVersion.OAS2]: oas2_1.Oas2Types, | ||
[SpecVersion.OAS3_0]: oas3_1.Oas3Types, | ||
[SpecVersion.OAS3_1]: oas3_1_1.Oas3_1Types, | ||
[SpecVersion.Async2]: asyncapi_1.AsyncApi2Types, | ||
}; | ||
function detectSpec(root) { | ||
if (typeof root !== 'object') { | ||
throw new Error(`Document must be JSON object, got ${typeof root}`); | ||
} | ||
if (!(root.openapi || root.swagger)) { | ||
throw new Error('This doesn’t look like an OpenAPI document.\n'); | ||
} | ||
if (root.openapi && typeof root.openapi !== 'string') { | ||
@@ -26,21 +35,38 @@ throw new Error(`Invalid OpenAPI version: should be a string but got "${typeof root.openapi}"`); | ||
if (root.openapi && root.openapi.startsWith('3.0')) { | ||
return OasVersion.Version3_0; | ||
return SpecVersion.OAS3_0; | ||
} | ||
if (root.openapi && root.openapi.startsWith('3.1')) { | ||
return OasVersion.Version3_1; | ||
return SpecVersion.OAS3_1; | ||
} | ||
if (root.swagger && root.swagger === '2.0') { | ||
return OasVersion.Version2; | ||
return SpecVersion.OAS2; | ||
} | ||
throw new Error(`Unsupported OpenAPI Version: ${root.openapi || root.swagger}`); | ||
// if not detected yet | ||
if (root.openapi || root.swagger) { | ||
throw new Error(`Unsupported OpenAPI version: ${root.openapi || root.swagger}`); | ||
} | ||
if (root.asyncapi && root.asyncapi.startsWith('2.')) { | ||
return SpecVersion.Async2; | ||
} | ||
if (root.asyncapi) { | ||
throw new Error(`Unsupported AsyncAPI version: ${root.asyncapi}`); | ||
} | ||
throw new Error(`Unsupported specification`); | ||
} | ||
exports.detectOpenAPI = detectOpenAPI; | ||
function openAPIMajor(version) { | ||
if (version === OasVersion.Version2) { | ||
return OasMajorVersion.Version2; | ||
exports.detectSpec = detectSpec; | ||
function getMajorSpecVersion(version) { | ||
if (version === SpecVersion.OAS2) { | ||
return SpecMajorVersion.OAS2; | ||
} | ||
else if (version === SpecVersion.Async2) { | ||
return SpecMajorVersion.Async2; | ||
} | ||
else { | ||
return OasMajorVersion.Version3; | ||
return SpecMajorVersion.OAS3; | ||
} | ||
} | ||
exports.openAPIMajor = openAPIMajor; | ||
exports.getMajorSpecVersion = getMajorSpecVersion; | ||
function getTypes(spec) { | ||
return typesMap[spec]; | ||
} | ||
exports.getTypes = getTypes; |
@@ -17,3 +17,3 @@ "use strict"; | ||
report({ | ||
message: `Scalar property should have "example"${oasVersion === oas_types_1.OasVersion.Version3_1 ? ' or "examples"' : ''} defined.`, | ||
message: `Scalar property should have "example"${oasVersion === oas_types_1.SpecVersion.OAS3_1 ? ' or "examples"' : ''} defined.`, | ||
location: location.child(propName).key(), | ||
@@ -20,0 +20,0 @@ }); |
@@ -1,2 +0,2 @@ | ||
import type { Oas3Rule, Oas2Rule } from '../../visitors'; | ||
export declare const OasSpec: Oas3Rule | Oas2Rule; | ||
import type { Oas3Rule, Oas2Rule, Async2Rule } from '../../visitors'; | ||
export declare const Spec: Oas3Rule | Oas2Rule | Async2Rule; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.OasSpec = void 0; | ||
exports.Spec = void 0; | ||
const types_1 = require("../../types"); | ||
@@ -8,3 +8,3 @@ const utils_1 = require("../utils"); | ||
const utils_2 = require("../../utils"); | ||
const OasSpec = () => { | ||
const Spec = () => { | ||
return { | ||
@@ -160,2 +160,2 @@ any(node, { report, type, location, rawLocation, key, resolve, ignoreNextVisitorsOnNode }) { | ||
}; | ||
exports.OasSpec = OasSpec; | ||
exports.Spec = Spec; |
@@ -47,3 +47,3 @@ "use strict"; | ||
exports.rules = { | ||
spec: spec_1.OasSpec, | ||
spec: spec_1.Spec, | ||
'no-invalid-schema-examples': no_invalid_schema_examples_1.NoInvalidSchemaExamples, | ||
@@ -50,0 +50,0 @@ 'no-invalid-parameter-examples': no_invalid_parameter_examples_1.NoInvalidParameterExamples, |
@@ -58,3 +58,3 @@ "use strict"; | ||
exports.rules = { | ||
spec: spec_1.OasSpec, | ||
spec: spec_1.Spec, | ||
'info-contact': info_contact_1.InfoContact, | ||
@@ -61,0 +61,0 @@ 'info-license': info_license_1.InfoLicense, |
@@ -64,2 +64,4 @@ "use strict"; | ||
'component-name-unique', | ||
'channels-kebab-case', | ||
'no-channel-trailing-slash', | ||
]; | ||
@@ -126,2 +128,3 @@ const nodeTypesList = [ | ||
'SpecExtension', | ||
'Message', | ||
]; | ||
@@ -128,0 +131,0 @@ const ConfigStyleguide = { |
@@ -7,2 +7,3 @@ import type { Oas3Definition, Oas3ExternalDocs, Oas3Info, Oas3Contact, Oas3Components, Oas3License, Oas3Schema, Oas3Header, Oas3Parameter, Oas3Operation, Oas3PathItem, Oas3ServerVariable, Oas3Server, Oas3MediaType, Oas3Response, Oas3Example, Oas3RequestBody, Oas3Tag, OasRef, Oas3SecurityScheme, Oas3SecurityRequirement, Oas3Encoding, Oas3Link, Oas3Xml, Oas3Discriminator, Oas3Callback } from './typings/openapi'; | ||
import type { Location } from './ref-utils'; | ||
import { Async2Definition } from './typings/asyncapi'; | ||
export declare type SkipFunctionContext = Pick<UserContext, 'location' | 'rawNode' | 'resolve' | 'rawLocation'>; | ||
@@ -138,2 +139,5 @@ export declare type VisitFunction<T> = (node: T, ctx: UserContext & { | ||
}; | ||
declare type Async2FlatVisitor = { | ||
Root?: VisitFunctionOrObject<Async2Definition>; | ||
}; | ||
declare type Oas3NestedVisitor = { | ||
@@ -145,6 +149,11 @@ [T in keyof Oas3FlatVisitor]: Oas3FlatVisitor[T] extends Function ? Oas3FlatVisitor[T] : Oas3FlatVisitor[T] & NestedVisitor<Oas3NestedVisitor>; | ||
}; | ||
declare type Async2NestedVisitor = { | ||
[T in keyof Async2FlatVisitor]: Async2FlatVisitor[T] extends Function ? Async2FlatVisitor[T] : Async2FlatVisitor[T] & NestedVisitor<Async2NestedVisitor>; | ||
}; | ||
export declare type Oas3Visitor = BaseVisitor & Oas3NestedVisitor & Record<string, VisitFunction<any> | NestedVisitObject<any, Oas3NestedVisitor>>; | ||
export declare type Oas2Visitor = BaseVisitor & Oas2NestedVisitor & Record<string, VisitFunction<any> | NestedVisitObject<any, Oas2NestedVisitor>>; | ||
export declare type Async2Visitor = BaseVisitor & Async2NestedVisitor & Record<string, VisitFunction<any> | NestedVisitObject<any, Async2NestedVisitor>>; | ||
export declare type Oas3TransformVisitor = BaseVisitor & Oas3FlatVisitor & Record<string, VisitFunction<any> | VisitObject<any>>; | ||
export declare type Oas2TransformVisitor = BaseVisitor & Oas2FlatVisitor & Record<string, VisitFunction<any> | VisitObject<any>>; | ||
export declare type Async2TransformVisitor = BaseVisitor & Async2FlatVisitor & Record<string, VisitFunction<any> | VisitObject<any>>; | ||
export declare type NestedVisitor<T> = Exclude<T, 'any' | 'ref' | 'Root'>; | ||
@@ -168,6 +177,9 @@ export declare type NormalizedOasVisitors<T extends BaseVisitor> = { | ||
export declare type Oas2Rule = (options: Record<string, any>) => Oas2Visitor | Oas2Visitor[]; | ||
export declare type Async2Rule = (options: Record<string, any>) => Async2Visitor | Async2Visitor[]; | ||
export declare type Oas3Preprocessor = (options: Record<string, any>) => Oas3TransformVisitor; | ||
export declare type Oas2Preprocessor = (options: Record<string, any>) => Oas2TransformVisitor; | ||
export declare type Async2Preprocessor = (options: Record<string, any>) => Async2TransformVisitor; | ||
export declare type Oas3Decorator = (options: Record<string, any>) => Oas3TransformVisitor; | ||
export declare type Oas2Decorator = (options: Record<string, any>) => Oas2TransformVisitor; | ||
export declare type Async2Decorator = (options: Record<string, any>) => Async2TransformVisitor; | ||
export declare type OasRule = Oas3Rule; | ||
@@ -174,0 +186,0 @@ export declare type OasPreprocessor = Oas3Preprocessor; |
@@ -8,3 +8,3 @@ import type { Referenced } from './typings/openapi'; | ||
import { ResolveError, YamlParseError, Source } from './resolve'; | ||
import { OasVersion } from './oas-types'; | ||
import { SpecVersion } from './oas-types'; | ||
declare type NonUndefined = string | number | boolean | symbol | bigint | object | Record<string, any>; | ||
@@ -31,3 +31,3 @@ export declare type ResolveResult<T extends NonUndefined> = { | ||
parent: any; | ||
oasVersion: OasVersion; | ||
oasVersion: SpecVersion; | ||
getVisitorData: () => Record<string, unknown>; | ||
@@ -70,3 +70,3 @@ }; | ||
problems: NormalizedProblem[]; | ||
oasVersion: OasVersion; | ||
oasVersion: SpecVersion; | ||
visitorsData: Record<string, Record<string, unknown>>; | ||
@@ -73,0 +73,0 @@ refTypes?: Map<string, NormalizedNodeType>; |
{ | ||
"name": "@redocly/openapi-core", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "", | ||
@@ -29,2 +29,3 @@ "main": "lib/index.js", | ||
"Swagger linter", | ||
"AsyncAPI linter", | ||
"oas" | ||
@@ -31,0 +32,0 @@ ], |
@@ -8,3 +8,3 @@ import * as path from 'path'; | ||
import { parseYamlToDocument, replaceSourceWithRef, makeConfig } from '../../__tests__/utils'; | ||
import { detectOpenAPI } from '../oas-types'; | ||
import { detectSpec } from '../oas-types'; | ||
@@ -65,3 +65,3 @@ describe('lint', () => { | ||
rule/operation-summary-length: | ||
subject: | ||
subject: | ||
type: Operation | ||
@@ -71,3 +71,3 @@ property: summary | ||
assertions: | ||
local/checkWordsCount: | ||
local/checkWordsCount: | ||
min: 3 | ||
@@ -195,4 +195,4 @@ theme: | ||
outdent` | ||
apis: | ||
entry: | ||
apis: | ||
entry: | ||
root: ./file.yaml | ||
@@ -311,3 +311,3 @@ rules: | ||
); | ||
expect(() => detectOpenAPI(testDocument.parsed)).toThrow( | ||
expect(() => detectSpec(testDocument.parsed)).toThrow( | ||
`Invalid OpenAPI version: should be a string but got "number"` | ||
@@ -317,2 +317,32 @@ ); | ||
it('detect unsupported OpenAPI version', () => { | ||
const testDocument = parseYamlToDocument( | ||
outdent` | ||
openapi: 1.0.4 | ||
`, | ||
'' | ||
); | ||
expect(() => detectSpec(testDocument.parsed)).toThrow(`Unsupported OpenAPI version: 1.0.4`); | ||
}); | ||
it('detect unsupported AsyncAPI version', () => { | ||
const testDocument = parseYamlToDocument( | ||
outdent` | ||
asyncapi: 1.0.4 | ||
`, | ||
'' | ||
); | ||
expect(() => detectSpec(testDocument.parsed)).toThrow(`Unsupported AsyncAPI version: 1.0.4`); | ||
}); | ||
it('detect unsupported spec format', () => { | ||
const testDocument = parseYamlToDocument( | ||
outdent` | ||
notapi: 3.1.0 | ||
`, | ||
'' | ||
); | ||
expect(() => detectSpec(testDocument.parsed)).toThrow(`Unsupported specification`); | ||
}); | ||
it("spec rule shouldn't throw an error for named callback", async () => { | ||
@@ -319,0 +349,0 @@ const document = parseYamlToDocument( |
import isEqual = require('lodash.isequal'); | ||
import { BaseResolver, resolveDocument, Document, ResolvedRefMap, makeRefId } from './resolve'; | ||
import { Oas3Rule, normalizeVisitors, Oas3Visitor, Oas2Visitor } from './visitors'; | ||
import { Oas3Types } from './types/oas3'; | ||
import { Oas2Types } from './types/oas2'; | ||
import { Oas3_1Types } from './types/oas3_1'; | ||
import { NormalizedNodeType, normalizeTypes, NodeType } from './types'; | ||
import { WalkContext, walkDocument, UserContext, ResolveResult, NormalizedProblem } from './walk'; | ||
import { detectOpenAPI, openAPIMajor, OasMajorVersion } from './oas-types'; | ||
import { detectSpec, getTypes, getMajorSpecVersion, SpecMajorVersion } from './oas-types'; | ||
import { isAbsoluteUrl, isRef, Location, refBaseName } from './ref-utils'; | ||
@@ -96,23 +93,16 @@ import { initRules } from './config/rules'; | ||
} = opts; | ||
const oasVersion = detectOpenAPI(document.parsed); | ||
const oasMajorVersion = openAPIMajor(oasVersion); | ||
const rules = config.getRulesForOasVersion(oasMajorVersion); | ||
const specVersion = detectSpec(document.parsed); | ||
const specMajorVersion = getMajorSpecVersion(specVersion); | ||
const rules = config.getRulesForOasVersion(specMajorVersion); | ||
const types = normalizeTypes( | ||
config.extendTypes( | ||
customTypes ?? oasMajorVersion === OasMajorVersion.Version3 | ||
? oasVersion === OasVersion.Version3_1 | ||
? Oas3_1Types | ||
: Oas3Types | ||
: Oas2Types, | ||
oasVersion | ||
), | ||
config.extendTypes(customTypes ?? getTypes(specVersion), specVersion), | ||
config | ||
); | ||
const preprocessors = initRules(rules as any, config, 'preprocessors', oasVersion); | ||
const decorators = initRules(rules as any, config, 'decorators', oasVersion); | ||
const preprocessors = initRules(rules as any, config, 'preprocessors', specVersion); | ||
const decorators = initRules(rules as any, config, 'decorators', specVersion); | ||
const ctx: BundleContext = { | ||
problems: [], | ||
oasVersion: oasVersion, | ||
oasVersion: specVersion, | ||
refTypes: new Map<string, NormalizedNodeType>(), | ||
@@ -127,3 +117,3 @@ visitorsData: {}, | ||
visitor: | ||
oasMajorVersion === OasMajorVersion.Version2 | ||
specMajorVersion === SpecMajorVersion.OAS2 | ||
? RemoveUnusedComponentsOas2({}) | ||
@@ -162,3 +152,3 @@ : RemoveUnusedComponentsOas3({}), | ||
visitor: makeBundleVisitor( | ||
oasMajorVersion, | ||
specMajorVersion, | ||
dereference, | ||
@@ -194,5 +184,5 @@ skipRedoclyRegistryRefs, | ||
export function mapTypeToComponent(typeName: string, version: OasMajorVersion) { | ||
export function mapTypeToComponent(typeName: string, version: SpecMajorVersion) { | ||
switch (version) { | ||
case OasMajorVersion.Version3: | ||
case SpecMajorVersion.OAS3: | ||
switch (typeName) { | ||
@@ -220,3 +210,3 @@ case 'Schema': | ||
} | ||
case OasMajorVersion.Version2: | ||
case SpecMajorVersion.OAS2: | ||
switch (typeName) { | ||
@@ -232,2 +222,11 @@ case 'Schema': | ||
} | ||
case SpecMajorVersion.Async2: | ||
switch (typeName) { | ||
case 'Schema': | ||
return 'schemas'; | ||
case 'Parameter': | ||
return 'parameters'; | ||
default: | ||
return null; | ||
} | ||
} | ||
@@ -239,3 +238,3 @@ } | ||
function makeBundleVisitor( | ||
version: OasMajorVersion, | ||
version: SpecMajorVersion, | ||
dereference: boolean, | ||
@@ -292,5 +291,5 @@ skipRedoclyRegistryRefs: boolean, | ||
rootLocation = ctx.location; | ||
if (version === OasMajorVersion.Version3) { | ||
if (version === SpecMajorVersion.OAS3) { | ||
components = root.components = root.components || {}; | ||
} else if (version === OasMajorVersion.Version2) { | ||
} else if (version === SpecMajorVersion.OAS2) { | ||
components = root; | ||
@@ -302,3 +301,3 @@ } | ||
if (version === OasMajorVersion.Version3) { | ||
if (version === SpecMajorVersion.OAS3) { | ||
visitor.DiscriminatorMapping = { | ||
@@ -357,3 +356,3 @@ leave(mapping: Record<string, string>, ctx: any) { | ||
components[componentType][name] = target.node; | ||
if (version === OasMajorVersion.Version3) { | ||
if (version === SpecMajorVersion.OAS3) { | ||
return `#/components/${componentType}/${name}`; | ||
@@ -360,0 +359,0 @@ } else { |
@@ -1,2 +0,2 @@ | ||
import { OasVersion } from '../../oas-types'; | ||
import { SpecVersion } from '../../oas-types'; | ||
import { Config, StyleguideConfig } from '../config'; | ||
@@ -120,2 +120,3 @@ import { getMergedConfig } from '../utils'; | ||
"decorators": Object { | ||
"async2": Object {}, | ||
"oas2": Object {}, | ||
@@ -131,2 +132,3 @@ "oas3_0": Object {}, | ||
"preprocessors": Object { | ||
"async2": Object {}, | ||
"oas2": Object {}, | ||
@@ -145,2 +147,5 @@ "oas3_0": Object {}, | ||
"rules": Object { | ||
"async2": Object { | ||
"operation-summary": "warn", | ||
}, | ||
"oas2": Object { | ||
@@ -223,2 +228,3 @@ "operation-summary": "warn", | ||
"decorators": Object { | ||
"async2": Object {}, | ||
"oas2": Object {}, | ||
@@ -234,2 +240,3 @@ "oas3_0": Object {}, | ||
"preprocessors": Object { | ||
"async2": Object {}, | ||
"oas2": Object {}, | ||
@@ -250,2 +257,6 @@ "oas3_0": Object {}, | ||
"rules": Object { | ||
"async2": Object { | ||
"no-empty-servers": "error", | ||
"operation-summary": "error", | ||
}, | ||
"oas2": Object { | ||
@@ -288,3 +299,3 @@ "no-empty-servers": "error", | ||
const styleguideConfig = new StyleguideConfig(testRawConfigStyleguide); | ||
styleguideConfig.extendTypes({}, OasVersion.Version3_0); | ||
styleguideConfig.extendTypes({}, SpecVersion.OAS3_0); | ||
expect(oas3).toHaveBeenCalledTimes(1); | ||
@@ -295,3 +306,3 @@ expect(oas2).toHaveBeenCalledTimes(0); | ||
const styleguideConfig = new StyleguideConfig(testRawConfigStyleguide); | ||
styleguideConfig.extendTypes({}, OasVersion.Version2); | ||
styleguideConfig.extendTypes({}, SpecVersion.OAS2); | ||
expect(oas3).toHaveBeenCalledTimes(0); | ||
@@ -302,3 +313,3 @@ expect(oas2).toHaveBeenCalledTimes(1); | ||
const styleguideConfig = new StyleguideConfig(testRawConfigStyleguide); | ||
expect(() => styleguideConfig.extendTypes({}, 'something else' as OasVersion)).toThrowError( | ||
expect(() => styleguideConfig.extendTypes({}, 'something else' as SpecVersion)).toThrowError( | ||
'Not implemented' | ||
@@ -305,0 +316,0 @@ ); |
@@ -6,2 +6,3 @@ import recommended from './recommended'; | ||
import { rules as oas2Rules } from '../rules/oas2'; | ||
import { rules as async2Rules } from '../rules/async2'; | ||
import { preprocessors as oas3Preprocessors } from '../rules/oas3'; | ||
@@ -28,2 +29,3 @@ import { preprocessors as oas2Preprocessors } from '../rules/oas2'; | ||
oas2: oas2Rules, | ||
async2: async2Rules, | ||
} as CustomRulesConfig, | ||
@@ -33,2 +35,3 @@ preprocessors: { | ||
oas2: oas2Preprocessors, | ||
async2: {}, | ||
}, | ||
@@ -38,4 +41,5 @@ decorators: { | ||
oas2: oas2Decorators, | ||
async2: {}, | ||
}, | ||
configs: builtInConfigs, | ||
}; |
@@ -144,4 +144,4 @@ import * as path from 'path'; | ||
if (pluginModule.rules) { | ||
if (!pluginModule.rules.oas3 && !pluginModule.rules.oas2) { | ||
throw new Error(`Plugin rules must have \`oas3\` or \`oas2\` rules "${p}.`); | ||
if (!pluginModule.rules.oas3 && !pluginModule.rules.oas2 && !pluginModule.rules.async2) { | ||
throw new Error(`Plugin rules must have \`oas3\`, \`oas2\` or \`async2\` rules "${p}.`); | ||
} | ||
@@ -155,7 +155,14 @@ plugin.rules = {}; | ||
} | ||
if (pluginModule.rules.async2) { | ||
plugin.rules.async2 = prefixRules(pluginModule.rules.async2, id); | ||
} | ||
} | ||
if (pluginModule.preprocessors) { | ||
if (!pluginModule.preprocessors.oas3 && !pluginModule.preprocessors.oas2) { | ||
if ( | ||
!pluginModule.preprocessors.oas3 && | ||
!pluginModule.preprocessors.oas2 && | ||
!pluginModule.preprocessors.async2 | ||
) { | ||
throw new Error( | ||
`Plugin \`preprocessors\` must have \`oas3\` or \`oas2\` preprocessors "${p}.` | ||
`Plugin \`preprocessors\` must have \`oas3\`, \`oas2\` or \`async2\` preprocessors "${p}.` | ||
); | ||
@@ -170,7 +177,16 @@ } | ||
} | ||
if (pluginModule.preprocessors.async2) { | ||
plugin.preprocessors.async2 = prefixRules(pluginModule.preprocessors.async2, id); | ||
} | ||
} | ||
if (pluginModule.decorators) { | ||
if (!pluginModule.decorators.oas3 && !pluginModule.decorators.oas2) { | ||
throw new Error(`Plugin \`decorators\` must have \`oas3\` or \`oas2\` decorators "${p}.`); | ||
if ( | ||
!pluginModule.decorators.oas3 && | ||
!pluginModule.decorators.oas2 && | ||
!pluginModule.decorators.async2 | ||
) { | ||
throw new Error( | ||
`Plugin \`decorators\` must have \`oas3\`, \`oas2\` or \`async2\` decorators "${p}.` | ||
); | ||
} | ||
@@ -184,2 +200,5 @@ plugin.decorators = {}; | ||
} | ||
if (pluginModule.decorators.async2) { | ||
plugin.decorators.async2 = prefixRules(pluginModule.decorators.async2, id); | ||
} | ||
} | ||
@@ -186,0 +205,0 @@ |
@@ -6,3 +6,9 @@ import * as fs from 'fs'; | ||
import { NormalizedProblem } from '../walk'; | ||
import { OasVersion, OasMajorVersion, Oas2RuleSet, Oas3RuleSet } from '../oas-types'; | ||
import { | ||
SpecVersion, | ||
SpecMajorVersion, | ||
Oas2RuleSet, | ||
Oas3RuleSet, | ||
Async2RuleSet, | ||
} from '../oas-types'; | ||
import { isBrowser, env } from '../env'; | ||
@@ -69,8 +75,8 @@ | ||
doNotResolveExamples: boolean; | ||
rules: Record<OasVersion, Record<string, RuleConfig>>; | ||
preprocessors: Record<OasVersion, Record<string, PreprocessorConfig>>; | ||
decorators: Record<OasVersion, Record<string, DecoratorConfig>>; | ||
rules: Record<SpecVersion, Record<string, RuleConfig>>; | ||
preprocessors: Record<SpecVersion, Record<string, PreprocessorConfig>>; | ||
decorators: Record<SpecVersion, Record<string, DecoratorConfig>>; | ||
private _usedRules: Set<string> = new Set(); | ||
private _usedVersions: Set<OasVersion> = new Set(); | ||
private _usedVersions: Set<SpecVersion> = new Set(); | ||
@@ -88,17 +94,20 @@ recommendedFallback: boolean; | ||
this.rules = { | ||
[OasVersion.Version2]: { ...rawConfig.rules, ...rawConfig.oas2Rules }, | ||
[OasVersion.Version3_0]: { ...rawConfig.rules, ...rawConfig.oas3_0Rules }, | ||
[OasVersion.Version3_1]: { ...rawConfig.rules, ...rawConfig.oas3_1Rules }, | ||
[SpecVersion.OAS2]: { ...rawConfig.rules, ...rawConfig.oas2Rules }, | ||
[SpecVersion.OAS3_0]: { ...rawConfig.rules, ...rawConfig.oas3_0Rules }, | ||
[SpecVersion.OAS3_1]: { ...rawConfig.rules, ...rawConfig.oas3_1Rules }, | ||
[SpecVersion.Async2]: { ...rawConfig.rules, ...rawConfig.async2Rules }, | ||
}; | ||
this.preprocessors = { | ||
[OasVersion.Version2]: { ...rawConfig.preprocessors, ...rawConfig.oas2Preprocessors }, | ||
[OasVersion.Version3_0]: { ...rawConfig.preprocessors, ...rawConfig.oas3_0Preprocessors }, | ||
[OasVersion.Version3_1]: { ...rawConfig.preprocessors, ...rawConfig.oas3_1Preprocessors }, | ||
[SpecVersion.OAS2]: { ...rawConfig.preprocessors, ...rawConfig.oas2Preprocessors }, | ||
[SpecVersion.OAS3_0]: { ...rawConfig.preprocessors, ...rawConfig.oas3_0Preprocessors }, | ||
[SpecVersion.OAS3_1]: { ...rawConfig.preprocessors, ...rawConfig.oas3_1Preprocessors }, | ||
[SpecVersion.Async2]: { ...rawConfig.preprocessors, ...rawConfig.async2Preprocessors }, | ||
}; | ||
this.decorators = { | ||
[OasVersion.Version2]: { ...rawConfig.decorators, ...rawConfig.oas2Decorators }, | ||
[OasVersion.Version3_0]: { ...rawConfig.decorators, ...rawConfig.oas3_0Decorators }, | ||
[OasVersion.Version3_1]: { ...rawConfig.decorators, ...rawConfig.oas3_1Decorators }, | ||
[SpecVersion.OAS2]: { ...rawConfig.decorators, ...rawConfig.oas2Decorators }, | ||
[SpecVersion.OAS3_0]: { ...rawConfig.decorators, ...rawConfig.oas3_0Decorators }, | ||
[SpecVersion.OAS3_1]: { ...rawConfig.decorators, ...rawConfig.oas3_1Decorators }, | ||
[SpecVersion.Async2]: { ...rawConfig.decorators, ...rawConfig.async2Decorators }, | ||
}; | ||
@@ -179,3 +188,3 @@ | ||
extendTypes(types: Record<string, NodeType>, version: OasVersion) { | ||
extendTypes(types: Record<string, NodeType>, version: SpecVersion) { | ||
let extendedTypes = types; | ||
@@ -185,11 +194,15 @@ for (const plugin of this.plugins) { | ||
switch (version) { | ||
case OasVersion.Version3_0: | ||
case OasVersion.Version3_1: | ||
case SpecVersion.OAS3_0: | ||
case SpecVersion.OAS3_1: | ||
if (!plugin.typeExtension.oas3) continue; | ||
extendedTypes = plugin.typeExtension.oas3(extendedTypes, version); | ||
break; | ||
case OasVersion.Version2: | ||
case SpecVersion.OAS2: | ||
if (!plugin.typeExtension.oas2) continue; | ||
extendedTypes = plugin.typeExtension.oas2(extendedTypes, version); | ||
break; | ||
case SpecVersion.Async2: | ||
if (!plugin.typeExtension.async2) continue; | ||
extendedTypes = plugin.typeExtension.async2(extendedTypes, version); | ||
break; | ||
default: | ||
@@ -203,3 +216,3 @@ throw new Error('Not implemented'); | ||
getRuleSettings(ruleId: string, oasVersion: OasVersion): RuleSettings { | ||
getRuleSettings(ruleId: string, oasVersion: SpecVersion): RuleSettings { | ||
this._usedRules.add(ruleId); | ||
@@ -217,3 +230,3 @@ this._usedVersions.add(oasVersion); | ||
getPreprocessorSettings(ruleId: string, oasVersion: OasVersion): RuleSettings { | ||
getPreprocessorSettings(ruleId: string, oasVersion: SpecVersion): RuleSettings { | ||
this._usedRules.add(ruleId); | ||
@@ -232,3 +245,3 @@ this._usedVersions.add(oasVersion); | ||
getDecoratorSettings(ruleId: string, oasVersion: OasVersion): RuleSettings { | ||
getDecoratorSettings(ruleId: string, oasVersion: SpecVersion): RuleSettings { | ||
this._usedRules.add(ruleId); | ||
@@ -270,5 +283,5 @@ this._usedVersions.add(oasVersion); | ||
getRulesForOasVersion(version: OasMajorVersion) { | ||
getRulesForOasVersion(version: SpecMajorVersion) { | ||
switch (version) { | ||
case OasMajorVersion.Version3: | ||
case SpecMajorVersion.OAS3: | ||
// eslint-disable-next-line no-case-declarations | ||
@@ -280,3 +293,3 @@ const oas3Rules: Oas3RuleSet[] = []; // default ruleset | ||
return oas3Rules; | ||
case OasMajorVersion.Version2: | ||
case SpecMajorVersion.OAS2: | ||
// eslint-disable-next-line no-case-declarations | ||
@@ -288,2 +301,13 @@ const oas2Rules: Oas2RuleSet[] = []; // default ruleset | ||
return oas2Rules; | ||
case SpecMajorVersion.Async2: | ||
// eslint-disable-next-line no-case-declarations | ||
const asyncApiRules: Async2RuleSet[] = []; // default ruleset | ||
this.plugins.forEach( | ||
(p) => p.preprocessors?.async2 && asyncApiRules.push(p.preprocessors.async2) | ||
); | ||
this.plugins.forEach((p) => p.rules?.async2 && asyncApiRules.push(p.rules.async2)); | ||
this.plugins.forEach( | ||
(p) => p.decorators?.async2 && asyncApiRules.push(p.decorators.async2) | ||
); | ||
return asyncApiRules; | ||
} | ||
@@ -294,3 +318,3 @@ } | ||
for (const ruleId of rules || []) { | ||
for (const version of Object.values(OasVersion)) { | ||
for (const version of Object.values(SpecVersion)) { | ||
if (this.rules[version][ruleId]) { | ||
@@ -305,3 +329,3 @@ this.rules[version][ruleId] = 'off'; | ||
for (const preprocessorId of preprocessors || []) { | ||
for (const version of Object.values(OasVersion)) { | ||
for (const version of Object.values(SpecVersion)) { | ||
if (this.preprocessors[version][preprocessorId]) { | ||
@@ -316,3 +340,3 @@ this.preprocessors[version][preprocessorId] = 'off'; | ||
for (const decoratorId of decorators || []) { | ||
for (const version of Object.values(OasVersion)) { | ||
for (const version of Object.values(SpecVersion)) { | ||
if (this.decorators[version][decoratorId]) { | ||
@@ -319,0 +343,0 @@ this.decorators[version][decoratorId] = 'off'; |
@@ -1,2 +0,2 @@ | ||
import { RuleSet, OasVersion } from '../oas-types'; | ||
import { RuleSet, SpecVersion } from '../oas-types'; | ||
import { StyleguideConfig } from './config'; | ||
@@ -16,3 +16,3 @@ import { isDefined } from '../utils'; | ||
type: 'rules' | 'preprocessors' | 'decorators', | ||
oasVersion: OasVersion | ||
oasVersion: SpecVersion | ||
): InitializedRule[] { | ||
@@ -19,0 +19,0 @@ return rules |
import type { ProblemSeverity, UserContext } from '../walk'; | ||
import type { | ||
Oas3PreprocessorsSet, | ||
OasMajorVersion, | ||
SpecMajorVersion, | ||
Oas3DecoratorsSet, | ||
@@ -10,4 +10,8 @@ Oas2RuleSet, | ||
Oas3RuleSet, | ||
OasVersion, | ||
SpecVersion, | ||
Async2PreprocessorsSet, | ||
Async2DecoratorsSet, | ||
Async2RuleSet, | ||
} from '../oas-types'; | ||
import type { NodeType } from '../types'; | ||
@@ -47,2 +51,3 @@ import { Location } from '../ref-utils'; | ||
oas3_1Rules?: Record<string, RuleConfig>; | ||
async2Rules?: Record<string, RuleConfig>; | ||
@@ -53,2 +58,3 @@ preprocessors?: Record<string, PreprocessorConfig>; | ||
oas3_1Preprocessors?: Record<string, PreprocessorConfig>; | ||
async2Preprocessors?: Record<string, PreprocessorConfig>; | ||
@@ -59,2 +65,3 @@ decorators?: Record<string, DecoratorConfig>; | ||
oas3_1Decorators?: Record<string, DecoratorConfig>; | ||
async2Decorators?: Record<string, DecoratorConfig>; | ||
}; | ||
@@ -75,2 +82,3 @@ | ||
oas2?: Oas2PreprocessorsSet; | ||
async2?: Async2PreprocessorsSet; | ||
}; | ||
@@ -81,2 +89,3 @@ | ||
oas2?: Oas2DecoratorsSet; | ||
async2?: Async2DecoratorsSet; | ||
}; | ||
@@ -86,6 +95,6 @@ | ||
types: Record<string, NodeType>, | ||
oasVersion: OasVersion | ||
oasVersion: SpecVersion | ||
) => Record<string, NodeType>; | ||
export type TypeExtensionsConfig = Partial<Record<OasMajorVersion, TypesExtensionFn>>; | ||
export type TypeExtensionsConfig = Partial<Record<SpecMajorVersion, TypesExtensionFn>>; | ||
@@ -95,2 +104,3 @@ export type CustomRulesConfig = { | ||
oas2?: Oas2RuleSet; | ||
async2?: Async2RuleSet; | ||
}; | ||
@@ -97,0 +107,0 @@ |
@@ -55,3 +55,9 @@ export { BundleOutputFormat, readFileFromUrl, slash, doesYamlFileExist, isTruthy } from './utils'; | ||
export { unescapePointer, isRef, isAbsoluteUrl } from './ref-utils'; | ||
export { detectOpenAPI, OasMajorVersion, openAPIMajor, OasVersion } from './oas-types'; | ||
export { | ||
SpecMajorVersion, | ||
getMajorSpecVersion, | ||
SpecVersion, | ||
detectSpec, | ||
getTypes, | ||
} from './oas-types'; | ||
export { normalizeVisitors } from './visitors'; | ||
@@ -58,0 +64,0 @@ |
import { BaseResolver, resolveDocument, Document, makeDocumentFromString } from './resolve'; | ||
import { normalizeVisitors } from './visitors'; | ||
import { Oas3_1Types } from './types/oas3_1'; | ||
import { Oas3Types } from './types/oas3'; | ||
import { Oas2Types } from './types/oas2'; | ||
import { NodeType } from './types'; | ||
@@ -11,5 +8,5 @@ import { ProblemSeverity, WalkContext, walkDocument } from './walk'; | ||
import { releaseAjvInstance } from './rules/ajv'; | ||
import { detectOpenAPI, Oas3RuleSet, OasMajorVersion, OasVersion, openAPIMajor } from './oas-types'; | ||
import { Oas3RuleSet, SpecVersion, getMajorSpecVersion, detectSpec, getTypes } from './oas-types'; | ||
import { ConfigTypes } from './types/redocly-yaml'; | ||
import { OasSpec } from './rules/common/spec'; | ||
import { Spec } from './rules/common/spec'; | ||
@@ -58,14 +55,7 @@ export async function lint(opts: { | ||
const { document, customTypes, externalRefResolver, config } = opts; | ||
const oasVersion = detectOpenAPI(document.parsed); | ||
const oasMajorVersion = openAPIMajor(oasVersion); | ||
const rules = config.getRulesForOasVersion(oasMajorVersion); | ||
const specVersion = detectSpec(document.parsed); | ||
const specMajorVersion = getMajorSpecVersion(specVersion); | ||
const rules = config.getRulesForOasVersion(specMajorVersion); | ||
const types = normalizeTypes( | ||
config.extendTypes( | ||
customTypes ?? oasMajorVersion === OasMajorVersion.Version3 | ||
? oasVersion === OasVersion.Version3_1 | ||
? Oas3_1Types | ||
: Oas3Types | ||
: Oas2Types, | ||
oasVersion | ||
), | ||
config.extendTypes(customTypes ?? getTypes(specVersion), specVersion), | ||
config | ||
@@ -76,8 +66,8 @@ ); | ||
problems: [], | ||
oasVersion: oasVersion, | ||
oasVersion: specVersion, | ||
visitorsData: {}, | ||
}; | ||
const preprocessors = initRules(rules as any, config, 'preprocessors', oasVersion); | ||
const regularRules = initRules(rules as Oas3RuleSet[], config, 'rules', oasVersion); | ||
const preprocessors = initRules(rules as any, config, 'preprocessors', specVersion); | ||
const regularRules = initRules(rules as Oas3RuleSet[], config, 'rules', specVersion); | ||
@@ -123,3 +113,3 @@ let resolvedRefMap = await resolveDocument({ | ||
problems: [], | ||
oasVersion: OasVersion.Version3_0, | ||
oasVersion: SpecVersion.OAS3_0, | ||
visitorsData: {}, | ||
@@ -138,6 +128,7 @@ }; | ||
ruleId: 'configuration spec', | ||
visitor: OasSpec({ severity: 'error' }), | ||
visitor: Spec({ severity: 'error' }), | ||
}, | ||
]; | ||
const normalizedVisitors = normalizeVisitors(rules, types); | ||
// TODO: check why any is needed | ||
const normalizedVisitors = normalizeVisitors(rules as any, types); | ||
@@ -144,0 +135,0 @@ walkDocument({ |
@@ -1,24 +0,47 @@ | ||
import { Oas3Rule, Oas3Preprocessor, Oas2Rule, Oas2Preprocessor } from './visitors'; | ||
import { | ||
Oas3Rule, | ||
Oas3Preprocessor, | ||
Oas2Rule, | ||
Oas2Preprocessor, | ||
Async2Preprocessor, | ||
Async2Rule, | ||
} from './visitors'; | ||
import { Oas2Types } from './types/oas2'; | ||
import { Oas3Types } from './types/oas3'; | ||
import { Oas3_1Types } from './types/oas3_1'; | ||
import { AsyncApi2Types } from './types/asyncapi'; | ||
export type RuleSet<T> = Record<string, T>; | ||
export enum OasVersion { | ||
Version2 = 'oas2', | ||
Version3_0 = 'oas3_0', | ||
Version3_1 = 'oas3_1', | ||
export enum SpecVersion { | ||
OAS2 = 'oas2', | ||
OAS3_0 = 'oas3_0', | ||
OAS3_1 = 'oas3_1', | ||
Async2 = 'async2', // todo split into 2.x maybe? | ||
} | ||
export enum OasMajorVersion { | ||
Version2 = 'oas2', | ||
Version3 = 'oas3', | ||
export enum SpecMajorVersion { | ||
OAS2 = 'oas2', | ||
OAS3 = 'oas3', | ||
Async2 = 'async2', | ||
} | ||
const typesMap = { | ||
[SpecVersion.OAS2]: Oas2Types, | ||
[SpecVersion.OAS3_0]: Oas3Types, | ||
[SpecVersion.OAS3_1]: Oas3_1Types, | ||
[SpecVersion.Async2]: AsyncApi2Types, | ||
}; | ||
export type Oas3RuleSet = Record<string, Oas3Rule>; | ||
export type Oas2RuleSet = Record<string, Oas2Rule>; | ||
export type Async2RuleSet = Record<string, Async2Rule>; | ||
export type Oas3PreprocessorsSet = Record<string, Oas3Preprocessor>; | ||
export type Oas2PreprocessorsSet = Record<string, Oas2Preprocessor>; | ||
export type Async2PreprocessorsSet = Record<string, Async2Preprocessor>; | ||
export type Oas3DecoratorsSet = Record<string, Oas3Preprocessor>; | ||
export type Oas2DecoratorsSet = Record<string, Oas2Preprocessor>; | ||
export type Async2DecoratorsSet = Record<string, Async2Preprocessor>; | ||
export function detectOpenAPI(root: any): OasVersion { | ||
export function detectSpec(root: any): SpecVersion { | ||
if (typeof root !== 'object') { | ||
@@ -28,6 +51,2 @@ throw new Error(`Document must be JSON object, got ${typeof root}`); | ||
if (!(root.openapi || root.swagger)) { | ||
throw new Error('This doesn’t look like an OpenAPI document.\n'); | ||
} | ||
if (root.openapi && typeof root.openapi !== 'string') { | ||
@@ -38,22 +57,41 @@ throw new Error(`Invalid OpenAPI version: should be a string but got "${typeof root.openapi}"`); | ||
if (root.openapi && root.openapi.startsWith('3.0')) { | ||
return OasVersion.Version3_0; | ||
return SpecVersion.OAS3_0; | ||
} | ||
if (root.openapi && root.openapi.startsWith('3.1')) { | ||
return OasVersion.Version3_1; | ||
return SpecVersion.OAS3_1; | ||
} | ||
if (root.swagger && root.swagger === '2.0') { | ||
return OasVersion.Version2; | ||
return SpecVersion.OAS2; | ||
} | ||
throw new Error(`Unsupported OpenAPI Version: ${root.openapi || root.swagger}`); | ||
// if not detected yet | ||
if (root.openapi || root.swagger) { | ||
throw new Error(`Unsupported OpenAPI version: ${root.openapi || root.swagger}`); | ||
} | ||
if (root.asyncapi && root.asyncapi.startsWith('2.')) { | ||
return SpecVersion.Async2; | ||
} | ||
if (root.asyncapi) { | ||
throw new Error(`Unsupported AsyncAPI version: ${root.asyncapi}`); | ||
} | ||
throw new Error(`Unsupported specification`); | ||
} | ||
export function openAPIMajor(version: OasVersion): OasMajorVersion { | ||
if (version === OasVersion.Version2) { | ||
return OasMajorVersion.Version2; | ||
export function getMajorSpecVersion(version: SpecVersion): SpecMajorVersion { | ||
if (version === SpecVersion.OAS2) { | ||
return SpecMajorVersion.OAS2; | ||
} else if (version === SpecVersion.Async2) { | ||
return SpecMajorVersion.Async2; | ||
} else { | ||
return OasMajorVersion.Version3; | ||
return SpecMajorVersion.OAS3; | ||
} | ||
} | ||
export function getTypes(spec: SpecVersion) { | ||
return typesMap[spec]; | ||
} |
@@ -5,3 +5,3 @@ import type { Oas2Rule, Oas3Rule } from '../../visitors'; | ||
import type { Oas3Schema, Oas3_1Schema } from '../../typings/openapi'; | ||
import { OasVersion } from '../../oas-types'; | ||
import { SpecVersion } from '../../oas-types'; | ||
@@ -29,3 +29,3 @@ const SCALAR_TYPES = ['string', 'integer', 'number', 'boolean', 'null']; | ||
message: `Scalar property should have "example"${ | ||
oasVersion === OasVersion.Version3_1 ? ' or "examples"' : '' | ||
oasVersion === SpecVersion.OAS3_1 ? ' or "examples"' : '' | ||
} defined.`, | ||
@@ -32,0 +32,0 @@ location: location.child(propName).key(), |
@@ -1,2 +0,2 @@ | ||
import type { Oas3Rule, Oas2Rule } from '../../visitors'; | ||
import type { Oas3Rule, Oas2Rule, Async2Rule } from '../../visitors'; | ||
import { isNamedType, SpecExtension } from '../../types'; | ||
@@ -8,3 +8,3 @@ import { oasTypeOf, matchesJsonSchemaType, getSuggest, validateSchemaEnumType } from '../utils'; | ||
export const OasSpec: Oas3Rule | Oas2Rule = () => { | ||
export const Spec: Oas3Rule | Oas2Rule | Async2Rule = () => { | ||
return { | ||
@@ -11,0 +11,0 @@ any( |
import { Oas2Rule } from '../../visitors'; | ||
import { OasSpec } from '../common/spec'; | ||
import { Spec } from '../common/spec'; | ||
import { NoInvalidSchemaExamples } from '../common/no-invalid-schema-examples'; | ||
@@ -46,3 +46,3 @@ import { NoInvalidParameterExamples } from '../common/no-invalid-parameter-examples'; | ||
export const rules = { | ||
spec: OasSpec as Oas2Rule, | ||
spec: Spec as Oas2Rule, | ||
'no-invalid-schema-examples': NoInvalidSchemaExamples, | ||
@@ -49,0 +49,0 @@ 'no-invalid-parameter-examples': NoInvalidParameterExamples, |
import { Oas3RuleSet } from '../../oas-types'; | ||
import { OasSpec } from '../common/spec'; | ||
import { Spec } from '../common/spec'; | ||
import { Operation2xxResponse } from '../common/operation-2xx-response'; | ||
@@ -57,3 +57,3 @@ import { Operation4xxResponse } from '../common/operation-4xx-response'; | ||
export const rules = { | ||
spec: OasSpec, | ||
spec: Spec, | ||
'info-contact': InfoContact, | ||
@@ -60,0 +60,0 @@ 'info-license': InfoLicense, |
@@ -62,2 +62,4 @@ import { rootRedoclyConfigSchema, apiConfigSchema } from './portal-config-schema'; | ||
'component-name-unique', | ||
'channels-kebab-case', | ||
'no-channel-trailing-slash', | ||
]; | ||
@@ -125,2 +127,3 @@ | ||
'SpecExtension', | ||
'Message', | ||
]; | ||
@@ -127,0 +130,0 @@ |
@@ -52,2 +52,3 @@ import type { | ||
import type { Location } from './ref-utils'; | ||
import { Async2Definition } from './typings/asyncapi'; | ||
@@ -210,2 +211,6 @@ export type SkipFunctionContext = Pick< | ||
type Async2FlatVisitor = { | ||
Root?: VisitFunctionOrObject<Async2Definition>; | ||
}; | ||
const legacyTypesMap = { | ||
@@ -237,2 +242,8 @@ Root: 'DefinitionRoot', | ||
type Async2NestedVisitor = { | ||
[T in keyof Async2FlatVisitor]: Async2FlatVisitor[T] extends Function | ||
? Async2FlatVisitor[T] | ||
: Async2FlatVisitor[T] & NestedVisitor<Async2NestedVisitor>; | ||
}; | ||
export type Oas3Visitor = BaseVisitor & | ||
@@ -246,2 +257,6 @@ Oas3NestedVisitor & | ||
export type Async2Visitor = BaseVisitor & | ||
Async2NestedVisitor & | ||
Record<string, VisitFunction<any> | NestedVisitObject<any, Async2NestedVisitor>>; | ||
export type Oas3TransformVisitor = BaseVisitor & | ||
@@ -255,2 +270,6 @@ Oas3FlatVisitor & | ||
export type Async2TransformVisitor = BaseVisitor & | ||
Async2FlatVisitor & | ||
Record<string, VisitFunction<any> | VisitObject<any>>; | ||
export type NestedVisitor<T> = Exclude<T, 'any' | 'ref' | 'Root'>; | ||
@@ -277,6 +296,9 @@ | ||
export type Oas2Rule = (options: Record<string, any>) => Oas2Visitor | Oas2Visitor[]; | ||
export type Async2Rule = (options: Record<string, any>) => Async2Visitor | Async2Visitor[]; | ||
export type Oas3Preprocessor = (options: Record<string, any>) => Oas3TransformVisitor; | ||
export type Oas2Preprocessor = (options: Record<string, any>) => Oas2TransformVisitor; | ||
export type Async2Preprocessor = (options: Record<string, any>) => Async2TransformVisitor; | ||
export type Oas3Decorator = (options: Record<string, any>) => Oas3TransformVisitor; | ||
export type Oas2Decorator = (options: Record<string, any>) => Oas2TransformVisitor; | ||
export type Async2Decorator = (options: Record<string, any>) => Async2TransformVisitor; | ||
@@ -283,0 +305,0 @@ // alias for the latest version supported |
@@ -15,3 +15,3 @@ import type { Referenced } from './typings/openapi'; | ||
import { ResolveError, YamlParseError, Source, makeRefId } from './resolve'; | ||
import { OasVersion } from './oas-types'; | ||
import { SpecVersion } from './oas-types'; | ||
import { isNamedType, SpecExtension } from './types'; | ||
@@ -37,3 +37,3 @@ | ||
parent: any; | ||
oasVersion: OasVersion; | ||
oasVersion: SpecVersion; | ||
getVisitorData: () => Record<string, unknown>; | ||
@@ -84,3 +84,3 @@ }; | ||
problems: NormalizedProblem[]; | ||
oasVersion: OasVersion; | ||
oasVersion: SpecVersion; | ||
visitorsData: Record<string, Record<string, unknown>>; // custom data store that visitors can use for various purposes | ||
@@ -87,0 +87,0 @@ refTypes?: Map<string, NormalizedNodeType>; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
2870267
545
49205