Skip to content

Commit

Permalink
Merge pull request #209 from JaredCE/allow-configurable-redocly-rules
Browse files Browse the repository at this point in the history
Allow configurable redocly rules
  • Loading branch information
JaredCE authored Jun 17, 2024
2 parents 7faab6a + a7f784a commit 8f4a1b1
Show file tree
Hide file tree
Showing 7 changed files with 801 additions and 606 deletions.
29 changes: 22 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -942,16 +942,31 @@ This will set the `Cache-Control` Response Header to have a value of "no-store"

Validation for the OpenAPI Description is now (as of 0.0.90) done by [Redocly](https://redocly.com/). This is a slightly less opinionated validator for an OpenAPI Description, it should result in less errors around "YAML Anchors". It's also a maintained library, and has support for OpenAPI 3.1.0 which I hope to be able to support very soon.

I am making use of https://www.npmjs.com/package/@redocly/openapi-core, which I have been warned is likely to change. If you notice anything going wrong with validation of your OpenAPI Description, feel free to open an issue here. I make active use of this library, so will hopefully come across those issues too.

### Rules

I have configured the validator to use these Rules:

* [spec](https://redocly.com/docs/cli/rules/spec/)
* [path-parameters-defined](https://redocly.com/docs/cli/rules/path-parameters-defined/)
* [operation-2xx-response](https://redocly.com/docs/cli/rules/operation-2xx-response/)
* [operation-4xx-response](https://redocly.com/docs/cli/rules/operation-4xx-response/)
* [operation-operationId-unique](https://redocly.com/docs/cli/rules/operation-operationid-unique/)
* [path-declaration-must-exist](https://redocly.com/docs/cli/rules/path-declaration-must-exist/)
- [spec](https://redocly.com/docs/cli/rules/spec/)
- [path-parameters-defined](https://redocly.com/docs/cli/rules/path-parameters-defined/)
- [operation-2xx-response](https://redocly.com/docs/cli/rules/operation-2xx-response/)
- [operation-4xx-response](https://redocly.com/docs/cli/rules/operation-4xx-response/)
- [operation-operationId-unique](https://redocly.com/docs/cli/rules/operation-operationid-unique/)
- [path-declaration-must-exist](https://redocly.com/docs/cli/rules/path-declaration-must-exist/)

I am making use of https://www.npmjs.com/package/@redocly/openapi-core, which I have been warned is likely to change. If you notice anything going wrong with validation of your OpenAPI Description, feel free to open an issue here. I make active use of this library, so will hopefully come across those issues too.
However, you can configure your own rules from the [ruleset available on the Redocly site](https://redocly.com/docs/cli/rules/built-in-rules/). To do this, you will need to create a `redocly.json` file within an `options` folder. The file should look like:

```json
{
"spec": "error",
"path-parameters-defined": "error",
"operation-2xx-response": "error",
"operation-4xx-response": "error",
"operation-operationId-unique": "error",
"path-declaration-must-exist": "error"
}
```

## Example configuration

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "serverless-openapi-documenter",
"version": "0.0.97",
"version": "0.0.100",
"description": "Generate OpenAPI v3 documentation and Postman Collections from your Serverless Config",
"main": "index.js",
"keywords": [
Expand Down
24 changes: 14 additions & 10 deletions src/definitionGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,19 @@ class DefinitionGenerator {
},
};

try {
this.REDOCLY_RULES = require(path.resolve("options", "redocly.json"));
} catch (err) {
this.REDOCLY_RULES = {
spec: "error",
"path-parameters-defined": "error",
"operation-2xx-response": "error",
"operation-4xx-response": "error",
"operation-operationId-unique": "error",
"path-declaration-must-exist": "error",
};
}

try {
this.refParserOptions = require(path.resolve("options", "ref-parser.js"));
} catch (err) {
Expand Down Expand Up @@ -1003,16 +1016,7 @@ class DefinitionGenerator {
async validate() {
const config = await createConfig({
apis: {},
// styleguide: {
rules: {
spec: "error",
"path-parameters-defined": "error",
"operation-2xx-response": "error",
"operation-4xx-response": "error",
"operation-operationId-unique": "error",
"path-declaration-must-exist": "error",
},
// },
rules: this.REDOCLY_RULES,
});

const apiDesc = stringifyYaml(this.openAPI);
Expand Down
4 changes: 4 additions & 0 deletions test/helpers/redocly.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"spec": "error",
"operation-2xx-response": "warn"
}
Loading

0 comments on commit 8f4a1b1

Please sign in to comment.