Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into validate-tags-uniq…
Browse files Browse the repository at this point in the history
…ueness
  • Loading branch information
P0lip committed May 30, 2022
2 parents 891d874 + 9acc633 commit cf0a222
Show file tree
Hide file tree
Showing 13 changed files with 506 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -79,7 +79,7 @@ If you have a bug or feature request, please [create an issue](https://github.co

### How is this different to Ajv

[Ajv](https://www.npmjs.com/package/ajv) is a JSON Schema validator, and Spectral is a JSON/YAML linter. Instead of just validating against JSON Schema, it can be used to write rules for any sort of JSON/YAML object, which could be JSON Schema, or OpenAPI, or anything similar. Spectral does expose a [`schema` function](https://meta.stoplight.io/docs/spectral/docs/reference/functions.md) that you can use in your rules to validate all or part of the target object with JSON Schema (we even use Ajv used under the hood for this), but that's just one of many functions.
[Ajv](https://www.npmjs.com/package/ajv) is a JSON Schema validator, and Spectral is a JSON/YAML linter. Instead of just validating against JSON Schema, it can be used to write rules for any sort of JSON/YAML object, which could be JSON Schema, or OpenAPI, or anything similar. Spectral does expose a [`schema` function](https://meta.stoplight.io/docs/spectral/docs/reference/functions.md) that you can use in your rules to validate all or part of the target object with JSON Schema (we even use Ajv under the hood for this), but that's just one of many functions.

### I want to lint my OpenAPI documents but don't want to implement Spectral right now.

Expand Down
12 changes: 12 additions & 0 deletions docs/reference/asyncapi-rules.md
Expand Up @@ -24,6 +24,12 @@ Keep trailing slashes off of channel names, as it can cause some confusion. Most

**Recommended:** Yes

### asyncapi-channel-parameters

All channel parameters should be defined in the `parameters` object of the channel. They should also not contain redundant parameters that do not exist in the channel address.

**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 +294,12 @@ Server URL should not point at example.com.

**Recommended:** No

### asyncapi-server-variables

All server URL variables should be defined in the `variables` object of the server. They should also not contain redundant variables that do not exist in the server address.

**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,143 @@
import { DiagnosticSeverity } from '@stoplight/types';
import testRule from './__helpers__/tester';

testRule('asyncapi-channel-parameters', [
{
name: 'valid case',
document: {
asyncapi: '2.0.0',
channels: {
'users/{userId}/signedUp': {
parameters: {
userId: {},
},
},
},
},
errors: [],
},

{
name: 'channel has not defined definition for one of the parameters',
document: {
asyncapi: '2.0.0',
channels: {
'users/{userId}/{anotherParam}/signedUp': {
parameters: {
userId: {},
},
},
},
},
errors: [
{
message: 'Not all channel\'s parameters are described with "parameters" object. Missed: anotherParam.',
path: ['channels', 'users/{userId}/{anotherParam}/signedUp', 'parameters'],
severity: DiagnosticSeverity.Error,
},
],
},

{
name: 'channel has not defined definition for two+ of the parameters',
document: {
asyncapi: '2.0.0',
channels: {
'users/{userId}/{anotherParam1}/{anotherParam2}/signedUp': {
parameters: {
userId: {},
},
},
},
},
errors: [
{
message:
'Not all channel\'s parameters are described with "parameters" object. Missed: anotherParam1, anotherParam2.',
path: ['channels', 'users/{userId}/{anotherParam1}/{anotherParam2}/signedUp', 'parameters'],
severity: DiagnosticSeverity.Error,
},
],
},

{
name: 'channel has not defined definition for one of the parameters (in the components.channels)',
document: {
asyncapi: '2.3.0',
components: {
channels: {
'users/{userId}/{anotherParam}/signedUp': {
parameters: {
userId: {},
},
},
},
},
},
errors: [
{
message: 'Not all channel\'s parameters are described with "parameters" object. Missed: anotherParam.',
path: ['components', 'channels', 'users/{userId}/{anotherParam}/signedUp', 'parameters'],
severity: DiagnosticSeverity.Error,
},
],
},

{
name: 'channel has redundant paramaters',
document: {
asyncapi: '2.0.0',
channels: {
'users/{userId}/signedUp': {
parameters: {
userId: {},
anotherParam1: {},
anotherParam2: {},
},
},
},
},
errors: [
{
message: 'Channel\'s "parameters" object has redundant defined "anotherParam1" parameter.',
path: ['channels', 'users/{userId}/signedUp', 'parameters', 'anotherParam1'],
severity: DiagnosticSeverity.Error,
},
{
message: 'Channel\'s "parameters" object has redundant defined "anotherParam2" parameter.',
path: ['channels', 'users/{userId}/signedUp', 'parameters', 'anotherParam2'],
severity: DiagnosticSeverity.Error,
},
],
},

{
name: 'channel has redundant paramaters (in the components.channels)',
document: {
asyncapi: '2.3.0',
components: {
channels: {
'users/{userId}/signedUp': {
parameters: {
userId: {},
anotherParam1: {},
anotherParam2: {},
},
},
},
},
},
errors: [
{
message: 'Channel\'s "parameters" object has redundant defined "anotherParam1" parameter.',
path: ['components', 'channels', 'users/{userId}/signedUp', 'parameters', 'anotherParam1'],
severity: DiagnosticSeverity.Error,
},
{
message: 'Channel\'s "parameters" object has redundant defined "anotherParam2" parameter.',
path: ['components', 'channels', 'users/{userId}/signedUp', 'parameters', 'anotherParam2'],
severity: DiagnosticSeverity.Error,
},
],
},
]);
@@ -0,0 +1,155 @@
import { DiagnosticSeverity } from '@stoplight/types';
import testRule from './__helpers__/tester';

testRule('asyncapi-server-variables', [
{
name: 'valid case',
document: {
asyncapi: '2.0.0',
servers: {
production: {
url: '{sub}.stoplight.io',
protocol: 'https',
variables: {
sub: {},
},
},
},
},
errors: [],
},

{
name: 'server has not defined definition for one of the url variables',
document: {
asyncapi: '2.0.0',
servers: {
production: {
url: '{sub}.{anotherParam}.stoplight.io',
protocol: 'https',
variables: {
sub: {},
},
},
},
},
errors: [
{
message: 'Not all server\'s variables are described with "variables" object. Missed: anotherParam.',
path: ['servers', 'production', 'variables'],
severity: DiagnosticSeverity.Error,
},
],
},

{
name: 'server has not defined definition for two of the url variables',
document: {
asyncapi: '2.0.0',
servers: {
production: {
url: '{sub}.{anotherParam1}.{anotherParam2}.stoplight.io',
protocol: 'https',
variables: {
sub: {},
},
},
},
},
errors: [
{
message:
'Not all server\'s variables are described with "variables" object. Missed: anotherParam1, anotherParam2.',
path: ['servers', 'production', 'variables'],
severity: DiagnosticSeverity.Error,
},
],
},

{
name: 'server has not defined definition for one of the url variables (in the components.servers)',
document: {
asyncapi: '2.3.0',
components: {
servers: {
production: {
url: '{sub}.{anotherParam}.stoplight.io',
protocol: 'https',
variables: {
sub: {},
},
},
},
},
},
errors: [
{
message: 'Not all server\'s variables are described with "variables" object. Missed: anotherParam.',
path: ['components', 'servers', 'production', 'variables'],
severity: DiagnosticSeverity.Error,
},
],
},

{
name: 'server has redundant url variables',
document: {
asyncapi: '2.0.0',
servers: {
production: {
url: '{sub}.stoplight.io',
protocol: 'https',
variables: {
sub: {},
anotherParam1: {},
anotherParam2: {},
},
},
},
},
errors: [
{
message: 'Server\'s "variables" object has redundant defined "anotherParam1" url variable.',
path: ['servers', 'production', 'variables', 'anotherParam1'],
severity: DiagnosticSeverity.Error,
},
{
message: 'Server\'s "variables" object has redundant defined "anotherParam2" url variable.',
path: ['servers', 'production', 'variables', 'anotherParam2'],
severity: DiagnosticSeverity.Error,
},
],
},

{
name: 'server has redundant url variables (in the components.servers)',
document: {
asyncapi: '2.3.0',
components: {
servers: {
production: {
url: '{sub}.stoplight.io',
protocol: 'https',
variables: {
sub: {},
anotherParam1: {},
anotherParam2: {},
},
},
},
},
},
errors: [
{
message: 'Server\'s "variables" object has redundant defined "anotherParam1" url variable.',
path: ['components', 'servers', 'production', 'variables', 'anotherParam1'],
severity: DiagnosticSeverity.Error,
},
{
message: 'Server\'s "variables" object has redundant defined "anotherParam2" url variable.',
path: ['components', 'servers', 'production', 'variables', 'anotherParam2'],
severity: DiagnosticSeverity.Error,
},
],
},
]);
@@ -0,0 +1,51 @@
import { createRulesetFunction } from '@stoplight/spectral-core';

import { parseUrlVariables } from './utils/parseUrlVariables';
import { getMissingProps } from './utils/getMissingProps';
import { getRedundantProps } from './utils/getRedundantProps';

import type { IFunctionResult } from '@stoplight/spectral-core';

export default createRulesetFunction<{ parameters: Record<string, unknown> }, null>(
{
input: {
type: 'object',
properties: {
parameters: {
type: 'object',
},
},
required: ['parameters'],
},
options: null,
},
function asyncApi2ChannelParameters(targetVal, _, ctx) {
const path = ctx.path[ctx.path.length - 1] as string;
const results: IFunctionResult[] = [];

const parameters = parseUrlVariables(path);
if (parameters.length === 0) return;

const missingParameters = getMissingProps(parameters, targetVal.parameters);
if (missingParameters.length) {
results.push({
message: `Not all channel's parameters are described with "parameters" object. Missed: ${missingParameters.join(
', ',
)}.`,
path: [...ctx.path, 'parameters'],
});
}

const redundantParameters = getRedundantProps(parameters, targetVal.parameters);
if (redundantParameters.length) {
redundantParameters.forEach(param => {
results.push({
message: `Channel's "parameters" object has redundant defined "${param}" parameter.`,
path: [...ctx.path, 'parameters', param],
});
});
}

return results;
},
);

0 comments on commit cf0a222

Please sign in to comment.