AsyncAPI linter: every finding apigit lint reports, with its fix
Last checked and updated
apigit lint events.yaml checks an AsyncAPI 2.x or 3.x file on your machine, without an account. It runs Spectral's AsyncAPI rules (in the CLI's default set, oas-best-practice, which applies the rules for the file's format) plus a check of every $ref. Errors are a document that does not match the specification; warnings are mostly documentation advice (some, such as asyncapi-3-channel-no-query-nor-fragment, enforce a SHALL NOT of the specification); the exit code is 1 only when there is an error.
$ npm install -g @apigit/cli
$ apigit lint events.yaml
The findings on a minimal document
A valid 3.0 document with only a title, a version, one channel and one operation (it is on What is AsyncAPI) gets the documentation rules:
$ apigit lint asyncapi.yaml
asyncapi.yaml
1:1 warn asyncapi-servers AsyncAPI object must have non-empty "servers" object.
1:11 info asyncapi-latest-version The latest version is not used. You should update to the "3.1.0" version. (asyncapi)
2:6 warn asyncapi-3-tags AsyncAPI document must have non-empty "tags" array. (info)
2:6 warn asyncapi-info-contact Info object must have "contact" object. (info)
2:6 warn asyncapi-info-description Info "description" must be present and non-empty string. (info)
2:6 warn asyncapi-info-license Info object must have "license" object. (info)
18:23 warn asyncapi-3-operation-description Operation "description" must be present and non-empty string. (operations.publishOrderCreated)
7 problems (0 errors, 6 warnings, 1 info, 0 hints)
With a contact that has only name and email, one more appears:
...
6:11 warn asyncapi-info-contact-properties Contact object must have "name", "url" and "email". (info.contact)
...
The findings on a broken document
asyncapi: 3.0.0
info:
title: Orders events
version: 1.0.0
description: Events the order service sends when an order changes.
contact:
name: Orders team
url: https://example.com/orders-team
email: orders@example.com
license:
name: Apache 2.0
tags:
- name: orders
- name: orders
servers:
production:
host: broker.example.com/
protocol: amqp
channels:
orderCreated:
address: orders.created
messages:
orderCreated:
payload:
$ref: '#/components/schemas/Order'
operations:
publishOrderCreated:
action: send
description: The order service sends this event after an order is saved.
channel:
$ref: '#/channels/orderCreated'
security:
- $ref: '#/components/securitySchemes/apiKey'
components:
schemas:
Order:
type: object
properties:
orderId:
type: string
Refund:
type: object
$ apigit lint events.yaml
events.yaml
1:11 info asyncapi-latest-version The latest version is not used. You should update to the "3.1.0" version. (asyncapi)
12:8 error asyncapi-3-document-resolved "tags" property must not have duplicate items (items ## 0 and 1 are identical) (info.tags)
14:13 error asyncapi-3-tags-uniqueness "tags" object contains duplicate tag name "orders". (info.tags.1.name)
17:11 warn asyncapi-3-server-no-trailing-slash Server host must not end with slash. (servers.production.host)
33:15 error invalid-ref '#/components/securitySchemes/apiKey' does not exist (operations.publishOrderCreated.security.0.$ref)
41:12 warn asyncapi-unused-components-schema Potentially unused components schema has been detected. (components.schemas.Refund)
6 problems (3 errors, 2 warnings, 1 info, 0 hints)
error: lint failed: findings at severity error or above
A license without url is not reported: asyncapi-info-license-url exists, but Spectral lists it as not recommended, so it is off by default.
Every rule seen, with its fix
Severity is what apigit lint printed; the meaning is quoted from Spectral's rule documentation or the specification.
| Rule | Severity | What it asks for | Fix |
|---|---|---|---|
asyncapi-3-document-resolved | error | "Validate structure of AsyncAPI specification when references have been resolved": the document against the 3.x schema | Read the message: a missing required property (action, host), a property not allowed here (subscribe, url, messageId), a wrong type |
invalid-ref | error | every $ref points to something | correct the path; see finding a broken reference |
asyncapi-3-tags-uniqueness | error | "Tags must not have duplicate names" | remove or rename the duplicate |
asyncapi-channel-parameters | error | parameters in the address are described, and no others | names under parameters equal to the {...} in address |
asyncapi-operation-operationId (2.x) | error | an operationId on every publish and subscribe | add one |
asyncapi-servers | warn | "A non-empty servers object is expected to be located at the root of the document." | add servers with host and protocol (3.x) or url and protocol (2.x) |
asyncapi-info-contact | warn | "Info object should contain contact object." | add info.contact |
asyncapi-info-contact-properties | warn | a contact "full of the most useful properties: name, url, and email" | add the missing ones |
asyncapi-info-description | warn | "AsyncAPI object info description must be present and non-empty string." | add info.description (Markdown allowed) |
asyncapi-info-license | warn | "The info object should have a license key." | add info.license.name |
asyncapi-3-tags | warn | "AsyncAPI object should have non-empty tags array." | add info.tags (in 3.x the tags are inside info) |
asyncapi-3-operation-description, asyncapi-operation-description (2.x) | warn | "Operation objects should have a description." | add description to the operation |
asyncapi-3-server-no-trailing-slash | warn | "Server host should not have a trailing slash." | remove the slash |
asyncapi-3-channel-no-trailing-slash, asyncapi-3-channel-no-query-nor-fragment, asyncapi-3-channel-no-empty-parameter | warn | a clean channel address | see channels, operations and messages |
asyncapi-unused-components-schema | warn | "Potential unused reusable schema entry has been detected." Spectral warns that it "may identify false positives" for a file used as a library | use it, delete it, or turn the rule off for library files |
asyncapi-latest-version | info | "Checking if the AsyncAPI document is using the latest version." | change asyncapi to 3.1.0; the 3.1.0 release notes call it "a minor release" without breaking changes. A 2.x file needs the migration first |
A version string the rules do not know
The version string decides which rules run. When it does not match a known AsyncAPI version, none of them can run, and apigit lint (since CLI 0.9.1) reports that as an error:
$ apigit lint short-version.yaml # asyncapi: '3.0'
short-version.yaml
1:1 error unrecognized-format "asyncapi": "3.0" is not a version the AsyncAPI rules know, so none of them ran. Write major.minor.patch, such as "3.0.0" or "2.6.0"
1 problem (1 error, 0 warnings, 0 info, 0 hints)
error: lint failed: findings at severity error or above
The exit code was 1. The file was the clean document of the previous pages with asyncapi: '3.0'; the specification says the version string "must be major.minor.patch". Older CLI versions printed Spectral's own unrecognized-format warning here and passed with exit code 0; if you pin one of those in CI, add --fail-severity warn or upgrade.
A version that matches the pattern but does not exist yet, such as 3.2.0, is linted with the 3.x rules and gets only the asyncapi-latest-version note.
Failing a build on warnings, and annotations
With --fail-severity warn, the minimal document above fails (exit code 1):
$ apigit lint --fail-severity warn asyncapi.yaml
...
7 problems (0 errors, 6 warnings, 1 info, 0 hints)
error: lint failed: findings at severity warn or above
--format github-actions prints one workflow command per finding:
$ apigit lint --format github-actions events.yaml
::notice file=events.yaml,line=1,col=11,title=asyncapi-latest-version::The latest version is not used. You should update to the "3.1.0" version.
::error file=events.yaml,line=12,col=8,title=asyncapi-3-document-resolved::"tags" property must not have duplicate items (items ## 0 and 1 are identical)
::error file=events.yaml,line=14,col=13,title=asyncapi-3-tags-uniqueness::"tags" object contains duplicate tag name "orders".
::warning file=events.yaml,line=17,col=11,title=asyncapi-3-server-no-trailing-slash::Server host must not end with slash.
::error file=events.yaml,line=33,col=15,title=invalid-ref::'#/components/securitySchemes/apiKey' does not exist
::warning file=events.yaml,line=41,col=12,title=asyncapi-unused-components-schema::Potentially unused components schema has been detected.
error: lint failed: findings at severity error or above
GitHub Actions turns each ::error, ::warning and ::notice line into an annotation on that line of the file. A complete workflow, run on GitHub with an OpenAPI file, is in Lint an OpenAPI file in GitHub Actions; the lint step takes an AsyncAPI file the same way.
Your own rule set
A .spectral.yaml in the working directory replaces the built-in set. Spectral's documentation: "In your ruleset file you can add extends: "spectral:asyncapi" and you'll get all of the following rules applied." This one turns the version note off and switches on a rule that is off by default:
extends: ["spectral:asyncapi"]
rules:
asyncapi-latest-version: off
asyncapi-3-server-not-example-com: warn
$ apigit lint asyncapi.yaml # the document of the channels page, with host: example.com
asyncapi.yaml
17:11 warn asyncapi-3-server-not-example-com Server host must not point at example.com. (servers.production.host)
1 problem (0 errors, 1 warning, 0 info, 0 hints)
The CLI's other built-in sets (owasp-top10, url-guideline, document) are for OpenAPI and refuse an AsyncAPI file:
$ apigit lint --builtin owasp-top10 asyncapi.yaml
asyncapi.yaml
1:1 error not-openapi the rule set "owasp-top10" is for OpenAPI documents; this is an AsyncAPI document
1:1 warn unrecognized-format The provided document does not match any of the registered formats [OpenAPI 2.0 (Swagger), OpenAPI 3.x, OpenAPI 3.0.x, OpenAPI 3.1.x]
2 problems (1 error, 1 warning, 0 info, 0 hints)
error: lint failed: findings at severity error or above
What the other commands do with AsyncAPI
apigit score refuses an AsyncAPI file (error: asyncapi-3.yaml is not an OpenAPI document: no "openapi" or "swagger" field, exit code 2). apigit diff and apigit breaking "Compare an OpenAPI document" (their help text) and refuse AsyncAPI too:
$ apigit breaking asyncapi-3.yaml renamed.yaml
error: asyncapi-3.yaml is an AsyncAPI document: apigit diff and breaking compare OpenAPI documents only
The exit code is 2. Before CLI 0.9.1 they printed No breaking change. with exit code 0 for two AsyncAPI files whose channel address differed. For an AsyncAPI change, review the diff of the file.