Skip to content

Commit

Permalink
refactor: only run new rule if schemaFormat is set
Browse files Browse the repository at this point in the history
switch the new rule to only run of the schemaFormat is
set in the message. Also adjust the tests to better align
with this separation.

Signed-off-by: Chris Patmore <[email protected]>
  • Loading branch information
Chris Patmore committed Jun 15, 2023
1 parent 0ee59db commit 26eda0c
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 323 deletions.
43 changes: 13 additions & 30 deletions src/ruleset/v2/functions/messageExamples-spectral-rule-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,24 @@ import { createDetailedAsyncAPI } from '../../../utils';

export function asyncApi2MessageExamplesParserRule(parser: Parser): RuleDefinition {
return {
description: 'Examples of message object should validate againt the "payload" and "headers" schemas.',
description: 'Examples of message object should validate against the "payload" and "headers" schemas.',
message: '{{error}}',
severity: 'error',
recommended: true,
given: [
// messages
'$.channels.*.[publish,subscribe].message',
'$.channels.*.[publish,subscribe].message.oneOf.*',
'$.components.channels.*.[publish,subscribe].message',
'$.components.channels.*.[publish,subscribe].message.oneOf.*',
'$.components.messages.*',
'$.channels.*.[publish,subscribe][?(@property === \'message\' && @.schemaFormat !== void 0)]',
'$.channels.*.[publish,subscribe].message.oneOf[?(!@null && @.schemaFormat !== void 0)]',
'$.components.channels.*.[publish,subscribe].message[?(@property === \'message\' && @.schemaFormat !== void 0)]',
'$.components.channels.*.[publish,subscribe].message.oneOf[?(!@null && @.schemaFormat !== void 0)]',
'$.components.messages[?(!@null && @.schemaFormat !== void 0)]',
// message traits
'$.channels.*.[publish,subscribe].message.traits.*',
'$.channels.*.[publish,subscribe].message.oneOf.*.traits.*',
'$.components.channels.*.[publish,subscribe].message.traits.*',
'$.components.channels.*.[publish,subscribe].message.oneOf.*.traits.*',
'$.components.messages.*.traits.*',
'$.components.messageTraits.*',
'$.channels.*.[publish,subscribe].message.traits[?(!@null && @.schemaFormat !== void 0)]',
'$.channels.*.[publish,subscribe].message.oneOf.*.traits[?(!@null && @.schemaFormat !== void 0)]',
'$.components.channels.*.[publish,subscribe].message.traits[?(!@null && @.schemaFormat !== void 0)]',
'$.components.channels.*.[publish,subscribe].message.oneOf.*.traits[?(!@null && @.schemaFormat !== void 0)]',
'$.components.messages.*.traits[?(!@null && @.schemaFormat !== void 0)]',
'$.components.messageTraits[?(!@null && @.schemaFormat !== void 0)]',
],
then: {
function: rulesetFunction(parser),
Expand Down Expand Up @@ -121,7 +121,7 @@ async function parseExampleSchema(parser: Parser, schema: unknown, type: 'payloa
try {
const parseSchemaInput: ParseSchemaInput = {
asyncapi: input.asyncapi,
data: serializeSchema(schema, type),
data: schema,
meta: {},
path,
schemaFormat: input.schemaFormat,
Expand All @@ -138,23 +138,6 @@ async function parseExampleSchema(parser: Parser, schema: unknown, type: 'payloa
}
}

function serializeSchema(schema: unknown, type: 'payload' | 'headers'): any {
if (!schema && typeof schema !== 'boolean') { // if schema is falsy then
if (type === 'headers') { // object for headers
schema = { type: 'object' };
} else { // anything for payload
schema = {};
}
} else if (typeof schema === 'boolean') { // spectral cannot handle boolean schemas
if (schema === true) {
schema = {}; // everything
} else {
schema = { not: {} }; // nothing
}
}
return schema;
}

function getMessageExamples(message: v2.MessageObject): Array<{ path: JsonPath; value: v2.MessageExampleObject }> {
if (!Array.isArray(message.examples)) {
return [];
Expand Down
4 changes: 2 additions & 2 deletions src/ruleset/v2/ruleset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ export const v2CoreRuleset = {
recommended: true,
given: [
// messages
'$.channels.*.[publish,subscribe].[?(@property === \'message\' && @.schemaFormat === void 0)]',
'$.channels.*.[publish,subscribe][?(@property === \'message\' && @.schemaFormat === void 0)]',
'$.channels.*.[publish,subscribe].message.oneOf[?(!@null && @.schemaFormat === void 0)]',
'$.components.channels.*.[publish,subscribe].[?(@property === \'message\' && @.schemaFormat === void 0)]',
'$.components.channels.*.[publish,subscribe][?(@property === \'message\' && @.schemaFormat === void 0)]',
'$.components.channels.*.[publish,subscribe].message.oneOf[?(!@null && @.schemaFormat === void 0)]',
'$.components.messages[?(!@null && @.schemaFormat === void 0)]',
// message traits
Expand Down
Loading

0 comments on commit 26eda0c

Please sign in to comment.