Skip to content

Commit 305285d

Browse files
authored
Merge pull request #4993 from swagger-api/feat/SWG-15854-add-tag-validation-rules
2 parents cbb3446 + 3d0aeef commit 305285d

File tree

5 files changed

+78
-0
lines changed

5 files changed

+78
-0
lines changed

packages/apidom-ls/src/config/codes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -793,6 +793,7 @@ enum ApilintCodes {
793793
OPENAPI2_TAG_FIELD_NAME_REQUIRED,
794794
OPENAPI2_TAG_FIELD_DESCRIPTION_TYPE = 3190200,
795795
OPENAPI2_TAG_FIELD_EXTERNAL_DOCS_TYPE = 3190300,
796+
OPENAPI2_TAG_FIELD_UNIQUE_NAME_VALUE_TYPE = 3190400,
796797

797798
OPENAPI2_XML = 3200000,
798799
OPENAPI2_XML_FIELD_NAME_TYPE = 3200100,

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ import nameTypeLint from './name--type.ts';
33
import nameRequiredLint from './name--required.ts';
44
import descriptionTypeLint from './description--type.ts';
55
import externalDocsTypeLint from './external-docs--type.ts';
6+
import nameUniqueLint from './name--unique.ts';
67

78
const lints = [
89
allowedFieldsLint,
910
nameTypeLint,
1011
nameRequiredLint,
12+
nameUniqueLint,
1113
descriptionTypeLint,
1214
externalDocsTypeLint,
1315
];
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { DiagnosticSeverity } from 'vscode-languageserver-types';
2+
3+
import ApilintCodes from '../../../codes.ts';
4+
import { LinterMeta } from '../../../../apidom-language-types.ts';
5+
import { OpenAPI2, OpenAPI3 } from '../../target-specs.ts';
6+
7+
const nameUniqueLint: LinterMeta = {
8+
code: ApilintCodes.OPENAPI2_TAG_FIELD_UNIQUE_NAME_VALUE_TYPE,
9+
source: 'apilint',
10+
message: 'Tag Objects must have unique `name` field values.',
11+
severity: DiagnosticSeverity.Error,
12+
linterFunction: 'apilintPropertyUniqueSiblingValue',
13+
linterParams: ['tags', 'name'],
14+
marker: 'value',
15+
target: 'name',
16+
markerTarget: 'name',
17+
data: {},
18+
targetSpecs: [...OpenAPI2, ...OpenAPI3],
19+
};
20+
21+
export default nameUniqueLint;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
openapi: 3.0.0
2+
info:
3+
title: test
4+
version: 1.0.12
5+
tags:
6+
- name: pet
7+
description: Everything about your Pets
8+
externalDocs:
9+
description: Find out more
10+
url: http://swagger.io
11+
- name: pet
12+
description: Access to Petstore orders
13+
externalDocs:
14+
description: Find out more about our store
15+
url: http://swagger.io
16+
- name: user
17+
description: Operations about user
18+
paths:

packages/apidom-ls/test/validate.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4505,4 +4505,40 @@ describe('apidom-ls-validate', function () {
45054505

45064506
languageService.terminate();
45074507
});
4508+
4509+
it('oas - tags name should have unique values', async function () {
4510+
const spec = fs
4511+
.readFileSync(path.join(__dirname, 'fixtures', 'validation', 'oas', 'tags-unique-name.yaml'))
4512+
.toString();
4513+
const doc: TextDocument = TextDocument.create(
4514+
'foo://bar/tags-unique-name.yaml',
4515+
'yaml',
4516+
0,
4517+
spec,
4518+
);
4519+
const languageService: LanguageService = getLanguageService(contextNoSchema);
4520+
4521+
const result = await languageService.doValidation(doc);
4522+
const expected: Diagnostic[] = [
4523+
{
4524+
message: 'Tag Objects must have unique `name` field values.',
4525+
severity: 1,
4526+
code: 3190400,
4527+
source: 'apilint',
4528+
data: {},
4529+
range: { start: { line: 5, character: 10 }, end: { line: 5, character: 13 } },
4530+
},
4531+
{
4532+
message: 'Tag Objects must have unique `name` field values.',
4533+
severity: 1,
4534+
code: 3190400,
4535+
source: 'apilint',
4536+
data: {},
4537+
range: { start: { line: 10, character: 10 }, end: { line: 10, character: 13 } },
4538+
},
4539+
];
4540+
assert.deepEqual(result, expected);
4541+
4542+
languageService.terminate();
4543+
});
45084544
});

0 commit comments

Comments
 (0)