-
-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: validate message object examples with spectral schemas (#782)
Co-authored-by: Chris Patmore <[email protected]>
- Loading branch information
1 parent
0382c65
commit 298b289
Showing
8 changed files
with
422 additions
and
8 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
src/ruleset/v2/functions/messageExamples-spectral-rule-v2.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import { RuleDefinition, createRulesetFunction } from '@stoplight/spectral-core'; | ||
|
||
import type { JsonPath } from '@stoplight/types'; | ||
import type { IFunctionResult } from '@stoplight/spectral-core'; | ||
import type { v2 } from 'spec-types'; | ||
import type { Parser } from 'parser'; | ||
import type { DetailedAsyncAPI } from 'types'; | ||
import { ParseSchemaInput, getDefaultSchemaFormat, getSchemaFormat, parseSchema } from '../../../schema-parser'; | ||
import { createDetailedAsyncAPI } from '../../../utils'; | ||
import { getMessageExamples, validate } from './messageExamples'; | ||
|
||
export function asyncApi2MessageExamplesParserRule(parser: Parser): RuleDefinition { | ||
return { | ||
description: 'Examples of message object should validate against a payload with an explicit schemaFormat.', | ||
message: '{{error}}', | ||
severity: 'error', | ||
recommended: true, | ||
given: [ | ||
// 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[?(!@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), | ||
}, | ||
}; | ||
} | ||
|
||
function rulesetFunction(parser: Parser) { | ||
return createRulesetFunction<v2.MessageObject, null>( | ||
{ | ||
input: { | ||
type: 'object', | ||
properties: { | ||
name: { | ||
type: 'string', | ||
}, | ||
summary: { | ||
type: 'string', | ||
}, | ||
}, | ||
}, | ||
options: null, | ||
}, | ||
async (targetVal, _, ctx) => { | ||
if (!targetVal.examples) return; | ||
if (!targetVal.payload) return; | ||
|
||
const document = ctx.document; | ||
const parsedSpec = document.data as v2.AsyncAPIObject; | ||
const schemaFormat = getSchemaFormat(targetVal.schemaFormat, parsedSpec.asyncapi); | ||
const defaultSchemaFormat = getDefaultSchemaFormat(parsedSpec.asyncapi); | ||
const asyncapi = createDetailedAsyncAPI(parsedSpec, (document as any).__parserInput, document.source || undefined); | ||
const input: ParseExampleSchemaInput = { | ||
asyncapi, | ||
rootPath: ctx.path, | ||
schemaFormat, | ||
defaultSchemaFormat | ||
}; | ||
|
||
const results: IFunctionResult[] = []; | ||
const payloadSchemaResults = await parseExampleSchema(parser, targetVal.payload, input); | ||
const payloadSchema = payloadSchemaResults.schema; | ||
results.push(...payloadSchemaResults.errors); | ||
|
||
for (const example of getMessageExamples(targetVal)) { | ||
const { path, value } = example; | ||
// validate payload | ||
if (value.payload !== undefined && payloadSchema !== undefined) { | ||
const errors = validate(value.payload, path, 'payload', payloadSchema, ctx); | ||
if (Array.isArray(errors)) { | ||
results.push(...errors); | ||
} | ||
} | ||
} | ||
return results; | ||
} | ||
); | ||
} | ||
|
||
interface ParseExampleSchemaInput { | ||
asyncapi: DetailedAsyncAPI; | ||
rootPath: JsonPath; | ||
schemaFormat: string; | ||
defaultSchemaFormat: string; | ||
} | ||
|
||
interface ParseExampleSchemaResult { | ||
path: Array<string | number>; | ||
schema: v2.AsyncAPISchemaObject | undefined; | ||
errors: IFunctionResult[] | ||
} | ||
|
||
async function parseExampleSchema(parser: Parser, schema: unknown, input: ParseExampleSchemaInput): Promise<ParseExampleSchemaResult> { | ||
const path = [...input.rootPath, 'payload']; | ||
if (schema === undefined) { | ||
return {path, schema: undefined, errors: []}; | ||
} | ||
try { | ||
const parseSchemaInput: ParseSchemaInput = { | ||
asyncapi: input.asyncapi, | ||
data: schema, | ||
meta: {}, | ||
path, | ||
schemaFormat: input.schemaFormat, | ||
defaultSchemaFormat: input.defaultSchemaFormat, | ||
}; | ||
const parsedSchema = await parseSchema(parser, parseSchemaInput); | ||
return {path, schema: parsedSchema, errors: []}; | ||
} catch (err: any) { | ||
const error: IFunctionResult = { | ||
message: `Error thrown during schema validation. Name: ${err.name}, message: ${err.message}, stack: ${err.stack}`, | ||
path | ||
}; | ||
return {path, schema: undefined, errors: [error]}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.