redocly-cli icon indicating copy to clipboard operation
redocly-cli copied to clipboard

asyncapi lint support custom rules

Open marianobntz opened this issue 1 year ago • 8 comments

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

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

Thanks

marianobntz avatar May 02 '24 22:05 marianobntz

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

akemlek avatar May 03 '24 07:05 akemlek

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

tatomyr avatar May 03 '24 08:05 tatomyr

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:

  • 'plugins/custom-rules.js'

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 avatar May 06 '24 13:05 marianobntz

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 avatar May 06 '24 13:05 marianobntz

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!!

marianobntz avatar May 06 '24 13:05 marianobntz

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

akemlek avatar May 06 '24 13:05 akemlek

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

marianobntz avatar May 06 '24 14:05 marianobntz

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

tatomyr avatar May 09 '24 16:05 tatomyr