spectral:asyncapi error on by default

asyncapi-payload-default

Default must be valid against its defined schema.

Applies to
default
Selects
$.components.messageTraits[?(@.schemaFormat === void 0)].payload.default^
$.components.messages[?(@.schemaFormat === void 0)].payload.default^
$.channels[*][publish,subscribe][?(@property === 'message' && @.schemaFormat === void 0)].payload.default^
Function
asyncApiSchemaValidation
Specs
AsyncAPI 2.x
Message
{{error}}

Use it

This rule ships with OpenLint in spectral:asyncapi. Extending the ruleset turns it on:

extends: spectral:asyncapi
rules:
  asyncapi-payload-default: error   # or off, or another severity

Definition

As defined in @openlint/openlint-rulesets. Functions show by name.

asyncapi-payload-default:
  description: Default must be valid against its defined schema.
  message: '{{error}}'
  severity: error
  recommended: true
  formats:
    - AsyncAPI 2.x
  given:
    - '$.components.messageTraits[?(@.schemaFormat === void 0)].payload.default^'
    - '$.components.messages[?(@.schemaFormat === void 0)].payload.default^'
    - >-
      $.channels[*][publish,subscribe][?(@property === 'message' &&
      @.schemaFormat === void 0)].payload.default^
  then:
    function: asyncApiSchemaValidation
    functionOptions:
      type: default

About this rule

default objects should be valid against the payload they decorate.

Good Example

payload:
  type: object
  properties:
    value:
      type: integer
  required:
    - value
  default:
    value: 17

Bad Example

payload:
  type: object
  properties:
    value:
      type: integer
  required:
    - value
  default:
    value: nope!

Sources