Skip to content

Commit

Permalink
fix: add the possibility to skip configurable rules (#1809)
Browse files Browse the repository at this point in the history
  • Loading branch information
tatomyr authored Nov 21, 2024
1 parent 18bb9b6 commit f55be3f
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 4 deletions.
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
/.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

1 comment on commit f55be3f

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

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

Coverage report

St.
Category Percentage Covered / Total
🟡 Statements 78.78% 5016/6367
🟡 Branches 67.47% 2076/3077
🟡 Functions 73.32% 830/1132
🟡 Lines 79.08% 4734/5986

Test suite run success

812 tests passing in 121 suites.

Report generated by 🧪jest coverage report action from f55be3f

Please sign in to comment.