Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add the possibility to skip configurable rules #1809

Merged
merged 1 commit into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/big-peas-trade.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@redocly/openapi-core": patch
"@redocly/cli": patch
---

Added the possibility to skip configurable rules using the `--skip-rule` option.
4 changes: 0 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,3 @@ output/
*.tgz
redoc-static.html
packages/cli/README.md
/redocly.yaml
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove it as it makes it harder to find the current configuration file changes.

/.redocly.yaml
/redocly.yml
/.redocly.yml
16 changes: 16 additions & 0 deletions __tests__/commands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,22 @@ describe('E2E', () => {
join(folderPath, 'snapshot.js')
);
});

test('skip-rules', () => {
const dirName = 'skip-rules';
const folderPath = join(__dirname, `lint/${dirName}`);

const args = getParams('../../../packages/cli/src/index.ts', 'lint', [
'--skip-rule=operation-4xx-response',
'--skip-rule',
'rule/operationId-casing',
]);

const result = getCommandOutput(args, folderPath);
(expect(cleanupOutput(result)) as any).toMatchSpecificSnapshot(
join(folderPath, 'snapshot.js')
);
});
});

describe('zero-config', () => {
Expand Down
17 changes: 17 additions & 0 deletions __tests__/lint/skip-rules/openapi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
openapi: 3.0.0
info:
title: Sample API
version: 1.0.0
description: It should skip built-in and configurable rules.
paths:
/users:
get:
summary: Retrieve a list of users
operationId: camelCase
responses:
'200':
description: A list of users
content:
application/json:
schema:
type: object
11 changes: 11 additions & 0 deletions __tests__/lint/skip-rules/redocly.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apis:
main:
root: ./openapi.yaml
rules:
rule/operationId-casing:
subject:
type: Operation
property: operationId
assertions:
casing: PascalCase
operation-4xx-response: error
50 changes: 50 additions & 0 deletions __tests__/lint/skip-rules/snapshot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`E2E lint skip-rules 1`] = `

validating /openapi.yaml...
[1] openapi.yaml:10:20 at #/paths/~1users/get/operationId

rule/operationId-casing failed because the Operation operationId didn't meet the assertions: "camelCase" should use PascalCase

8 | get:
9 | summary: Retrieve a list of users
10 | operationId: camelCase
| ^^^^^^^^^
11 | responses:
12 | '200':

Error was generated by the rule/operationId-casing rule.


[2] openapi.yaml:11:7 at #/paths/~1users/get/responses

Operation must have at least one \`4XX\` response.

9 | summary: Retrieve a list of users
10 | operationId: camelCase
11 | responses:
| ^^^^^^^^^
12 | '200':
13 | description: A list of users

Error was generated by the operation-4xx-response rule.


/openapi.yaml: validated in <test>ms

❌ Validation failed with 2 errors.
run \`redocly lint --generate-ignore-file\` to add all problems to the ignore file.


`;

exports[`E2E lint skip-rules 2`] = `

validating /openapi.yaml...
/openapi.yaml: validated in <test>ms

Woohoo! Your API description is valid. 🎉


`;
7 changes: 7 additions & 0 deletions packages/core/src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,13 @@ export class StyleguideConfig {
for (const version of Object.values(SpecVersion)) {
if (this.rules[version][ruleId]) {
this.rules[version][ruleId] = 'off';
} else if (Array.isArray(this.rules[version].assertions)) {
// skip assertions
for (const configurableRule of this.rules[version].assertions) {
if (configurableRule.assertionId === ruleId) {
configurableRule.severity = 'off';
}
}
}
}
}
Expand Down
Loading