feat: add ignoreCase for tags-alphabetical rule (#1258)

This commit is contained in:
Arif Kurkchi
2023-09-13 13:18:07 +03:00
committed by GitHub
parent 6c1f7948e9
commit cb7e3edf42
4 changed files with 73 additions and 4 deletions

View File

@@ -0,0 +1,6 @@
---
'@redocly/openapi-core': minor
'@redocly/cli': minor
---
Added `ignoreCase` option for `tags-alphabetical` rule.

View File

@@ -33,6 +33,7 @@ This rule is intended to prevent bikeshedding and diffuse tension between teamma
|Option|Type|Description|
|---|---|---|
|severity|string|Possible values: `off`, `warn`, `error`. Default `off` (in `recommended` configuration). |
|ignoreCase|boolean|Possible values: `true`, `false`. Default `false` (in `recommended` configuration). |
An example configuration:

View File

@@ -61,4 +61,62 @@ describe('Oas3 tags-alphabetical', () => {
expect(replaceSourceWithRef(results)).toMatchInlineSnapshot(`Array []`);
});
it('should report on tags object if not sorted alphabetically not ignoring case', async () => {
const document = parseYamlToDocument(
outdent`
openapi: 3.0.0
paths: {}
tags:
- name: a
- name: B
`,
'foobar.yaml'
);
const results = await lintDocument({
externalRefResolver: new BaseResolver(),
document,
config: await makeConfig({ 'tags-alphabetical': 'error' }),
});
expect(replaceSourceWithRef(results)).toMatchInlineSnapshot(`
Array [
Object {
"location": Array [
Object {
"pointer": "#/tags/0",
"reportOnKey": false,
"source": "foobar.yaml",
},
],
"message": "The \`tags\` array should be in alphabetical order.",
"ruleId": "tags-alphabetical",
"severity": "error",
"suggest": Array [],
},
]
`);
});
it('should not report on tags object if sorted alphabetically ignoring case', async () => {
const document = parseYamlToDocument(
outdent`
openapi: 3.0.0
paths: {}
tags:
- name: a
- name: B
`,
'foobar.yaml'
);
const results = await lintDocument({
externalRefResolver: new BaseResolver(),
document,
config: await makeConfig({ 'tags-alphabetical': { severity: 'error', ignoreCase: true } }),
});
expect(replaceSourceWithRef(results)).toMatchInlineSnapshot(`Array []`);
});
});

View File

@@ -1,14 +1,14 @@
import { Oas3Rule, Oas2Rule } from '../../visitors';
import { Oas2Definition } from '../../typings/swagger';
import { Oas3Definition } from '../../typings/openapi';
import { Oas2Definition, Oas2Tag } from '../../typings/swagger';
import { Oas3Definition, Oas3Tag } from '../../typings/openapi';
import { UserContext } from '../../walk';
export const TagsAlphabetical: Oas3Rule | Oas2Rule = () => {
export const TagsAlphabetical: Oas3Rule | Oas2Rule = ({ ignoreCase = false }) => {
return {
Root(root: Oas2Definition | Oas3Definition, { report, location }: UserContext) {
if (!root.tags) return;
for (let i = 0; i < root.tags.length - 1; i++) {
if (root.tags[i].name > root.tags[i + 1].name) {
if (getTagName(root.tags[i], ignoreCase) > getTagName(root.tags[i + 1], ignoreCase)) {
report({
message: 'The `tags` array should be in alphabetical order.',
location: location.child(['tags', i]),
@@ -18,3 +18,7 @@ export const TagsAlphabetical: Oas3Rule | Oas2Rule = () => {
},
};
};
function getTagName(tag: Oas2Tag | Oas3Tag, ignoreCase: boolean): string {
return ignoreCase ? tag.name.toLowerCase() : tag.name;
}