Skip to content

Commit

Permalink
feat(ls): provide OpenAPI 3.1.0 License lint rules (#2136)
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-lai authored Oct 18, 2022
1 parent 4b3b527 commit 20a09d5
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 3 deletions.
3 changes: 3 additions & 0 deletions packages/apidom-ls/src/config/codes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,9 @@ enum ApilintCodes {
OPENAPI3_1_COMPONENTS = 7020000,
OPENAPI3_1_COMPONENTS_FIELD_PATH_ITEMS_VALUES_TYPE = 7020100,

OPENAPI3_1_LICENSE_FIELD_IDENTIFIER_TYPE = 7040300,
OPENAPI3_1_LICENSE_FIELD_IDENTIFIER_MUTUALLY_EXCLUSIVE = 7040600,

ADS = 8000000,
ADS_INFO = 8010000,
ADS_INFO_REQUIRED = 8010010,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import ApilintCodes from '../../../codes';
import { LinterMeta } from '../../../../apidom-language-types';

// eslint-disable-next-line @typescript-eslint/naming-convention
const allowedFields3_1Lint: LinterMeta = {
code: ApilintCodes.NOT_ALLOWED_FIELDS,
source: 'apilint',
message: 'Object includes not allowed fields',
severity: 1,
linterFunction: 'allowedFields',
linterParams: [['name', 'url', 'identifier'], 'x-'],
marker: 'key',
targetSpecs: [{ namespace: 'openapi', version: '3.1.0' }],
};

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

const identifierMutuallyExclusiveLint: LinterMeta = {
code: ApilintCodes.OPENAPI3_1_LICENSE_FIELD_IDENTIFIER_MUTUALLY_EXCLUSIVE,
source: 'apilint',
message: 'The identifier field and url field are mutually exclusive.',
severity: 1,
linterFunction: 'missingFields',
linterParams: [['identifier']],
marker: 'key',
markerTarget: 'identifier',
conditions: [
{
function: 'existFields',
params: [['url']],
},
],
targetSpecs: [{ namespace: 'openapi', version: '3.1.0' }],
};

export default identifierMutuallyExclusiveLint;
Empty file.
11 changes: 10 additions & 1 deletion packages/apidom-ls/src/config/openapi/license/lint/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import allowedFields3_0Lint from './allowed-fields-3-0';
import allowedFields3_1Lint from './allowed-fields-3-1';
import nameTypeLint from './name--type';
import nameRequiredLint from './name--required';
import identifierMutuallyExclusiveLint from './identifier--mutually-exclusive';
import urlFormatURILint from './url--format-uri';

const lints = [nameTypeLint, nameRequiredLint, urlFormatURILint, allowedFields3_0Lint];
const lints = [
nameTypeLint,
nameRequiredLint,
identifierMutuallyExclusiveLint,
urlFormatURILint,
allowedFields3_0Lint,
allowedFields3_1Lint,
];

export default lints;
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import ApilintCodes from '../../../codes';
import { LinterMeta } from '../../../../apidom-language-types';

const nameTypeLint: LinterMeta = {
code: ApilintCodes.OPENAPI3_0_LICENSE_FIELD_NAME_TYPE,
code: ApilintCodes.OPENAPI3_1_LICENSE_FIELD_IDENTIFIER_TYPE,
source: 'apilint',
message: "'name' must be a string",
message: "'identifier' must be a string",
severity: 1,
linterFunction: 'apilintType',
linterParams: ['string'],
Expand Down
16 changes: 16 additions & 0 deletions packages/apidom-ls/test/openapi-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,22 @@ describe('apidom-ls', function () {
severity: 1,
source: 'apilint',
},
{
code: 7040600,
message: 'The identifier field and url field are mutually exclusive.',
range: {
end: {
character: 18,
line: 14,
},
start: {
character: 6,
line: 14,
},
},
severity: 1,
source: 'apilint',
},
{
code: 5130600,
data: {},
Expand Down
16 changes: 16 additions & 0 deletions packages/apidom-ls/test/openapi-yaml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,22 @@ describe('apidom-ls-yaml', function () {
severity: 1,
source: 'apilint',
},
{
code: 7040600,
message: 'The identifier field and url field are mutually exclusive.',
range: {
end: {
character: 14,
line: 15,
},
start: {
character: 4,
line: 15,
},
},
severity: 1,
source: 'apilint',
},
{
code: 5130600,
data: {},
Expand Down
16 changes: 16 additions & 0 deletions packages/apidom-ls/test/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2870,6 +2870,22 @@ describe('apidom-ls-validate', function () {

const result = await languageService.doValidation(doc, validationContext);
const expected: Diagnostic[] = [
{
code: 7040600,
message: 'The identifier field and url field are mutually exclusive.',
range: {
end: {
character: 14,
line: 6,
},
start: {
character: 4,
line: 6,
},
},
severity: 1,
source: 'apilint',
},
{
range: {
start: {
Expand Down

1 comment on commit 20a09d5

@char0n
Copy link
Member

@char0n char0n commented on 20a09d5 Oct 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've issued a following PR against this commit: #2155

Things addressed:

  • OpenAPI 3.1.0 License object range was not created (only fields ranges were created)
  • Licese objects field ranges where not set correctly (starting from 100)
  • removed the email completion rule as it's not part of License object
  • added missing identifierTypeLint

Please sign in to comment.