Skip to content

Commit

Permalink
fix(rulesets): fix latst version of AsyncAPI
Browse files Browse the repository at this point in the history
  • Loading branch information
magicmatatjahu committed Oct 5, 2022
1 parent 57cd059 commit dd647d6
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 19 deletions.
7 changes: 7 additions & 0 deletions packages/formats/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
# [@stoplight/spectral-formats-v1.3.0](https://github.com/stoplightio/spectral/compare/@stoplight/spectral-formats-v1.2.0...@stoplight/spectral-formats-v1.3.0) (2022-10-03)


### Features

* **formats:** add support for 2.5.0 AsyncAPI ([#2292](https://github.com/stoplightio/spectral/issues/2292)) ([a7f9fa7](https://github.com/stoplightio/spectral/commit/a7f9fa72b80b0327fb1fca6e4ee84f9878618f4e))

# [@stoplight/spectral-formats-v1.3.0](https://github.com/stoplightio/spectral/compare/@stoplight/spectral-formats-v1.2.0...@stoplight/spectral-formats-v1.3.0) (2022-10-03)


### Features

* **rulesets:** add support for 2.5.0 AsyncAPI ([#2292](https://github.com/stoplightio/spectral/issues/2292)) ([9050785](https://github.com/stoplightio/spectral/commit/90507856be44ae3538c214b12ca9ed242e4db64b))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { DiagnosticSeverity } from '@stoplight/types';
import { latestAsyncApiVersion } from '../functions/asyncApi2DocumentSchema';
import { latestVersion } from '../functions/utils/specs';
import testRule from './__helpers__/tester';

testRule('asyncapi-latest-version', [
{
name: 'valid case',
document: {
asyncapi: latestAsyncApiVersion,
asyncapi: latestVersion,
},
errors: [],
},
Expand All @@ -18,7 +18,7 @@ testRule('asyncapi-latest-version', [
},
errors: [
{
message: `The latest version is not used. You should update to the "${latestAsyncApiVersion}" version.`,
message: `The latest version is not used. You should update to the "${latestVersion}" version.`,
path: ['asyncapi'],
severity: DiagnosticSeverity.Information,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ import type { ErrorObject } from 'ajv';
import type { IFunctionResult, Format } from '@stoplight/spectral-core';
import type { AsyncAPISpecVersion } from './utils/specs';

export const asyncApiSpecVersions = ['2.0.0', '2.1.0', '2.2.0', '2.3.0', '2.4.0'];
export const latestAsyncApiVersion = asyncApiSpecVersions[asyncApiSpecVersions.length - 1];

function shouldIgnoreError(error: ErrorObject): boolean {
return (
// oneOf is a fairly error as we have 2 options to choose from for most of the time.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ export interface MessageFragment {
examples?: MessageExample[];
}

function getMessageExamples(message: MessageFragment): Array<{ path: JsonPath; value: MessageExample }> {
function getMessageExamples(message: MessageFragment): Array<{ path: JsonPath; example: MessageExample }> {
if (!Array.isArray(message.examples)) {
return [];
}
return (
message.examples.map((example, index) => {
return {
path: ['examples', index],
value: example,
example,
};
}) ?? []
);
Expand Down Expand Up @@ -78,18 +78,18 @@ export default createRulesetFunction<MessageFragment, null>(

for (const example of examples) {
// validate payload
if (example.value.payload !== undefined) {
if (example.example.payload !== undefined) {
const payload = targetVal.payload ?? {}; // if payload is undefined we treat it as any schema
const errors = validate(example.value.payload, example.path, 'payload', payload, ctx);
const errors = validate(example.example.payload, example.path, 'payload', payload, ctx);
if (Array.isArray(errors)) {
results.push(...errors);
}
}

// validate headers
if (example.value.headers !== undefined) {
if (example.example.headers !== undefined) {
const headers = targetVal.headers ?? {}; // if headers are undefined we treat them as any schema
const errors = validate(example.value.headers, example.path, 'headers', headers, ctx);
const errors = validate(example.example.headers, example.path, 'headers', headers, ctx);
if (Array.isArray(errors)) {
results.push(...errors);
}
Expand Down
3 changes: 3 additions & 0 deletions packages/rulesets/src/asyncapi/functions/utils/specs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ export const specs = {
'2.5.0': asyncAPI2_5_0Schema,
};

const versions = Object.keys(specs);
export const latestVersion = versions[versions.length - 1];

export function getCopyOfSchema(version: AsyncAPISpecVersion): Record<string, unknown> {
return JSON.parse(JSON.stringify(specs[version])) as Record<string, unknown>;
}
7 changes: 4 additions & 3 deletions packages/rulesets/src/asyncapi/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {

import asyncApi2ChannelParameters from './functions/asyncApi2ChannelParameters';
import asyncApi2ChannelServers from './functions/asyncApi2ChannelServers';
import asyncApi2DocumentSchema, { latestAsyncApiVersion } from './functions/asyncApi2DocumentSchema';
import asyncApi2DocumentSchema from './functions/asyncApi2DocumentSchema';
import asyncApi2MessageExamplesValidation from './functions/asyncApi2MessageExamplesValidation';
import asyncApi2MessageIdUniqueness from './functions/asyncApi2MessageIdUniqueness';
import asyncApi2OperationIdUniqueness from './functions/asyncApi2OperationIdUniqueness';
Expand All @@ -19,6 +19,7 @@ import asyncApi2PayloadValidation from './functions/asyncApi2PayloadValidation';
import asyncApi2ServerVariables from './functions/asyncApi2ServerVariables';
import { uniquenessTags } from '../shared/functions';
import asyncApi2Security from './functions/asyncApi2Security';
import { latestVersion } from './functions/utils/specs';

export default {
documentationUrl: 'https://meta.stoplight.io/docs/spectral/docs/reference/asyncapi-rules.md',
Expand Down Expand Up @@ -174,7 +175,7 @@ export default {
},
'asyncapi-latest-version': {
description: 'Checking if the AsyncAPI document is using the latest version.',
message: `The latest version is not used. You should update to the "${latestAsyncApiVersion}" version.`,
message: `The latest version is not used. You should update to the "${latestVersion}" version.`,
recommended: true,
type: 'style',
severity: 'info',
Expand All @@ -183,7 +184,7 @@ export default {
function: schema,
functionOptions: {
schema: {
const: latestAsyncApiVersion,
const: latestVersion,
},
},
},
Expand Down
40 changes: 36 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2444,6 +2444,25 @@ __metadata:
languageName: node
linkType: hard

"@stoplight/json-ref-resolver@npm:3.1.3":
version: 3.1.3
resolution: "@stoplight/json-ref-resolver@npm:3.1.3"
dependencies:
"@stoplight/json": ^3.17.0
"@stoplight/path": ^1.3.2
"@stoplight/types": ^12.3.0
"@types/urijs": ^1.19.16
dependency-graph: ~0.11.0
fast-memoize: ^2.5.2
immer: ^9.0.6
lodash.get: ^4.4.2
lodash.set: ^4.3.2
tslib: ^2.3.1
urijs: ^1.19.6
checksum: 3aba23c9a5ee270dbeb6e3b9dd38b8c5579dce88194145e590d54b5385ea2a0a0c411a39a98506c9b7d767f12952480e836d3d3c2559721388c4abfb070e69d7
languageName: node
linkType: hard

"@stoplight/json-ref-resolver@npm:~3.1.4":
version: 3.1.4
resolution: "@stoplight/json-ref-resolver@npm:3.1.4"
Expand Down Expand Up @@ -2622,7 +2641,7 @@ __metadata:
languageName: unknown
linkType: soft

"@stoplight/spectral-ref-resolver@*, @stoplight/spectral-ref-resolver@1.0.1, @stoplight/spectral-ref-resolver@>=1, @stoplight/spectral-ref-resolver@^1.0.0, @stoplight/spectral-ref-resolver@workspace:packages/ref-resolver":
"@stoplight/spectral-ref-resolver@*, @stoplight/spectral-ref-resolver@>=1, @stoplight/spectral-ref-resolver@^1.0.0, @stoplight/spectral-ref-resolver@workspace:packages/ref-resolver":
version: 0.0.0-use.local
resolution: "@stoplight/spectral-ref-resolver@workspace:packages/ref-resolver"
dependencies:
Expand All @@ -2634,6 +2653,19 @@ __metadata:
languageName: unknown
linkType: soft

"@stoplight/spectral-ref-resolver@npm:1.0.1":
version: 1.0.1
resolution: "@stoplight/spectral-ref-resolver@npm:1.0.1"
dependencies:
"@stoplight/json-ref-readers": 1.2.2
"@stoplight/json-ref-resolver": 3.1.3
"@stoplight/spectral-runtime": ^1.0.0
dependency-graph: 0.11.0
tslib: ^2.3.1
checksum: 514260aca087cdff24cd4181234f72e24b2d713fd28b721bea0a72162f0e923c88c4730afeca82b44c13d22f9d2c11fa7bcd0c81df87f7d4cb077b9407ca3994
languageName: node
linkType: hard

"@stoplight/spectral-ruleset-bundler@^1.0.0, @stoplight/spectral-ruleset-bundler@workspace:packages/ruleset-bundler":
version: 0.0.0-use.local
resolution: "@stoplight/spectral-ruleset-bundler@workspace:packages/ruleset-bundler"
Expand Down Expand Up @@ -3257,7 +3289,7 @@ __metadata:
languageName: node
linkType: hard

"@types/urijs@npm:^1.19.19":
"@types/urijs@npm:^1.19.16, @types/urijs@npm:^1.19.19":
version: 1.19.19
resolution: "@types/urijs@npm:1.19.19"
checksum: 2c08d41782149a243b374b28be009ca461f541c440d8d47c9d75b1d3255ff7169b34bb721cf2dd6266c2c44be6b70fc6d67a1abad50c4dae369774042b1facd8
Expand Down Expand Up @@ -8897,7 +8929,7 @@ __metadata:
languageName: node
linkType: hard

"lodash.get@npm:^4":
"lodash.get@npm:^4, lodash.get@npm:^4.4.2":
version: 4.4.2
resolution: "lodash.get@npm:4.4.2"
checksum: e403047ddb03181c9d0e92df9556570e2b67e0f0a930fcbbbd779370972368f5568e914f913e93f3b08f6d492abc71e14d4e9b7a18916c31fa04bd2306efe545
Expand Down Expand Up @@ -13142,7 +13174,7 @@ __metadata:
languageName: node
linkType: hard

"urijs@npm:^1.19.11":
"urijs@npm:^1.19.11, urijs@npm:^1.19.6":
version: 1.19.11
resolution: "urijs@npm:1.19.11"
checksum: f9b95004560754d30fd7dbee44b47414d662dc9863f1cf5632a7c7983648df11d23c0be73b9b4f9554463b61d5b0a520b70df9e1ee963ebb4af02e6da2cc80f3
Expand Down

0 comments on commit dd647d6

Please sign in to comment.