Skip to content

Commit

Permalink
feat(asyncapi): make asyncapi-payload-examples support asyncapi schem…
Browse files Browse the repository at this point in the history
…aFormats
  • Loading branch information
nulltoken committed Apr 24, 2020
1 parent 5f2a244 commit 6f9dbe0
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
35 changes: 33 additions & 2 deletions src/rulesets/asyncapi/__tests__/asyncapi-payload-examples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { cloneDeep } from 'lodash';
import { buildTestSpectralWithAsyncApiRule } from '../../../../setupTests';
import { Spectral } from '../../../spectral';
import { IRunRule } from '../../../types';
import { supportedSchemaFormats } from '../functions/asyncApi2PayloadValidation';

const ruleName = 'asyncapi-payload-examples';
let s: Spectral;
Expand Down Expand Up @@ -54,8 +55,38 @@ describe(`Rule '${ruleName}'`, () => {
},
};

test('validates a correct object', async () => {
const results = await s.run(doc, { ignoreUnknownFormat: false });
test.each(supportedSchemaFormats)(
'validates a correct object (schemaFormat: "%s")',
async (schemaFormat: string | undefined) => {
const clone = cloneDeep(doc);

clone.components.messages.aMessage.schemaFormat = schemaFormat;
clone.components.messageTraits.aTrait.schemaFormat = schemaFormat;
clone.channels['users/{userId}/signedUp'].publish.message.schemaFormat = schemaFormat;
clone.channels['users/{userId}/signedUp'].subscribe.message.schemaFormat = schemaFormat;

const results = await s.run(clone, { ignoreUnknownFormat: false });

expect(results).toEqual([]);
},
);

test('silently ignore invalid examples values when the schemaFormat is unsupported', async () => {
const clone = cloneDeep(doc);

clone.components.messages.aMessage.schemaFormat = 'application/nope';
clone.components.messages.aMessage.payload.examples[1] = { seventeen: 17 };

clone.components.messageTraits.aTrait.schemaFormat = 'application/nope';
clone.components.messageTraits.aTrait.payload.examples[1] = { seventeen: 17 };

clone.channels['users/{userId}/signedUp'].publish.message.schemaFormat = 'application/nope';
clone.channels['users/{userId}/signedUp'].publish.message.payload.examples[1] = { seventeen: 17 };

clone.channels['users/{userId}/signedUp'].subscribe.message.schemaFormat = 'application/nope';
clone.channels['users/{userId}/signedUp'].subscribe.message.payload.examples[1] = { seventeen: 17 };

const results = await s.run(clone, { ignoreUnknownFormat: false });

expect(results).toEqual([]);
});
Expand Down
12 changes: 6 additions & 6 deletions src/rulesets/asyncapi/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -198,15 +198,15 @@
"recommended": true,
"type": "validation",
"given": [
"$.components.messageTraits[?(@.schemaFormat === void 0)].payload.examples^",
"$.components.messages[?(@.schemaFormat === void 0)].payload.examples^",
"$.channels.*.[publish,subscribe][?(@property === 'message' && @.schemaFormat === void 0)].payload.examples^"
"$.components.messageTraits.*.payload.examples^^",
"$.components.messages.*.payload.examples^^",
"$.channels.*.[publish,subscribe].message.payload.examples^^"
],
"then": {
"function": "schemaPath",
"function": "asyncApi2SchemaPath",
"functionOptions": {
"field": "$.examples.*",
"schemaPath": "$",
"field": "$.payload.examples.*",
"schemaPath": "$.payload",
"allErrors": true
}
}
Expand Down

0 comments on commit 6f9dbe0

Please sign in to comment.