mirror of
https://github.com/LukeHagar/redocly-cli.git
synced 2025-12-06 04:21:09 +00:00
feat: add ignoreCase for tags-alphabetical rule (#1258)
This commit is contained in:
6
.changeset/forty-onions-deliver.md
Normal file
6
.changeset/forty-onions-deliver.md
Normal file
@@ -0,0 +1,6 @@
|
||||
---
|
||||
'@redocly/openapi-core': minor
|
||||
'@redocly/cli': minor
|
||||
---
|
||||
|
||||
Added `ignoreCase` option for `tags-alphabetical` rule.
|
||||
@@ -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:
|
||||
|
||||
|
||||
@@ -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 []`);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user