Skip to content

Commit

Permalink
feat(rulesets): add rules to validate channels.servers and server.sec…
Browse files Browse the repository at this point in the history
…urity
  • Loading branch information
magicmatatjahu committed Apr 12, 2022
1 parent 4ececf0 commit b2fa1a4
Show file tree
Hide file tree
Showing 6 changed files with 544 additions and 0 deletions.
48 changes: 48 additions & 0 deletions docs/reference/asyncapi-rules.md
Expand Up @@ -24,6 +24,48 @@ Keep trailing slashes off of channel names, as it can cause some confusion. Most

**Recommended:** Yes

### asyncapi-channel-servers

Channel servers must be defined in the `servers` object.

**Bad Example**

```yaml
asyncapi: "2.0.0"
info:
title: Awesome API
description: A very well defined API
version: "1.0"
servers:
production:
url: "stoplight.io"
protocol: "https"
channels:
hello:
servers:
- development
```

**Good Example**

```yaml
asyncapi: "2.0.0"
info:
title: Awesome API
description: A very well defined API
version: "1.0"
servers:
production:
url: "stoplight.io"
protocol: "https"
channels:
hello:
servers:
- production
```

**Recommended:** Yes

### asyncapi-headers-schema-type-object

The schema definition of the application headers must be of type “object”.
Expand Down Expand Up @@ -288,6 +330,12 @@ Server URL should not point at example.com.

**Recommended:** No

### asyncapi-server-security

Server `security` values must match a scheme defined in the `components.securitySchemes` object. It also checks if there are `oauth2` scopes that have been defined for the given security.

**Recommended:** Yes

### asyncapi-servers

A non empty `servers` object is expected to be located at the root of the document.
Expand Down
@@ -0,0 +1,118 @@
import { DiagnosticSeverity } from '@stoplight/types';
import testRule from './__helpers__/tester';

testRule('asyncapi-channel-servers', [
{
name: 'valid case',
document: {
asyncapi: '2.2.0',
servers: {
development: {},
production: {},
},
channels: {
channel: {
servers: ['development'],
},
},
},
errors: [],
},

{
name: 'valid case - without defined servers',
document: {
asyncapi: '2.2.0',
servers: {
development: {},
production: {},
},
channels: {
channel: {},
},
},
errors: [],
},

{
name: 'valid case - with empty array',
document: {
asyncapi: '2.2.0',
servers: {
development: {},
production: {},
},
channels: {
channel: {
servers: [],
},
},
},
errors: [],
},

{
name: 'invalid case',
document: {
asyncapi: '2.2.0',
servers: {
development: {},
production: {},
},
channels: {
channel: {
servers: ['another-server'],
},
},
},
errors: [
{
message: 'Channel contains server that are not defined on the "servers" object.',
path: ['channels', 'channel', 'servers', '0'],
severity: DiagnosticSeverity.Error,
},
],
},

{
name: 'invalid case - one server is defined, another one not',
document: {
asyncapi: '2.2.0',
servers: {
development: {},
production: {},
},
channels: {
channel: {
servers: ['production', 'another-server'],
},
},
},
errors: [
{
message: 'Channel contains server that are not defined on the "servers" object.',
path: ['channels', 'channel', 'servers', '1'],
severity: DiagnosticSeverity.Error,
},
],
},

{
name: 'invalid case - without defined servers',
document: {
asyncapi: '2.2.0',
channels: {
channel: {
servers: ['production'],
},
},
},
errors: [
{
message: 'Channel contains server that are not defined on the "servers" object.',
path: ['channels', 'channel', 'servers', '0'],
severity: DiagnosticSeverity.Error,
},
],
},
]);

0 comments on commit b2fa1a4

Please sign in to comment.