Skip to content

Commit 2742a4f

Browse files
fix: improve rules to pass test cases
1 parent 6f6a798 commit 2742a4f

File tree

19 files changed

+116
-28
lines changed

19 files changed

+116
-28
lines changed

packages/apidom-ls/src/config/openapi/reference/lint/$ref-3-0--request-bodies.ts renamed to packages/apidom-ls/src/config/common/schema/lint/$ref-3-0--request-bodies.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { DiagnosticSeverity } from 'vscode-languageserver-types';
22

33
import ApilintCodes from '../../../codes.ts';
44
import { LinterMeta } from '../../../../apidom-language-types.ts';
5-
import { OpenAPI3 } from '../../target-specs.ts';
5+
import { OpenAPI3 } from '../../../openapi/target-specs.ts';
66

77
const $ref3RequestBodiesLint: LinterMeta = {
88
code: ApilintCodes.OPENAPI3_0_REFERENCE_FIELD_$REF_REQUEST_BODIES,
@@ -12,6 +12,13 @@ const $ref3RequestBodiesLint: LinterMeta = {
1212
severity: DiagnosticSeverity.Error,
1313
linterFunction: 'parentExistFields',
1414
linterParams: [['requestBody']],
15+
conditions: [
16+
{
17+
targets: [{ path: '$ref' }],
18+
function: 'apilintValueRegex',
19+
params: ['^(?!.*#/components/schemas).*$'],
20+
},
21+
],
1522
marker: 'value',
1623
target: '$ref',
1724
data: {},

packages/apidom-ls/src/config/common/schema/lint/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import allowedFieldsOpenAPI3_0Lint from './allowed-fields-openapi-3-0.ts';
33
import $idFormatURILint from './$id--format-uri.ts';
44
import $refValidLint from './$ref--valid.ts';
55
import $refNoSiblingsLint from './$ref--no-siblings.ts';
6+
import $ref3RequestBodiesLint from './$ref-3-0--request-bodies.ts';
67
import additionalItemsNonArrayLint from './additional-items--non-array.ts';
78
import additionalItemsTypeLint from './additional-items--type.ts';
89
import additionalItemsTypeOpenAPI3_1__AsyncAPI2Lint from './additional-items--type-openapi-3-1--asyncapi-2.ts';
@@ -90,6 +91,7 @@ const schemaLints = [
9091
$refValidLint,
9192
$refNoSiblingsLint,
9293
$refNotUsedLint,
94+
$ref3RequestBodiesLint,
9395
additionalItemsNonArrayLint,
9496
additionalItemsTypeLint,
9597
additionalItemsTypeOpenAPI3_1__AsyncAPI2Lint,

packages/apidom-ls/src/config/openapi/reference/lint/index.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,11 @@ import $ref2_0__3_0NoSiblingsLint from './$ref-2-0--3-0--no-siblings.ts';
33
import $ref3_1AllowedSiblingsLint from './$ref-3-1--allowed-siblings.ts';
44
import description3_1TypeLint from './description-3-1--type.ts';
55
import summary3_1TypeLint from './summary-3-1--type.ts';
6-
import $ref3RequestBodiesLint from './$ref-3-0--request-bodies.ts';
7-
import $ref3RequestBodiesNamingLint from './$ref-3-0--request-bodies-naming.ts';
8-
import $ref3RequestBodiesNamingSchemaLint from './$ref-3-0--request-bodies-naming-schema.ts';
96

107
const lints = [
118
$refFormatURILint,
129
$ref2_0__3_0NoSiblingsLint,
1310
$ref3_1AllowedSiblingsLint,
14-
$ref3RequestBodiesLint,
15-
$ref3RequestBodiesNamingLint,
16-
$ref3RequestBodiesNamingSchemaLint,
1711
description3_1TypeLint,
1812
summary3_1TypeLint,
1913
];

packages/apidom-ls/src/config/openapi/request-body/lint/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@ import descriptionTypeLint from './description--type.ts';
44
import contentValuesTypeLint from './content--values-type.ts';
55
import contentRequiredLint from './content--required.ts';
66
import requiredTypeLint from './required--type.ts';
7+
import $ref3RequestBodiesNamingLint from './$ref-3-0--request-bodies-naming.ts';
8+
import $ref3RequestBodiesNamingSchemaLint from './$ref-3-0--request-bodies-naming-schema.ts';
79

810
const lints = [
11+
$ref3RequestBodiesNamingLint,
12+
$ref3RequestBodiesNamingSchemaLint,
913
descriptionTypeLint,
1014
contentRequiredLint,
1115
contentValuesTypeLint,

packages/apidom-ls/src/config/openapi/schema/lint.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ import uniqueItemsTypeLint from '../../common/schema/lint/unique-items--type.ts'
7676
import writeOnlyTypeLint from '../../common/schema/lint/write-only--type.ts';
7777
import exampleDeprecatedLint from '../../common/schema/lint/example--deprecated.ts';
7878
import $refNotUsedLint from '../../common/schema/lint/$ref--not-used.ts';
79+
import $ref3RequestBodiesLint from '../../common/schema/lint/$ref-3-0--request-bodies.ts';
7980
import { OpenAPI31 } from '../target-specs.ts';
8081

8182
const schemaLints = [
@@ -156,6 +157,7 @@ const schemaLints = [
156157
writeOnlyTypeLint,
157158
exampleDeprecatedLint,
158159
$refNotUsedLint,
160+
$ref3RequestBodiesLint,
159161
];
160162

161163
export default schemaLints;

packages/apidom-ls/src/services/validation/linter-functions.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,20 +1147,21 @@ export const standardLinterfunctions: FunctionItem[] = [
11471147
},
11481148
{
11491149
functionName: 'apilintReferenceNotUsed',
1150-
function: (element) => {
1151-
const elParent = element.parent?.parent?.parent?.parent;
1152-
if (!elParent.hasKey || !elParent.hasKey('schemas')) {
1150+
function: (element: Element & { content?: { key?: string } }) => {
1151+
const elParent: Element = element.parent?.parent?.parent?.parent;
1152+
if (typeof elParent.hasKey !== 'function' || !elParent.hasKey('schemas')) {
11531153
return true;
11541154
}
11551155

11561156
const api = root(element);
1157-
const isReferenceElement = (el: Element) => el.element === 'reference';
1157+
const isReferenceElement = (el: Element & { content?: { key?: string } }) =>
1158+
toValue(el.content.key) === '$ref';
11581159
const referenceElements = filter((el) => {
11591160
return isReferenceElement(el);
11601161
}, api);
11611162
const referenceNames = referenceElements.map((refElement: Element) =>
11621163
// @ts-expect-error
1163-
toValue(refElement.get('$ref')).split('/').at(-1),
1164+
toValue(refElement.content.value).split('/').at(-1),
11641165
);
11651166
// @ts-expect-error
11661167
return referenceNames.includes(toValue(element.parent.key));

packages/apidom-ls/test/custom-rules.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ describe('apidom-ls-validate-custom-rules', function () {
8787
},
8888
{
8989
range: {
90-
start: { line: 13, character: 8 },
91-
end: { line: 13, character: 17 },
90+
start: { line: 15, character: 8 },
91+
end: { line: 15, character: 17 },
9292
},
9393
message: 'properties MUST follow camelCase',
9494
severity: 1,
@@ -98,8 +98,8 @@ describe('apidom-ls-validate-custom-rules', function () {
9898
},
9999
{
100100
range: {
101-
start: { line: 18, character: 4 },
102-
end: { line: 18, character: 7 },
101+
start: { line: 20, character: 4 },
102+
end: { line: 20, character: 7 },
103103
},
104104
message: 'Parameter Object must contain one of the following fields: content, schema',
105105
severity: 1,
@@ -118,8 +118,8 @@ describe('apidom-ls-validate-custom-rules', function () {
118118
},
119119
{
120120
range: {
121-
start: { line: 20, character: 6 },
122-
end: { line: 20, character: 10 },
121+
start: { line: 22, character: 6 },
122+
end: { line: 22, character: 10 },
123123
},
124124
message: 'parameter names MUST follow camelCase',
125125
severity: 1,
@@ -129,8 +129,8 @@ describe('apidom-ls-validate-custom-rules', function () {
129129
},
130130
{
131131
range: {
132-
start: { line: 21, character: 4 },
133-
end: { line: 21, character: 8 },
132+
start: { line: 23, character: 4 },
133+
end: { line: 23, character: 8 },
134134
},
135135
message: 'Parameter Object must contain one of the following fields: content, schema',
136136
severity: 1,
@@ -149,8 +149,8 @@ describe('apidom-ls-validate-custom-rules', function () {
149149
},
150150
{
151151
range: {
152-
start: { line: 24, character: 4 },
153-
end: { line: 24, character: 7 },
152+
start: { line: 26, character: 4 },
153+
end: { line: 26, character: 7 },
154154
},
155155
message: 'Parameter Object must contain one of the following fields: content, schema',
156156
severity: 1,
@@ -200,31 +200,31 @@ describe('apidom-ls-validate-custom-rules', function () {
200200
const result = await languageService.doValidation(docOpenapi, validationContext);
201201
const expected: Diagnostic[] = [
202202
{
203-
range: { start: { line: 23, character: 6 }, end: { line: 23, character: 10 } },
203+
range: { start: { line: 25, character: 6 }, end: { line: 25, character: 10 } },
204204
message: 'parameter names MUST follow camelCase',
205205
severity: 1,
206206
code: 20002,
207207
source: 'apilint',
208208
data: {},
209209
},
210210
{
211-
range: { start: { line: 32, character: 6 }, end: { line: 32, character: 10 } },
211+
range: { start: { line: 34, character: 6 }, end: { line: 34, character: 10 } },
212212
message: 'parameter names MUST follow camelCase',
213213
severity: 1,
214214
code: 20002,
215215
source: 'apilint',
216216
data: {},
217217
},
218218
{
219-
range: { start: { line: 16, character: 8 }, end: { line: 16, character: 17 } },
219+
range: { start: { line: 18, character: 8 }, end: { line: 18, character: 17 } },
220220
message: 'keys MUST follow camelCase',
221221
severity: 1,
222222
code: 20001,
223223
source: 'apilint',
224224
data: {},
225225
},
226226
{
227-
range: { start: { line: 31, character: 4 }, end: { line: 31, character: 7 } },
227+
range: { start: { line: 33, character: 4 }, end: { line: 33, character: 7 } },
228228
message: 'keys MUST follow camelCase',
229229
severity: 1,
230230
code: 20001,

packages/apidom-ls/test/fixtures/custom-rules-jsonpath.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ paths:
99
/keb-ab:
1010
get:
1111
operationId: kebab
12+
/endpoint:
13+
$ref: '#/components/schemas/User'
1214
components:
1315
schemas:
1416
User:

0 commit comments

Comments
 (0)