# operation-messages-from-channel

Source: https://api-genie.dev/docs/linter/asyncapi/rules/operation-messages-from-channel

A rule of the AsyncAPI linter, which reads AsyncAPI 3 documents; the reader rejects AsyncAPI 2 documents. Since 0.12.0. In the rulesets: Recommended at error, Strict at error, All at error.

This rule checks that the messages of an operation, and of its reply, are references to messages
of the channel that the operation, or the reply, references.

## Rule details

An operation's `messages` lists which of its channel's messages it sends or receives. The
specification requires each entry to be a reference to a message of that channel, so with the
channel `#/channels/pet`, every entry starts with `#/channels/pet/messages/`. A reference to the
same message under `components/messages` is reported too: it says nothing about the channel. The
same holds for the `messages` of a reply that has a `channel`. The rule compares references as
written: a message in another file is reported, unless the channel is a reference into the same
file and the message is under that channel.

The rule checks operations under `operations` and `components/operations`, and replies under
`components/replies`, where each is defined. A reference to one is not checked again.

The rule reports `Operation message '<ref>' must be a reference to a message of its channel
('<channel>/messages/...')`. For a reply, the message starts with `Reply message` instead.

The rule is the counterpart of the AsyncAPI parser's
`asyncapi3-operation-messages-from-referred-channel`, which does not check replies.

Turn the rule off for a spec whose channels are in this file and whose operations name their
messages in another file: the linter does not read other files, and `asyncapi validate` accepts
the reference.

## Examples

**Good**.

```yaml
asyncapi: 3.0.0
info:
  title: Pet Events
  version: 1.0.0
channels:
  pet:
    address: pets
    messages:
      petAdopted:
        $ref: '#/components/messages/petAdopted'
operations:
  onPetAdopted:
    action: receive
    channel:
      $ref: '#/channels/pet'
    messages:
      - $ref: '#/channels/pet/messages/petAdopted'
components:
  messages:
    petAdopted:
      payload:
        type: object
```

**Bad**. The operation names the message under `components`.

```yaml
asyncapi: 3.0.0
info:
  title: Pet Events
  version: 1.0.0
channels:
  pet:
    address: pets
    messages:
      petAdopted:
        $ref: '#/components/messages/petAdopted'
operations:
  onPetAdopted:
    action: receive
    channel:
      $ref: '#/channels/pet'
    messages:
      - $ref: '#/components/messages/petAdopted'
components:
  messages:
    petAdopted:
      payload:
        type: object
```
