Skip to content
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

feat(ls): add lint rules for OpenAPI 2.0 Parameter Object #3602

Merged
merged 3 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
30 changes: 30 additions & 0 deletions packages/apidom-ls/src/config/codes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,35 @@ enum ApilintCodes {
OPENAPI2_EXTERNAL_DOCUMENTATION_FIELD_URL_FORMAT_URI = 3090200,
OPENAPI2_EXTERNAL_DOCUMENTATION_FIELD_URL_REQUIRED,

OPENAPI2_PARAMETER = 3100000,
OPENAPI2_PARAMETER_FIELD_NAME_TYPE = 3100100,
OPENAPI2_PARAMETER_FIELD_NAME_REQUIRED,
OPENAPI2_PARAMETER_FIELD_IN_EQUALS = 3100200,
OPENAPI2_PARAMETER_FIELD_IN_REQUIRED,
OPENAPI2_PARAMETER_FIELD_IN_VALID,
OPENAPI2_PARAMETER_FIELD_DESCRIPTION_TYPE = 3100300,
OPENAPI2_PARAMETER_FIELD_REQUIRED_TYPE = 3100400,
OPENAPI2_PARAMETER_FIELD_REQUIRED_REQUIRED,
OPENAPI2_PARAMETER_FIELD_REQUIRED_EQUALS,
OPENAPI2_PARAMETER_FIELD_SCHEMA_TYPE = 3100500,
OPENAPI2_PARAMETER_FIELD_SCHEMA_REQUIRED,
OPENAPI2_PARAMETER_FIELD_TYPE_EQUALS = 3100600,
OPENAPI2_PARAMETER_FIELD_TYPE_REQUIRED,
OPENAPI2_PARAMETER_FIELD_FORMAT_TYPE = 3100700,
OPENAPI2_PARAMETER_FIELD_ALLOW_EMPTY_VALUE_TYPE = 3100800,
OPENAPI2_PARAMETER_FIELD_ITEMS_TYPE = 3100900,
OPENAPI2_PARAMETER_FIELD_ITEMS_REQUIRED,
OPENAPI2_PARAMETER_FIELD_COLLECTION_FORMAT_EQUALS = 3101000,
OPENAPI2_PARAMETER_FIELD_MAXIMUM_TYPE = 3101100,
OPENAPI2_PARAMETER_FIELD_EXCLUSIVE_MAXIMUM_TYPE = 3101200,
OPENAPI2_PARAMETER_FIELD_MINIMUM_TYPE = 3101300,
OPENAPI2_PARAMETER_FIELD_EXCLUSIVE_MINIMUM_TYPE = 3101400,
OPENAPI2_PARAMETER_FIELD_MAX_LENGTH_TYPE = 3101500,
OPENAPI2_PARAMETER_FIELD_MIN_LENGTH_TYPE = 3101600,
OPENAPI2_PARAMETER_FIELD_UNIQUE_ITEMS_TYPE = 3101700,
OPENAPI2_PARAMETER_FIELD_ENUM_TYPE = 3101800,
OPENAPI2_PARAMETER_FIELD_MULTIPLE_OF_TYPE = 3101900,

OPENAPI3_0 = 5000000,

OPENAPI3_0_OPENAPI_VALUE_PATTERN_3_0_0 = 5000100,
Expand Down Expand Up @@ -838,6 +867,7 @@ enum ApilintCodes {
OPENAPI3_0_PARAMETER_FIELD_NAME_REQUIRED,
OPENAPI3_0_PARAMETER_FIELD_IN_TYPE = 5150300,
OPENAPI3_0_PARAMETER_FIELD_IN_REQUIRED,
OPENAPI3_0_PARAMETER_FIELD_IN_EQUALS,
OPENAPI3_0_PARAMETER_FIELD_DESCRIPTION_TYPE = 5150400,
OPENAPI3_0_PARAMETER_FIELD_REQUIRED_TYPE = 5150500,
OPENAPI3_0_PARAMETER_FIELD_REQUIRED_REQUIRED,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { DiagnosticSeverity } from 'vscode-languageserver-types';

import ApilintCodes from '../../../codes';
import { LinterMeta } from '../../../../apidom-language-types';
import { OpenAPI3 } from '../../target-specs';
import { OpenAPI2, OpenAPI3 } from '../../target-specs';

const allowEmptyValueTypeLint: LinterMeta = {
code: ApilintCodes.OPENAPI3_0_PARAMETER_FIELD_ALLOW_EMPTY_VALUE_TYPE,
code: ApilintCodes.OPENAPI2_PARAMETER_FIELD_ALLOW_EMPTY_VALUE_TYPE,
source: 'apilint',
message: 'allowEmptyValue must be a boolean',
severity: DiagnosticSeverity.Error,
Expand All @@ -14,7 +14,7 @@ const allowEmptyValueTypeLint: LinterMeta = {
marker: 'value',
target: 'allowEmptyValue',
data: {},
targetSpecs: OpenAPI3,
targetSpecs: [...OpenAPI2, ...OpenAPI3],
};

export default allowEmptyValueTypeLint;
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { DiagnosticSeverity } from 'vscode-languageserver-types';

import ApilintCodes from '../../../codes';
import { LinterMeta } from '../../../../apidom-language-types';
import { OpenAPI2 } from '../../target-specs';

// eslint-disable-next-line @typescript-eslint/naming-convention
const allowedFields2_0Lint: LinterMeta = {
code: ApilintCodes.NOT_ALLOWED_FIELDS,
source: 'apilint',
message: 'Object includes not allowed fields',
severity: DiagnosticSeverity.Error,
linterFunction: 'allowedFields',
linterParams: [
[
'name',
'in',
'description',
'required',
'schema',
'type',
'format',
'allowEmptyValue',
'items',
'collectionFormat',
'default',
'maximum',
'exclusiveMaximum',
'minimum',
'exclusiveMinimum',
'maxLength',
'minLength',
'pattern',
'maxItems',
'minItems',
'uniqueItems',
'enum',
'multipleOf',
'$ref',
],
'x-',
],
marker: 'key',
targetSpecs: OpenAPI2,
};

export default allowedFields2_0Lint;
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { DiagnosticSeverity } from 'vscode-languageserver-types';

import ApilintCodes from '../../../codes';
import { LinterMeta } from '../../../../apidom-language-types';
import { OpenAPI2 } from '../../target-specs';

const collectionFormatEqualsLint: LinterMeta = {
code: ApilintCodes.OPENAPI2_PARAMETER_FIELD_COLLECTION_FORMAT_EQUALS,
source: 'apilint',
message:
"'collectionFormat' must be one of allowed values: 'csv', 'ssv', 'tsv', 'pipes', 'multi'",
severity: DiagnosticSeverity.Error,
linterFunction: 'apilintValueOrArray',
linterParams: [['csv', 'ssv', 'tsv', 'pipes', 'multi']],
marker: 'value',
target: 'collectionFormat',
data: {},
targetSpecs: OpenAPI2,
};

export default collectionFormatEqualsLint;
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { DiagnosticSeverity } from 'vscode-languageserver-types';

import ApilintCodes from '../../../codes';
import { LinterMeta } from '../../../../apidom-language-types';
import { OpenAPI3 } from '../../target-specs';
import { OpenAPI2, OpenAPI3 } from '../../target-specs';

const descriptionTypeLint: LinterMeta = {
code: ApilintCodes.OPENAPI3_0_PARAMETER_FIELD_DESCRIPTION_TYPE,
code: ApilintCodes.OPENAPI2_PARAMETER_FIELD_DESCRIPTION_TYPE,
source: 'apilint',
message: 'description must be a string',
severity: DiagnosticSeverity.Error,
Expand All @@ -14,7 +14,7 @@ const descriptionTypeLint: LinterMeta = {
marker: 'value',
target: 'description',
data: {},
targetSpecs: OpenAPI3,
targetSpecs: [...OpenAPI2, ...OpenAPI3],
};

export default descriptionTypeLint;
20 changes: 20 additions & 0 deletions packages/apidom-ls/src/config/openapi/parameter/lint/enum--type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { DiagnosticSeverity } from 'vscode-languageserver-types';

import ApilintCodes from '../../../codes';
import { LinterMeta } from '../../../../apidom-language-types';
import { OpenAPI2 } from '../../target-specs';

const enumTypeLint: LinterMeta = {
code: ApilintCodes.OPENAPI2_PARAMETER_FIELD_ENUM_TYPE,
source: 'apilint',
message: 'enum must be an array',
severity: DiagnosticSeverity.Error,
linterFunction: 'apilintType',
linterParams: ['array'],
marker: 'value',
target: 'enum',
data: {},
targetSpecs: OpenAPI2,
};

export default enumTypeLint;
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { DiagnosticSeverity } from 'vscode-languageserver-types';

import ApilintCodes from '../../../codes';
import { LinterMeta } from '../../../../apidom-language-types';
import { OpenAPI2 } from '../../target-specs';

const exclusiveMaximumTypeLint: LinterMeta = {
code: ApilintCodes.OPENAPI2_PARAMETER_FIELD_EXCLUSIVE_MAXIMUM_TYPE,
source: 'apilint',
message: 'exclusiveMaximum must be a boolean',
severity: DiagnosticSeverity.Error,
linterFunction: 'apilintType',
linterParams: ['boolean'],
marker: 'value',
target: 'exclusiveMaximum',
data: {},
targetSpecs: OpenAPI2,
};

export default exclusiveMaximumTypeLint;
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { DiagnosticSeverity } from 'vscode-languageserver-types';

import ApilintCodes from '../../../codes';
import { LinterMeta } from '../../../../apidom-language-types';
import { OpenAPI2 } from '../../target-specs';

const exclusiveMinimumTypeLint: LinterMeta = {
code: ApilintCodes.OPENAPI2_PARAMETER_FIELD_EXCLUSIVE_MINIMUM_TYPE,
source: 'apilint',
message: 'exclusiveMinimum must be a boolean',
severity: DiagnosticSeverity.Error,
linterFunction: 'apilintType',
linterParams: ['boolean'],
marker: 'value',
target: 'exclusiveMinimum',
data: {},
targetSpecs: OpenAPI2,
};

export default exclusiveMinimumTypeLint;
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ import { DiagnosticSeverity } from 'vscode-languageserver-types';

import ApilintCodes from '../../../codes';
import { LinterMeta } from '../../../../apidom-language-types';
import { OpenAPI3 } from '../../target-specs';
import { OpenAPI2 } from '../../target-specs';

const inTypeLint: LinterMeta = {
code: ApilintCodes.OPENAPI3_0_PARAMETER_FIELD_IN_TYPE,
const formatTypeLint: LinterMeta = {
code: ApilintCodes.OPENAPI2_PARAMETER_FIELD_FORMAT_TYPE,
source: 'apilint',
message: 'in must be a string',
message: 'explode must be a string',
char0n marked this conversation as resolved.
Show resolved Hide resolved
severity: DiagnosticSeverity.Error,
linterFunction: 'apilintType',
linterParams: ['string'],
marker: 'value',
target: 'in',
target: 'format',
data: {},
targetSpecs: OpenAPI3,
targetSpecs: OpenAPI2,
};

export default inTypeLint;
export default formatTypeLint;
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { DiagnosticSeverity } from 'vscode-languageserver-types';

import ApilintCodes from '../../../codes';
import { LinterMeta } from '../../../../apidom-language-types';
import { OpenAPI2 } from '../../target-specs';

// eslint-disable-next-line @typescript-eslint/naming-convention
const inEquals2_0Lint: LinterMeta = {
code: ApilintCodes.OPENAPI2_PARAMETER_FIELD_IN_EQUALS,
source: 'apilint',
message: "'in' must be one of allowed values: query, header, path, formData, body",
severity: DiagnosticSeverity.Error,
linterFunction: 'apilintValueOrArray',
linterParams: [['query', 'header', 'path', 'formData', 'body']],
marker: 'value',
target: 'in',
data: {},
targetSpecs: OpenAPI2,
};

export default inEquals2_0Lint;
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { DiagnosticSeverity } from 'vscode-languageserver-types';

import ApilintCodes from '../../../codes';
import { LinterMeta } from '../../../../apidom-language-types';
import { OpenAPI3 } from '../../target-specs';

// eslint-disable-next-line @typescript-eslint/naming-convention
const inEquals3_0__3_1Lint: LinterMeta = {
code: ApilintCodes.OPENAPI3_0_PARAMETER_FIELD_IN_EQUALS,
source: 'apilint',
message: "'in' must be one of allowed values: query, header, path, cookie",
severity: DiagnosticSeverity.Error,
linterFunction: 'apilintValueOrArray',
linterParams: [['query', 'header', 'path', 'cookie']],
marker: 'value',
target: 'in',
data: {},
targetSpecs: OpenAPI3,
};

export default inEquals3_0__3_1Lint;
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { DiagnosticSeverity } from 'vscode-languageserver-types';

import ApilintCodes from '../../../codes';
import { LinterMeta } from '../../../../apidom-language-types';
import { OpenAPI3 } from '../../target-specs';
import { OpenAPI2, OpenAPI3 } from '../../target-specs';

const inRequiredLint: LinterMeta = {
code: ApilintCodes.OPENAPI3_0_PARAMETER_FIELD_IN_REQUIRED,
code: ApilintCodes.OPENAPI2_PARAMETER_FIELD_IN_REQUIRED,
source: 'apilint',
message: "should always have an 'in'",
severity: DiagnosticSeverity.Error,
Expand All @@ -28,7 +28,7 @@ const inRequiredLint: LinterMeta = {
params: ['$ref'],
},
],
targetSpecs: OpenAPI3,
targetSpecs: [...OpenAPI2, ...OpenAPI3],
};

export default inRequiredLint;
31 changes: 31 additions & 0 deletions packages/apidom-ls/src/config/openapi/parameter/lint/in--valid.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { DiagnosticSeverity } from 'vscode-languageserver-types';

import ApilintCodes from '../../../codes';
import { LinterMeta } from '../../../../apidom-language-types';
import { OpenAPI2 } from '../../target-specs';

const inValidLint: LinterMeta = {
code: ApilintCodes.OPENAPI2_PARAMETER_FIELD_IN_VALID,
source: 'apilint',
message: "'in' field must contain 'formData' value for 'type'=file",
severity: DiagnosticSeverity.Error,
linterFunction: 'apilintContainsValue',
linterParams: ['formData'],
marker: 'value',
target: 'in',
data: {},
conditions: [
{
targets: [{ path: 'type' }],
function: 'apilintContainsValue',
params: ['file'],
},
{
function: 'missingField',
params: ['$ref'],
},
],
targetSpecs: OpenAPI2,
};

export default inValidLint;
Loading