Redocly / redocly-cli

⚒️ Redocly CLI makes OpenAPI easy. Lint/validate to any standard, generate beautiful docs, and more.
https://redocly.com/docs/cli/
MIT License
912 stars 139 forks source link

asyncapi lint support custom rules #1544

Open marianobntz opened 5 months ago

marianobntz commented 5 months ago

Currently the custom rules do not apply to asyncapi lint validation.

Any restriction (other than priorities) to add this feature?

Thanks

akemlek commented 5 months ago

Import this and it should work: import type { Async2Rule } from "@redocly/openapi-core/lib/visitors.d.js";

tatomyr commented 5 months ago

@marianobntz how exactly are you trying to apply the rules? Could you provide an example of what doesn't work for you?

marianobntz commented 4 months ago

I have this js file "custom_rules.js"

`const path = require('path');

function FileNameArtifactRule() { return { Info: { enter(operation, ctx) { const split = ctx.location.source.absoluteRef.split(path.sep); const apiFile = split[split.length - 1]; const operationName = operation["x-name"]; const splitVersion = operation.version.split("."); const operationVersion = ${splitVersion[0]}.${splitVersion[1]}; const expected = operationName + "-" + operationVersion + ".json" if (apiFile !== expected) { ctx.report({ message: El nombre del archivo "${apiFile}" debe coincidir con el info.x-name "${operationName}" y la info.versión "${operationVersion}"!, location: ctx.location.child('x-name'), suggest: [El archivo debería ser ${expected}] }) } } } } }

function AddSourceLinkDecorator() { return { Root: { leave(root) { // console.log(operation); console.log(root); // console.log(ctx.type.properties.externalDocs); root.externalDocs = { url: "http://localhost:8080" } } } } }

module.exports = { id: 'custom-rules', rules: { oas3: { 'file-name-artifact': FileNameArtifactRule, 'add-source-link': AddSourceLinkDecorator, }, async: { 'file-name-artifact': FileNameArtifactRule, 'add-source-link': AddSourceLinkDecorator, } } };

` and I use it like this:

`plugins:

rules: custom-rules/file-name-artifact: error `

This works fine when I validate openapi json files, but when I do it with asyncapi files I get this warning:

.\configuration\oas\sys\api-utils\cache-messages-1.0.json: validated in 8ms

Woohoo! Your API description is valid. 🎉

[WARNING] Unused rules found in C:\Users\m_benitez\work\git\dd-apis\resources\redocly\asyncapi-development.yaml: custom-rules/file-name-artifact. Check the spelling and verify the added plugin prefix.

Of course the validation does not run :-)

marianobntz commented 4 months ago

same thing happens when I add a custom decorator... the same warning

[WARNING] Unused rules found in C:\Users\m_benitez\work\farmacity\git\dd-apis\resources\redocly\asyncapi-development.yaml: custom-rules/file-name-artifact. [WARNING] Unused decorators found in C:\Users\m_benitez\work\farmacity\git\dd-apis\resources\redocly\asyncapi-development.yaml: custom-rules/add-source-link.

marianobntz commented 4 months ago

Import this and it should work: import type { Async2Rule } from "@redocly/openapi-core/lib/visitors.d.js";

Where do I put this line? inside the custom js ? If I put it on top of the file it breaks compilation.

Thanks!!

akemlek commented 4 months ago

There are some asyncapi rules in the lib @redocly/openapi-core/src/rules/async2. Look there and make yours :)

marianobntz commented 4 months ago

So the problem was that I was not exporting properly the rules in my js file...

The proper way to add asyncapi rules and decorators is using async2 as the key...

module.exports = { id: 'custom-rules', rules: { oas3: { 'file-name-artifact': FileNameArtifactRule, 'add-source-link': AddSourceLinkDecorator, }, async2: { 'file-name-artifact': FileNameArtifactRule, 'add-source-link': AddSourceLinkDecorator, } } }; I could not find that proper keyword anywhere in the documentation. I guess that is the root problem.

Best

tatomyr commented 4 months ago

@marianobntz sorry for the late response. Glad you found the root cause 🎉 I'd love to see your contribution to our Cookbook 🧑‍🍳📖.