Skip to content

feat(ls): provide OpenAPI 3.0.x OpenAPI lint rules #2069

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import ApilintCodes from '../../../../../codes';
import { LinterMeta } from '../../../../../../apidom-language-types';

const heartBeatIntervalTypeLint: LinterMeta = {
code: ApilintCodes.ASYNCAPI2_IBMMQ_MESSAGE_BINDING_FIELD_EXPIRY_TYPE,
code: ApilintCodes.ASYNCAPI2_IBMMQ_SERVER_BINDING_FIELD_HEART_BEAT_INTERVAL_TYPE,
source: 'apilint',
message: "'heartBeatInterval' value must be a positive integer (zero included)",
severity: 1,
Expand Down
21 changes: 21 additions & 0 deletions packages/apidom-ls/src/config/codes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -581,8 +581,29 @@ enum ApilintCodes {

OPENAPI3_0 = 5000000,

OPENAPI3_0_OPENAPI_VALUE_PATTERN_3_0_0 = 5000100,
OPENAPI3_0_OPENAPI_VALUE_PATTERN_3_0_1,
OPENAPI3_0_OPENAPI_VALUE_PATTERN_3_0_2,
OPENAPI3_0_OPENAPI_VALUE_PATTERN_3_0_3,

OPENAPI3_0_OPEN_API = 5010000,
OPENAPI3_0_OPEN_API_FIELD_INFO_TYPE = 5010100,
OPENAPI3_0_OPEN_API_FIELD_INFO_TYPE_REQUIRED,
OPENAPI3_0_OPEN_API_FIELD_SERVERS_TYPE = 5010200,
OPENAPI3_0_OPEN_API_FIELD_SERVERS_ITEMS_TYPE,
OPENAPI3_0_OPEN_API_FIELD_PATHS_TYPE = 5010300,
OPENAPI3_0_OPEN_API_FIELD_PATHS_REQUIRED,
OPENAPI3_0_OPEN_API_FIELD_COMPONENTS_TYPE = 5010400,
OPENAPI3_0_OPEN_API_FIELD_SECURITY_TYPE = 5010500,
OPENAPI3_0_OPEN_API_FIELD_SECURITY_ITEMS_TYPE,
OPENAPI3_0_OPEN_API_FIELD_TAGS_TYPE = 5010600,
OPENAPI3_0_OPEN_API_FIELD_TAGS_ITEMS_TYPE,
OPENAPI3_0_OPEN_API_FIELD_EXTERNAL_DOCS_TYPE = 5010700,

OPENAPI3_1 = 7000000,

OPENAPI3_1_OPENAPI_VALUE_PATTERN_3_1_0 = 7000100,

OPENAPI3_1_INFO = 7010000,
OPENAPI3_1_INFO_FIELD_SUMMARY_TYPE = 7010100,
OPENAPI3_1_INFO_FIELD_TERMS_OF_SERVICE_FORMAT_URI = 7010200,
Expand Down
2 changes: 2 additions & 0 deletions packages/apidom-ls/src/config/openapi/config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import openapiMeta from './openapi/meta';
import openapi3_0Meta from './openapi3_0/meta';
import openapi3_1Meta from './openapi3_1/meta';
import callbackMeta from './callback/meta';
Expand Down Expand Up @@ -43,6 +44,7 @@ export default {
},
],
},
openapi: openapiMeta,
openApi3_0: openapi3_0Meta,
openApi3_1: openapi3_1Meta,
callback: callbackMeta,
Expand Down
50 changes: 50 additions & 0 deletions packages/apidom-ls/src/config/openapi/openapi/completion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import {
ApidomCompletionItem,
CompletionFormat,
CompletionType,
} from '../../../apidom-language-types';

const completion: ApidomCompletionItem[] = [
{
label: '3.0.0',
insertText: '3.0.0',
kind: 12,
format: CompletionFormat.QUOTED_FORCED,
type: CompletionType.VALUE,
insertTextFormat: 2,
},
{
label: '3.0.1',
insertText: '3.0.1',
kind: 12,
format: CompletionFormat.QUOTED_FORCED,
type: CompletionType.VALUE,
insertTextFormat: 2,
},
{
label: '3.0.2',
insertText: '3.0.2',
kind: 12,
format: CompletionFormat.QUOTED_FORCED,
type: CompletionType.VALUE,
insertTextFormat: 2,
},
{
label: '3.0.3',
insertText: '3.0.3',
kind: 12,
format: CompletionFormat.QUOTED_FORCED,
type: CompletionType.VALUE,
insertTextFormat: 2,
},
{
label: '3.1.0',
insertText: '3.1.0',
kind: 12,
format: CompletionFormat.QUOTED_FORCED,
type: CompletionType.VALUE,
insertTextFormat: 2,
},
];

export default completion;
15 changes: 15 additions & 0 deletions packages/apidom-ls/src/config/openapi/openapi/lint/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import valuePattern3_0_0Lint from './value--pattern-3-0-0';
import valuePattern3_0_1Lint from './value--pattern-3-0-1';
import valuePattern3_0_2Lint from './value--pattern-3-0-2';
import valuePattern3_0_3Lint from './value--pattern-3-0-3';
import valuePattern3_1_0Lint from './value--pattern-3-1-0';

const lints = [
valuePattern3_0_0Lint,
valuePattern3_0_1Lint,
valuePattern3_0_2Lint,
valuePattern3_0_3Lint,
valuePattern3_1_0Lint,
];

export default lints;
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import ApilintCodes from '../../../codes';
import { LinterMeta } from '../../../../apidom-language-types';

// eslint-disable-next-line @typescript-eslint/naming-convention
const valuePattern3_0_0Lint: LinterMeta = {
code: ApilintCodes.OPENAPI3_0_OPENAPI_VALUE_PATTERN_3_0_0,
source: 'apilint',
message: "'openapi' value must be 3.0.0",
severity: 1,
linterFunction: 'apilintValueRegex',
linterParams: ['3\\.0\\.0'],
marker: 'value',
targetSpecs: [{ namespace: 'openapi', version: '3.0.0' }],
data: {
quickFix: [
{
message: "update to '3.0.0'",
action: 'updateValue',
functionParams: ['3.0.0'],
},
],
},
};

export default valuePattern3_0_0Lint;
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import ApilintCodes from '../../../codes';
import { LinterMeta } from '../../../../apidom-language-types';

// eslint-disable-next-line @typescript-eslint/naming-convention
const valuePattern3_0_1Lint: LinterMeta = {
code: ApilintCodes.OPENAPI3_0_OPENAPI_VALUE_PATTERN_3_0_1,
source: 'apilint',
message: "'openapi' value must be 3.0.1",
severity: 1,
linterFunction: 'apilintValueRegex',
linterParams: ['3\\.0\\.1'],
marker: 'value',
targetSpecs: [{ namespace: 'openapi', version: '3.0.1' }],
data: {
quickFix: [
{
message: "update to '3.0.1'",
action: 'updateValue',
functionParams: ['3.0.1'],
},
],
},
};

export default valuePattern3_0_1Lint;
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import ApilintCodes from '../../../codes';
import { LinterMeta } from '../../../../apidom-language-types';

// eslint-disable-next-line @typescript-eslint/naming-convention
const valuePattern3_0_2Lint: LinterMeta = {
code: ApilintCodes.OPENAPI3_0_OPENAPI_VALUE_PATTERN_3_0_2,
source: 'apilint',
message: "'openapi' value must be 3.0.2",
severity: 1,
linterFunction: 'apilintValueRegex',
linterParams: ['3\\.0\\.2'],
marker: 'value',
targetSpecs: [{ namespace: 'openapi', version: '3.0.2' }],
data: {
quickFix: [
{
message: "update to '3.0.2'",
action: 'updateValue',
functionParams: ['3.0.2'],
},
],
},
};

export default valuePattern3_0_2Lint;
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import ApilintCodes from '../../../codes';
import { LinterMeta } from '../../../../apidom-language-types';

// eslint-disable-next-line @typescript-eslint/naming-convention
const valuePattern3_0_3Lint: LinterMeta = {
code: ApilintCodes.OPENAPI3_0_OPENAPI_VALUE_PATTERN_3_0_3,
source: 'apilint',
message: "'openapi' value must be 3.0.3",
severity: 1,
linterFunction: 'apilintValueRegex',
linterParams: ['3\\.0\\.3'],
marker: 'value',
targetSpecs: [{ namespace: 'openapi', version: '3.0.3' }],
data: {
quickFix: [
{
message: "update to '3.0.3'",
action: 'updateValue',
functionParams: ['3.0.3'],
},
],
},
};

export default valuePattern3_0_3Lint;
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import ApilintCodes from '../../../codes';
import { LinterMeta } from '../../../../apidom-language-types';

// eslint-disable-next-line @typescript-eslint/naming-convention
const valuePattern3_1_0Lint: LinterMeta = {
code: ApilintCodes.OPENAPI3_1_OPENAPI_VALUE_PATTERN_3_1_0,
source: 'apilint',
message: "'openapi' value must be 3.1.0",
severity: 1,
linterFunction: 'apilintValueRegex',
linterParams: ['3\\.1\\.0'],
marker: 'value',
targetSpecs: [{ namespace: 'openapi', version: '3.1.0' }],
data: {
quickFix: [
{
message: "update to '3.1.0'",
action: 'updateValue',
functionParams: ['3.1.0'],
},
],
},
};

export default valuePattern3_1_0Lint;
10 changes: 10 additions & 0 deletions packages/apidom-ls/src/config/openapi/openapi/meta.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import lint from './lint';
import completion from './completion';
import { FormatMeta } from '../../../apidom-language-types';

const meta: FormatMeta = {
lint,
completion,
};

export default meta;
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,10 @@ const documentation = [
target: 'openapi',
docs: '**REQUIRED**. This string MUST be the [semantic version number](https://semver.org/spec/v2.0.0.html) of the [OpenAPI Specification version](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#versions) that the OpenAPI document uses. The `openapi` field SHOULD be used by tooling specifications and clients to interpret the OpenAPI document. This is *not* related to the API [`info.version`](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#infoVersion) string',
},
{
target: 'info',
docs: '[Info Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#infoObject)\n\\\n\\\n**REQUIRED**. Provides metadata about the API. The metadata MAY be used by tooling as required.',
},
{
target: 'servers',
docs: '[[Server Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#serverObject)]\n\\\n\\\nAn array of Server Objects, which provide connectivity information to a target server. If the `servers` property is not provided, or is an empty array, the default value would be a [Server Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#serverObject) with a [url](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#serverUrl) value of `/`.',
},
{
target: 'paths',
docs: '[Paths Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#pathsObject)\n\\\n\\\n**REQUIRED**. The available paths and operations for the API.',
},
{
target: 'security',
docs: '[[Security Requirement Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#securityRequirementObject)]\n\\\n\\\nA declaration of which security mechanisms can be used across the API. The list of values includes alternative security requirement objects that can be used. Only one of the security requirement objects need to be satisfied to authorize a request. Individual operations can override this definition. To make security optional, an empty security requirement (`{}`) can be included in the array.',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import ApilintCodes from '../../../codes';
import { LinterMeta } from '../../../../apidom-language-types';

const allowedFieldsLint: LinterMeta = {
code: ApilintCodes.NOT_ALLOWED_FIELDS,
source: 'apilint',
message: 'Object includes not allowed fields',
severity: 1,
linterFunction: 'allowedFields',
linterParams: [
['openapi', 'info', 'servers', 'paths', 'components', 'security', 'tags', 'externalDocs'],
'x-',
],
marker: 'key',
};

export default allowedFieldsLint;
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import ApilintCodes from '../../../codes';
import { LinterMeta } from '../../../../apidom-language-types';

const componentsTypeLint: LinterMeta = {
code: ApilintCodes.OPENAPI3_0_OPEN_API_FIELD_COMPONENTS_TYPE,
source: 'apilint',
message: 'components must be an object',
severity: 1,
linterFunction: 'apilintElementOrClass',
linterParams: ['components'],
marker: 'value',
target: 'components',
data: {},
};

export default componentsTypeLint;
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import ApilintCodes from '../../../codes';
import { LinterMeta } from '../../../../apidom-language-types';

const externalDocsTypeLint: LinterMeta = {
code: ApilintCodes.OPENAPI3_0_OPEN_API_FIELD_EXTERNAL_DOCS_TYPE,
source: 'apilint',
message: 'externalDocs must be an object',
severity: 1,
linterFunction: 'apilintElementOrClass',
linterParams: ['externalDocumentation'],
marker: 'value',
target: 'externalDocs',
data: {},
};

export default externalDocsTypeLint;
31 changes: 31 additions & 0 deletions packages/apidom-ls/src/config/openapi/openapi3_0/lint/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import allowedFieldsLint from './allowed-fields';
import infoTypeLint from './info--type';
import infoRequiredLint from './info--required';
import serversTypeLint from './servers--type';
import serversItemsTypeLint from './servers--items-type';
import pathsTypeLint from './paths--type';
import pathsRequiredLint from './paths--required';
import componentsTypeLint from './components--type';
import securityTypeLint from './security--type';
import securityItemsTypeLint from './security--items-type';
import tagsTypeLint from './tags--type';
import tagsItemsTypeLint from './tags--items-type';
import externalDocsTypeLint from './external-docs--type';

const lints = [
allowedFieldsLint,
infoTypeLint,
infoRequiredLint,
serversTypeLint,
serversItemsTypeLint,
pathsTypeLint,
pathsRequiredLint,
componentsTypeLint,
securityTypeLint,
securityItemsTypeLint,
tagsTypeLint,
tagsItemsTypeLint,
externalDocsTypeLint,
];

export default lints;
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import ApilintCodes from '../../../codes';
import { LinterMeta } from '../../../../apidom-language-types';

const infoRequiredLint: LinterMeta = {
code: ApilintCodes.OPENAPI3_0_OPEN_API_FIELD_INFO_TYPE_REQUIRED,
source: 'apilint',
message: "should always have a 'info' section",
severity: 1,
linterFunction: 'hasRequiredField',
linterParams: ['info'],
marker: 'key',
data: {
quickFix: [
{
message: "add 'info' section",
action: 'addChild',
snippetYaml: 'info: \n \n',
snippetJson: '"info": {\n \n },\n',
},
],
},
};

export default infoRequiredLint;
Loading