Skip to content

Commit

Permalink
update tests and rules
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaslagoni committed Sep 23, 2024
1 parent 15e3b34 commit 9590abc
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 53 deletions.
1 change: 1 addition & 0 deletions .spectral.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
extends: ["spectral:asyncapi"]
2 changes: 1 addition & 1 deletion docs/getting-started/1-concepts.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ To achieve this, Spectral has three key concepts:
- **Functions** accept a value and return issues if the value is incorrect.
- **Rulesets** act as a container for rules and functions.

Spectral comes bundled with a [set of core functions](../reference/functions.md) and rulesets for working with [OpenAPI v2 and v3](./4-openapi.md) and [AsyncAPI v2](./5-asyncapi.md) that you can chose to use or extend, but Spectral is about far more than just checking your OpenAPI/AsyncAPI documents are valid.
Spectral comes bundled with a [set of core functions](../reference/functions.md) and rulesets for working with [OpenAPI v2 and v3](./4-openapi.md) and [AsyncAPI v2 and v3](./5-asyncapi.md) that you can chose to use or extend, but Spectral is about far more than just checking your OpenAPI/AsyncAPI documents are valid.

By far the most popular use-case of Spectral is automating [API Style Guides](https://stoplight.io/api-style-guides-guidelines-and-best-practices?utm_source=github&utm_medium=spectral&utm_campaign=docs), implementing rules that your Architecture, DevOps, API Governance or "Center of Excellence" teams have decided upon. Companies generally write these style guides as wiki pages, and several can be found on [API Stylebook](http://apistylebook.com/), but most of these rules could be automated with Spectral. For example:

Expand Down
4 changes: 2 additions & 2 deletions docs/getting-started/3-rulesets.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ The fastest way to create a ruleset is to use the `extends` property to leverage
Spectral comes with two built-in rulesets:

- `spectral:oas` - [OpenAPI v2/v3 rules](./4-openapi.md)
- `spectral:asyncapi` - [AsyncAPI v2 rules](./5-asyncapi.md)
- `spectral:asyncapi` - [AsyncAPI v2/v3 rules](./5-asyncapi.md)

To create a ruleset that extends both rulesets, open your terminal and run:

```bash
echo 'extends: ["spectral:oas", "spectral:asyncapi"]' > .spectral.yaml
```

The newly created ruleset file can then be used to lint any OpenAPI v2/v3 or AsyncAPI descriptions using the `spectral lint` command:
The newly created ruleset file can then be used to lint any OpenAPI v2/v3 or AsyncAPI v2/v3 descriptions using the `spectral lint` command:

```bash
spectral lint myapifile.yaml
Expand Down
7 changes: 1 addition & 6 deletions docs/reference/asyncapi-rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ specifications that reference those objects).
**Recommended:** Yes

## AsyncAPI v2

The following rules ONLY apply to AsyncAPI v2 documents.

### asyncapi-server-security
Expand Down Expand Up @@ -721,12 +722,6 @@ Operation objects should have a description.

**Recommended:** Yes

### asyncapi-3-operation-operationId

This operation ID is essentially a reference for the operation. Tools may use it for defining function names, class method names, and even URL hashes in documentation systems.

**Recommended:** Yes

### asyncapi-3-payload-unsupported-schemaFormat

AsyncAPI can support various `schemaFormat` values. When unspecified, one of the following will be assumed:
Expand Down

This file was deleted.

24 changes: 13 additions & 11 deletions packages/rulesets/src/asyncapi/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ export default {
message: '{{error}}',
severity: 'error',
recommended: true,
formats: [aas2, aas3],
given: ['$.channels.*', '$.components.channels.*'],
then: {
function: asyncApiChannelParameters,
Expand All @@ -141,6 +142,7 @@ export default {
recommended: true,
resolved: false, // We use the JSON pointer to match the channel.
given: '$.channels.*',
formats: [aas3],
then: {
field: '$.servers.*.$ref',
function: pattern,
Expand Down Expand Up @@ -209,6 +211,7 @@ export default {
description: 'Contact object must have "name", "url" and "email".',
recommended: true,
given: '$.info.contact',
formats: [aas2, aas3],
then: [
{
field: 'name',
Expand All @@ -227,6 +230,7 @@ export default {
'asyncapi-info-contact': {
description: 'Info object must have "contact" object.',
recommended: true,
formats: [aas2, aas3],
given: '$',
then: {
field: 'info.contact',
Expand All @@ -237,6 +241,7 @@ export default {
description: 'Info "description" must be present and non-empty string.',
recommended: true,
given: '$',
formats: [aas2, aas3],
then: {
field: 'info.description',
function: truthy,
Expand All @@ -246,6 +251,7 @@ export default {
description: 'License object must include "url".',
recommended: false,
given: '$',
formats: [aas2, aas3],
then: {
field: 'info.license.url',
function: truthy,
Expand All @@ -255,6 +261,7 @@ export default {
description: 'Info object must have "license" object.',
recommended: true,
given: '$',
formats: [aas2, aas3],
then: {
field: 'info.license',
function: truthy,
Expand All @@ -266,6 +273,7 @@ export default {
recommended: true,
severity: 'info',
given: '$.asyncapi',
formats: [aas2, aas3],
then: {
function: schema,
functionOptions: {
Expand Down Expand Up @@ -351,17 +359,6 @@ export default {
function: truthy,
},
},
'asyncapi-3-operation-operationId': {
description: 'Operation must have "operationId".',
severity: 'error',
recommended: true,
formats: [aas3],
given: '$.operations.*',
then: {
field: 'operationId',
function: truthy,
},
},
'asyncapi-operation-security': {
description: 'Operation have to reference a defined security schemes.',
message: '{{error}}',
Expand Down Expand Up @@ -393,6 +390,7 @@ export default {
'asyncapi-parameter-description': {
description: 'Parameter objects must have "description".',
recommended: false,
formats: [aas2, aas3],
given: ['$.components.parameters.*', '$.channels.*.parameters.*'],
then: {
field: 'description',
Expand Down Expand Up @@ -513,6 +511,7 @@ export default {
message: '{{error}}',
severity: 'error',
recommended: true,
formats: [aas2, aas3],
given: '$',
then: {
function: asyncApiDocumentSchema,
Expand Down Expand Up @@ -618,6 +617,7 @@ export default {
'asyncapi-servers': {
description: 'AsyncAPI object must have non-empty "servers" object.',
recommended: true,
formats: [aas2, aas3],
given: '$',
then: {
field: 'servers',
Expand Down Expand Up @@ -771,6 +771,7 @@ export default {
description: 'Potentially unused components schema has been detected.',
recommended: true,
resolved: false,
formats: [aas2, aas3],
given: '$.components.schemas',
then: {
function: unreferencedReusableObject,
Expand All @@ -783,6 +784,7 @@ export default {
description: 'Potentially unused components server has been detected.',
recommended: true,
resolved: false,
formats: [aas2, aas3],
given: '$.components.servers',
then: {
function: unreferencedReusableObject,
Expand Down
44 changes: 44 additions & 0 deletions test-harness/scenarios/asyncapi3-streetlights.scenario
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
====test====
Validate simple AsyncAPI 3.0 sample
====document====
asyncapi: 3.0.0
info:
title: Account Service
version: 1.0.0
description: This service is in charge of processing user signups
channels:
userSignedup:
address: user/signedup
messages:
UserSignedUp:
payload:
type: object
properties:
displayName:
type: string
description: Name of the user
email:
type: string
format: email
description: Email of the user
operations:
sendUserSignedup:
action: send
channel:
$ref: '#/channels/userSignedup'
====asset:ruleset====
const { asyncapi } = require('@stoplight/spectral-rulesets');
module.exports = asyncapi;
====command====
{bin} lint {document} --ruleset "{asset:ruleset}"
====stdout====
{document}
1:1 warning asyncapi-tags AsyncAPI object must have non-empty "tags" array.
1:11 information asyncapi-latest-version The latest version is not used. You should update to the "2.6.0" version. asyncapi
2:6 warning asyncapi-info-contact Info object must have "contact" object. info
45:13 warning asyncapi-operation-description Operation "description" must be present and non-empty string. channels.smartylighting/streetlights/1/0/event/{streetlightId}/lighting/measured.publish
57:15 warning asyncapi-operation-description Operation "description" must be present and non-empty string. channels.smartylighting/streetlights/1/0/action/{streetlightId}/turn/on.subscribe
68:15 warning asyncapi-operation-description Operation "description" must be present and non-empty string. channels.smartylighting/streetlights/1/0/action/{streetlightId}/turn/off.subscribe
79:15 warning asyncapi-operation-description Operation "description" must be present and non-empty string. channels.smartylighting/streetlights/1/0/action/{streetlightId}/dim.subscribe

✖ 7 problems (0 errors, 6 warnings, 1 info, 0 hints)

0 comments on commit 9590abc

Please sign in to comment.