Skip to content

Commit

Permalink
feat(ls): add allowed fields lint rule for OpenAPI 2.0 Schema Object (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
char0n committed Jan 22, 2024
1 parent f32ee83 commit f3c799f
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 5 deletions.
@@ -0,0 +1,57 @@
import { DiagnosticSeverity } from 'vscode-languageserver-types';

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

// eslint-disable-next-line @typescript-eslint/naming-convention
const allowedFieldsOpenAPI2_0Lint: LinterMeta = {
code: ApilintCodes.NOT_ALLOWED_FIELDS,
source: 'apilint',
message: 'Object includes not allowed fields',
/**
* Technically additional JSON Schema keywords can be present, they are just unsupported.
* That's why we use Warning severity here instead of Error.
*/
severity: DiagnosticSeverity.Warning,
linterFunction: 'allowedFields',
linterParams: [
[
'$ref',
'format',
'title',
'description',
'default',
'multipleOf',
'maximum',
'exclusiveMaximum',
'minimum',
'exclusiveMinimum',
'maxLength',
'minLength',
'pattern',
'maxItems',
'minItems',
'uniqueItems',
'maxProperties',
'minProperties',
'required',
'enum',
'type',
'items',
'allOf',
'properties',
'additionalProperties',
'discriminator',
'readOnly',
'xml',
'externalDocs',
'example',
],
'x-',
],
marker: 'key',
targetSpecs: OpenAPI2,
};

export default allowedFieldsOpenAPI2_0Lint;
@@ -0,0 +1,63 @@
import { DiagnosticSeverity } from 'vscode-languageserver-types';

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

// eslint-disable-next-line @typescript-eslint/naming-convention
const allowedFieldsOpenAPI3_0Lint: LinterMeta = {
code: ApilintCodes.NOT_ALLOWED_FIELDS,
source: 'apilint',
message: 'Object includes not allowed fields',
/**
* Technically additional JSON Schema keywords can be present, they are just unsupported.
* That's why we use Warning severity here instead of Error.
*/
severity: DiagnosticSeverity.Warning,
linterFunction: 'allowedFields',
linterParams: [
[
'$ref',
'title',
'multipleOf',
'maximum',
'exclusiveMaximum',
'minimum',
'exclusiveMinimum',
'maxLength',
'minLength',
'pattern',
'maxItems',
'minItems',
'uniqueItems',
'maxProperties',
'minProperties',
'required',
'enum',
'type',
'allOf',
'oneOf',
'anyOf',
'not',
'items',
'properties',
'additionalProperties',
'description',
'format',
'default',
'nullable',
'discriminator',
'readOnly',
'writeOnly',
'xml',
'externalDocs',
'example',
'deprecated',
],
'x-',
],
marker: 'key',
targetSpecs: OpenAPI30,
};

export default allowedFieldsOpenAPI3_0Lint;
Expand Up @@ -6,7 +6,7 @@ import { AsyncAPI2 } from '../../../asyncapi/target-specs';
import { OpenAPI2 } from '../../../openapi/target-specs';

// eslint-disable-next-line @typescript-eslint/naming-convention
const discriminatorTypeOpenAPI2__AsyncAPI2Lint: LinterMeta = {
const discriminatorTypeOpenAPI2_0__AsyncAPI2Lint: LinterMeta = {
code: ApilintCodes.SCHEMA_DISCRIMINATOR,
source: 'apilint',
message: "'discriminator' value must be a string",
Expand All @@ -19,4 +19,4 @@ const discriminatorTypeOpenAPI2__AsyncAPI2Lint: LinterMeta = {
targetSpecs: [...AsyncAPI2, ...OpenAPI2],
};

export default discriminatorTypeOpenAPI2__AsyncAPI2Lint;
export default discriminatorTypeOpenAPI2_0__AsyncAPI2Lint;
8 changes: 6 additions & 2 deletions packages/apidom-ls/src/config/common/schema/lint/index.ts
@@ -1,3 +1,5 @@
import allowedFieldsOpenAPI2_0Lint from './allowed-fields-openapi-2-0';
import allowedFieldsOpenAPI3_0Lint from './allowed-fields-openapi-3-0';
import $idFormatURILint from './$id--format-uri';
import $refValidLint from './$ref--valid';
import $refNoSiblingsLint from './$ref--no-siblings';
Expand All @@ -15,7 +17,7 @@ import containsTypeLint from './contains--type';
import containsTypeOpenAPI3_1__AsyncAPI2Lint from './contains--type-openapi-3-1--asyncapi-2';
import descriptionTypeLint from './description--type';
import discriminatorExistInRequiredLint from './discriminator--exist-in-required';
import discriminatorTypeOpenAPI2__AsyncAPI2Lint from './discriminator--type-openapi-2--asyncapi-2';
import discriminatorTypeOpenAPI2_0__AsyncAPI2Lint from './discriminator--type-openapi-2-0--asyncapi-2';
import discriminatorTypeOpenAPI3Lint from './discriminator--type-openapi-3';
import elseNonIfLint from './else--non-if';
import elseTypeLint from './else--type';
Expand Down Expand Up @@ -80,6 +82,8 @@ import writeOnlyTypeLint from './write-only--type';
import exampleDeprecatedLint from './example--deprecated';

const schemaLints = [
allowedFieldsOpenAPI2_0Lint,
allowedFieldsOpenAPI3_0Lint,
$idFormatURILint,
$refValidLint,
$refNoSiblingsLint,
Expand All @@ -97,7 +101,7 @@ const schemaLints = [
containsTypeOpenAPI3_1__AsyncAPI2Lint,
descriptionTypeLint,
discriminatorExistInRequiredLint,
discriminatorTypeOpenAPI2__AsyncAPI2Lint,
discriminatorTypeOpenAPI2_0__AsyncAPI2Lint,
discriminatorTypeOpenAPI3Lint,
elseNonIfLint,
elseTypeLint,
Expand Down
1 change: 0 additions & 1 deletion packages/apidom-ls/test/validate.ts
Expand Up @@ -3750,7 +3750,6 @@ describe('apidom-ls-validate', function () {
source: 'apilint',
},
];
console.log(JSON.stringify(result, null, 2));
assert.deepEqual(result, expected as Diagnostic[]);

languageService.terminate();
Expand Down

0 comments on commit f3c799f

Please sign in to comment.