Skip to content

Commit

Permalink
fix(ls): add lint rule for OpenAPI 3.x.y Header.content field (#2388)
Browse files Browse the repository at this point in the history
  • Loading branch information
char0n authored Dec 16, 2022
1 parent fe23ea8 commit c169eb3
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/apidom-ls/src/config/codes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -847,6 +847,7 @@ enum ApilintCodes {
OPENAPI3_0_HEADER_FIELD_EXAMPLES_VALUES_TYPE = 5280900,
OPENAPI3_0_HEADER_FIELD_EXAMPLES_MUTUALLY_EXCLUSIVE,
OPENAPI3_0_HEADER_FIELD_CONTENT_VALUES_TYPE = 5281000,
OPENAPI3_0_HEADER_FIELD_CONTENT_ALLOWED_FIELDS,

OPENAPI3_0_RESPONSE = 5290000,
OPENAPI3_0_RESPONSE_FIELD_DESCRIPTION_TYPE = 5290100,
Expand Down Expand Up @@ -897,6 +898,9 @@ enum ApilintCodes {
OPENAPI3_1_PARAMETER = 7070000,
OPENAPI3_1_PARAMETER_FIELD_CONTENT_ALLOWED_FIELDS = 7070100,

OPENAPI3_1_HEADER = 7080000,
OPENAPI3_1_HEADER_FIELD_CONTENT_ALLOWED_FIELDS = 7080100,

ADS = 8000000,

ADS_INFO = 8010000,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { DiagnosticSeverity } from 'vscode-languageserver-types';

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

// eslint-disable-next-line @typescript-eslint/naming-convention
const contentAllowedFields3_0Lint: LinterMeta = {
code: ApilintCodes.OPENAPI3_0_HEADER_FIELD_CONTENT_ALLOWED_FIELDS,
source: 'apilint',
message:
'If "content" field is present, following fields are not allowed: style, explode, allowReserved, example and examples',
severity: DiagnosticSeverity.Error,
linterFunction: 'allowedFields',
linterParams: [
['description', 'required', 'deprecated', 'allowEmptyValue', 'schema', 'content', '$ref'],
'x-',
],
marker: 'key',
conditions: [
{
function: 'existFields',
params: [['content']],
},
],
targetSpecs: [
{ namespace: 'openapi', version: '3.0.0' },
{ namespace: 'openapi', version: '3.0.1' },
{ namespace: 'openapi', version: '3.0.2' },
{ namespace: 'openapi', version: '3.0.3' },
],
};

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

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

// eslint-disable-next-line @typescript-eslint/naming-convention
const contentAllowedFields3_1Lint: LinterMeta = {
code: ApilintCodes.OPENAPI3_1_HEADER_FIELD_CONTENT_ALLOWED_FIELDS,
source: 'apilint',
message:
'If "content" field is present, following fields are not allowed: style, explode, allowReserved, example and examples',
severity: DiagnosticSeverity.Error,
linterFunction: 'allowedFields',
linterParams: [
['description', 'required', 'deprecated', 'allowEmptyValue', 'schema', 'content'],
'x-',
],
marker: 'key',
conditions: [
{
function: 'existFields',
params: [['content']],
},
{
function: 'missingField',
params: ['$ref'],
},
],
targetSpecs: [{ namespace: 'openapi', version: '3.1.0' }],
};

export default contentAllowedFields3_1Lint;
4 changes: 4 additions & 0 deletions packages/apidom-ls/src/config/openapi/header/lint/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import schemaMutuallyExclusiveLint from './schema--mutually-exclusive';
import examplesValuesTypeLint from './examples--values-type';
import examplesMutuallyExclusiveLint from './examples--mutually-exclusive';
import contentValuesTypeLint from './content--values-type';
import contentAllowedFields3_0Lint from './content--allowed-fields-3-0';
import contentAllowedFields3_1Lint from './content--allowed-fields-3-1';

const lints = [
descriptionTypeLint,
Expand All @@ -27,6 +29,8 @@ const lints = [
examplesValuesTypeLint,
examplesMutuallyExclusiveLint,
contentValuesTypeLint,
contentAllowedFields3_0Lint,
contentAllowedFields3_1Lint,
requiredFieldsLint,
allowedFields3_0Lint,
allowedFields3_1Lint,
Expand Down

0 comments on commit c169eb3

Please sign in to comment.