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

asyncapi lint support custom rules #1544

Open
marianobntz opened this issue May 2, 2024 · 8 comments
Open

asyncapi lint support custom rules #1544

marianobntz opened this issue May 2, 2024 · 8 comments
Labels
question Further information is requested Type: Docs Type: Enhancement

Comments

@marianobntz
Copy link

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

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

Thanks

@akemlek
Copy link

akemlek commented May 3, 2024

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

@tatomyr
Copy link
Contributor

tatomyr commented May 3, 2024

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

@tatomyr tatomyr added the question Further information is requested label May 3, 2024
@marianobntz
Copy link
Author

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
Copy link
Author

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
Copy link
Author

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
Copy link

akemlek commented May 6, 2024

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

@marianobntz
Copy link
Author

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
Copy link
Contributor

tatomyr commented May 9, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested Type: Docs Type: Enhancement
Projects
None yet
Development

No branches or pull requests

4 participants