Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: new guide for message validation #1005

Merged
merged 121 commits into from
Feb 24, 2023
Merged
Changes from 4 commits
Commits
Show all changes
121 commits
Select commit Hold shift + click to select a range
6b7f75d
Create message-validation.md
starlightknown Oct 6, 2022
7fc8b96
fix: tutorial to guide
starlightknown Oct 6, 2022
8b9c69e
Merge branch 'master' into guide
starlightknown Oct 6, 2022
80e38f3
Merge branch 'master' into guide
quetzalliwrites Oct 6, 2022
3398977
Update pages/docs/guides/message-validation.md
starlightknown Oct 6, 2022
1041b60
Update pages/docs/guides/message-validation.md
starlightknown Oct 6, 2022
5bd9707
Merge branch 'master' into guide
quetzalliwrites Oct 7, 2022
b3aa91c
Update message-validation.md
starlightknown Oct 10, 2022
bf7e8f9
Update message-validation.md
starlightknown Oct 10, 2022
e8dc4cc
Merge branch 'master' into guide
quetzalliwrites Oct 11, 2022
2b84e48
re-wrote guide
quetzalliwrites Oct 12, 2022
7e7c1c5
Update message-validation.md
starlightknown Oct 12, 2022
23897c5
Update message-validation.md
starlightknown Oct 12, 2022
da51491
Update message-validation.md
starlightknown Oct 12, 2022
12eebd8
Update message-validation.md
starlightknown Oct 12, 2022
144fd06
fix: added additional resources
starlightknown Oct 12, 2022
a6d7f17
Update pages/docs/guides/message-validation.md
starlightknown Oct 12, 2022
e42f134
Merge branch 'master' into guide
starlightknown Oct 12, 2022
65ddb14
Merge branch 'master' into guide
starlightknown Oct 24, 2022
28c86c6
Merge branch 'master' into guide
quetzalliwrites Oct 26, 2022
8f4278a
rewrote the guide
starlightknown Oct 26, 2022
147579e
Merge branch 'master' into guide
starlightknown Oct 26, 2022
f112fa9
Merge branch 'master' into guide
starlightknown Nov 6, 2022
240cf57
Update message-validation.md
starlightknown Nov 8, 2022
3807ac6
Merge branch 'master' into guide
starlightknown Nov 8, 2022
9ee03b9
Merge branch 'master' into guide
quetzalliwrites Nov 8, 2022
7fd7653
Update pages/docs/guides/message-validation.md
starlightknown Nov 9, 2022
225b6b5
Update pages/docs/guides/message-validation.md
starlightknown Nov 9, 2022
d9fcab4
Update pages/docs/guides/message-validation.md
starlightknown Nov 9, 2022
4eb2802
Update pages/docs/guides/message-validation.md
starlightknown Nov 9, 2022
8cc349a
Update pages/docs/guides/message-validation.md
starlightknown Nov 9, 2022
81107ab
Update pages/docs/guides/message-validation.md
starlightknown Nov 9, 2022
1161010
Update pages/docs/guides/message-validation.md
starlightknown Nov 9, 2022
b2d87c4
Update pages/docs/guides/message-validation.md
starlightknown Nov 9, 2022
3d7e76a
Update pages/docs/guides/message-validation.md
starlightknown Nov 9, 2022
7c52344
Update pages/docs/guides/message-validation.md
starlightknown Nov 9, 2022
901587e
Update pages/docs/guides/message-validation.md
starlightknown Nov 9, 2022
87d7b4e
Update pages/docs/guides/message-validation.md
starlightknown Nov 9, 2022
3e66201
Update pages/docs/guides/message-validation.md
starlightknown Nov 9, 2022
303703f
Update pages/docs/guides/message-validation.md
starlightknown Nov 9, 2022
53eb88e
Update pages/docs/guides/message-validation.md
starlightknown Nov 9, 2022
ef5ceb6
Update message-validation.md
starlightknown Nov 9, 2022
ec7435c
Merge branch 'master' into guide
quetzalliwrites Nov 16, 2022
1a0e42c
Update pages/docs/guides/message-validation.md
starlightknown Nov 16, 2022
24e770d
Update pages/docs/guides/message-validation.md
starlightknown Nov 16, 2022
4d992a6
Update pages/docs/guides/message-validation.md
starlightknown Nov 16, 2022
9909fb3
Update message-validation.md
starlightknown Nov 22, 2022
f0180c9
Update pages/docs/guides/message-validation.md
starlightknown Nov 24, 2022
998df2b
Update pages/docs/guides/message-validation.md
starlightknown Nov 24, 2022
cec215d
Update pages/docs/guides/message-validation.md
starlightknown Nov 24, 2022
d513aee
Update message-validation.md
starlightknown Nov 24, 2022
484f60a
Update message-validation.md
starlightknown Nov 24, 2022
902a9d1
Merge branch 'master' into guide
quetzalliwrites Dec 6, 2022
ca19c20
Update pages/docs/guides/message-validation.md
starlightknown Dec 6, 2022
955ce30
Update message-validation.md
starlightknown Dec 6, 2022
462784d
Update message-validation.md
starlightknown Dec 6, 2022
5483f30
Update message-validation.md
starlightknown Dec 6, 2022
dc84aa5
Merge branch 'master' into guide
starlightknown Dec 6, 2022
55bf3e0
Update message-validation.md
starlightknown Dec 6, 2022
6c5e17d
Update pages/docs/guides/message-validation.md
starlightknown Dec 7, 2022
039d293
Update pages/docs/guides/message-validation.md
starlightknown Dec 7, 2022
b3e3061
Update pages/docs/guides/message-validation.md
starlightknown Dec 7, 2022
805a5e8
Update pages/docs/guides/message-validation.md
starlightknown Dec 7, 2022
0d7dc93
Update message-validation.md
starlightknown Dec 8, 2022
64fb62b
Merge branch 'master' into guide
starlightknown Dec 8, 2022
b8b7f26
Update message-validation.md
starlightknown Dec 8, 2022
dd0c2e1
Update message-validation.md
starlightknown Dec 8, 2022
9cf6212
Merge branch 'master' into guide
quetzalliwrites Dec 10, 2022
cc9c7c7
Update pages/docs/guides/message-validation.md
starlightknown Dec 13, 2022
b44277a
Update pages/docs/guides/message-validation.md
starlightknown Dec 13, 2022
3b2bec4
Update pages/docs/guides/message-validation.md
starlightknown Dec 13, 2022
b8e6acc
Merge branch 'master' into guide
quetzalliwrites Dec 14, 2022
16c370e
Merge branch 'master' into guide
quetzalliwrites Dec 16, 2022
08a4cc4
Merge branch 'master' into guide
quetzalliwrites Dec 20, 2022
04dda05
Update message-validation.md
starlightknown Dec 21, 2022
0064510
Update message-validation.md
starlightknown Dec 21, 2022
6b18b15
Merge branch 'master' into guide
starlightknown Dec 28, 2022
f10b236
updated diagram
starlightknown Dec 28, 2022
ec94084
Merge branch 'master' into guide
starlightknown Jan 4, 2023
1a5b760
Merge branch 'master' into guide
quetzalliwrites Jan 17, 2023
d57a309
Update pages/docs/guides/message-validation.md
starlightknown Jan 17, 2023
202c58d
Update pages/docs/guides/message-validation.md
starlightknown Jan 17, 2023
b2f5507
Update pages/docs/guides/message-validation.md
starlightknown Jan 17, 2023
c9bc0a6
Update pages/docs/guides/message-validation.md
starlightknown Jan 17, 2023
91cba0b
Update pages/docs/guides/message-validation.md
starlightknown Jan 17, 2023
ccaa3d2
Update pages/docs/guides/message-validation.md
starlightknown Jan 17, 2023
7e5be0d
Update pages/docs/guides/message-validation.md
starlightknown Jan 17, 2023
81e37dc
Update pages/docs/guides/message-validation.md
starlightknown Jan 17, 2023
d98c806
Update pages/docs/guides/message-validation.md
starlightknown Jan 17, 2023
9c6c64d
Update pages/docs/guides/message-validation.md
starlightknown Jan 17, 2023
aac8d37
Update pages/docs/guides/message-validation.md
starlightknown Jan 17, 2023
3a157fb
Update pages/docs/guides/message-validation.md
starlightknown Jan 17, 2023
ab70249
Update pages/docs/guides/message-validation.md
starlightknown Jan 17, 2023
0ecc659
Update pages/docs/guides/message-validation.md
starlightknown Jan 17, 2023
cb2d9fb
Update pages/docs/guides/message-validation.md
starlightknown Jan 17, 2023
b7d4db6
Update pages/docs/guides/message-validation.md
starlightknown Jan 17, 2023
e2ac99b
Update pages/docs/guides/message-validation.md
starlightknown Jan 17, 2023
d2650c5
Update pages/docs/guides/message-validation.md
starlightknown Jan 17, 2023
73e5968
Merge branch 'master' into guide
starlightknown Jan 17, 2023
2df2997
Update pages/docs/guides/message-validation.md
starlightknown Jan 17, 2023
af1dc80
Update message-validation.md
starlightknown Jan 17, 2023
a7b7d1a
Update message-validation.md
starlightknown Jan 17, 2023
2a78510
Merge branch 'master' into guide
quetzalliwrites Jan 18, 2023
813c5bc
Update message-validation.md
starlightknown Jan 18, 2023
c4c2c4a
Update message-validation.md
starlightknown Jan 20, 2023
013edee
Merge branch 'master' into guide
quetzalliwrites Jan 20, 2023
cce9e1b
Update pages/docs/guides/message-validation.md
starlightknown Jan 21, 2023
4e3a316
Merge branch 'master' into guide
starlightknown Jan 22, 2023
690cc26
Merge branch 'master' into guide
quetzalliwrites Jan 25, 2023
e725aa8
fixed page weight
quetzalliwrites Jan 25, 2023
88e6f5c
Merge branch 'master' into guide
quetzalliwrites Feb 2, 2023
66b85fa
Merge branch 'master' into guide
quetzalliwrites Feb 8, 2023
953d073
Update pages/docs/guides/message-validation.md
starlightknown Feb 8, 2023
6dc9b4a
Update pages/docs/guides/message-validation.md
starlightknown Feb 8, 2023
f7541c2
Update pages/docs/guides/message-validation.md
starlightknown Feb 8, 2023
bd6b2ed
Update message-validation.md
starlightknown Feb 8, 2023
d1963a3
Update pages/docs/guides/message-validation.md
starlightknown Feb 8, 2023
6d8a03d
Merge branch 'master' into guide
starlightknown Feb 8, 2023
8ab2941
Merge branch 'master' into guide
quetzalliwrites Feb 9, 2023
1222695
Merge branch 'master' into guide
quetzalliwrites Feb 24, 2023
5ba8792
Merge branch 'master' into guide
quetzalliwrites Feb 24, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
274 changes: 274 additions & 0 deletions pages/docs/guides/message-validation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,274 @@
---
title: How to validate Messages/Events in runtime
description: In this guide, you'll learn how to validate messages/events in AsyncAPI in runtime.
---

starlightknown marked this conversation as resolved.
Show resolved Hide resolved
# Introduction

In this guide, you will learn how to validate the messages in your AsyncAPI document after creating your application in runtime.
You will create an application using Node.js.
- Your application will connect to the message broker and receive a stream of events/ messages.
- Message validator will check the message name, payload, channel and operation (publish/subscribe)
- Message validator reads the AsyncAPI document if it is valid or not and returns an error if it is invalid.
You will learn about application run time, messages and creating an AsyncAPI file to describe your API and generate code from it.

# Prerequisites

You'll use Node.js to code the API. The selected technology is irrelevant here, since everything explained in this guide is applicable to any other programming language and message brokers.

Before you proceed to the next stage, you'll need to download a few things:

1. Install [Node.js](https://nodejs.org/en/download/) (v15 or newer).

2. Install Git based on your Operating System:
- [Install Git on MacOs](https://git-scm.com/download/mac)
- [Install Git on Windows](https://git-scm.com/download/win)
- [Install Git on Linux](https://git-scm.com/download/linux)

# Validation of messages

Validation of messages can be done in these scenarios

- [asyncapi-validator](https://github.com/WaleedAshraf/asyncapi-validator), it validates your messages against your AsyncAPI schema.
- [AsyncAPI event gateway](https://github.com/asyncapi/event-gateway), it is not a library that you intergrate in an App, but validation is done before the message gets into the app, on a gateway.

# Validation using AsyncAPI Validator

It is a message validator through AsyncAPI schema.
1. In order to validate messages, you need to install this package
<CodeBlock language="bash">
{`npm i asyncapi-validator`}
</CodeBlock>

2. To validate messages using `messageId`
Here messageId should be as defined in [AsyncAPI Schema v2.4.0](https://www.asyncapi.com/docs/reference/specification/v2.4.0#messageObject).
<CodeBlock>
{`.validateByMessageId(key, payload)`}
</CodeBlock>

3. To validate messages using `.validate()` method you should provide `msgIdentifier` in AsyncApiValidator `options`.

<CodeBlock>
{`.validate(key, payload, channel, operation)`}
</CodeBlock>

# Here's an example usecase with `.validateByMessageId()` method

1. Let's first create the AsyncAPI file
<CodeBlock language="yaml">
{`cat <<EOT >> asyncapi.yaml
asyncapi: 2.0.0
starlightknown marked this conversation as resolved.
Show resolved Hide resolved
info:
title: User Events
version: 1.0.0
channels:
user-events:
description: user related events
publish:
message:
name: UserDeletedMessage
x-custom-key: UserDeleted
payload:
type: object
properties:
userEmail:
type: string
userId:
type: string
EOT`}
</CodeBlock>

2. Now by using `asyncapi-validator`.To validate incoming MQTT messages, you have to load the AsyncAPI schema definition using the `fromSource` method and then you can validate any message with its key and payload using the validate method.

<CodeBlock>
{`const AsyncApiValidator = require('asyncapi-validator')
let va = await AsyncApiValidator.fromSource('./api.yaml')
// validate messageId 'UserRemoved'
va.validateByMessageId('UserRemoved', {
userId: '123456789',
userEmail: 'alex@mail.com',
})`}
</CodeBlock>

# Here's an example usecase with .validate() method

1. Let's create another AsyncAPI file

<CodeBlock language="yaml">
{`asyncapi: 2.0.0
info:
title: User Events
version: 1.0.0
channels:
user-events:
description: user related events
publish:
message:
name: UserDeletedMessage
x-custom-key: UserDeleted
payload:
type: object
properties:
userEmail:
type: string
userId:
type: string`}
</CodeBlock>

2. Now by using `asyncapi-validator`. here "msgIdentifier" is "x-custom-key". That is why, "UserDeleted" is used as "key" in "va.validate()" method.

<CodeBlock>
{`const AsyncApiValidator = require('asyncapi-validator')
let va = await AsyncApiValidator.fromSource('./api.yaml', {msgIdentifier: 'x-custom-key'})
// validate 'UserDeleted' on channel 'user-events' with operation 'publish'
va.validate('UserDeleted', {
userId: '123456789',
userEmail: 'alex@mail.com',
}, 'user-events', 'publish')`}
</CodeBlock>

# Validation using AsyncAPI gateway

AsyncAPI gateway intercepts all incoming messages moving them into a pipeline of middlewares and handlers such as Message validator.As per today, only the Kafka protocol is supported.

# Here's an example usecase using AsyncAPI gateway

1. Let's create the AsyncAPI file. Expected messages are based on a small portion of the StreetLights tutorial.

<CodeBlock language="yaml">
{`asyncapi: '2.4.0'
info:
title: AsyncAPI Event-Gateway demo API
version: 1.0.0-alpha
description: This API lets users interact with an instance of the [AsyncAPI Event-Gateway](https://github.com/asyncapi/event-gateway).
defaultContentType: application/json
servers:
asyncapi-event-gateway-demo:
url: 'event-gateway-demo.asyncapi.com:20472'
protocol: kafka
description: AsyncAPI [Event-Gateway](https://github.com/asyncapi/event-gateway) demo Kafka proxy. Expected messages are based on a small portion of the [StreetLights tutorial](https://bit.ly/asyncapi).
asyncapi-event-gateway-demo-validation:
url: 'event-gateway-demo.asyncapi.com:5000/ws'
protocol: ws
description: AsyncAPI [Event-Gateway](https://github.com/asyncapi/event-gateway) demo. Subscribe for Kafka proxy message validation errors.
asyncapi-kafka-test:
url: 'asyncapi-kafka-test-asyncapi-8f90.aivencloud.com:20472' # Kafka with 3 brokers.
protocol: kafka-secure
description: AsyncAPI Kafka test broker. Private.
channels:
event-gateway-demo:
description: Demo Kafka topic for asyncapi-event-gateway-demo server. Users can send their events to this topic and see how message validation happens on the fly based on this right AsyncAPI file by connecting to `event-gateway-demo-validation-events` channel (`asyncapi-event-gateway-demo-validation` ws server).
starlightknown marked this conversation as resolved.
Show resolved Hide resolved
x-servers: # Based on https://github.com/asyncapi/spec/pull/531
- asyncapi-event-gateway-demo
publish:
message:
$ref: '#/components/messages/lightMeasured'
event-gateway-demo-validation-events:
description: Validation errors are published here, so users can see how message validation happens on the fly based on this right AsyncAPI file.
x-servers: # Based on https://github.com/asyncapi/spec/pull/531
- asyncapi-event-gateway-demo-validation
subscribe:
message:
$ref: '#/components/messages/invalidMessage'
event-gateway-demo-validation:
description: Validation errors are published to and consumed from it. AsyncAPI Event-gateway is the only user of this channel. It can be consumed and exposed via `event-gateway-demo-validation-events` channel (`asyncapi-event-gateway-demo-validation` ws server).
starlightknown marked this conversation as resolved.
Show resolved Hide resolved
x-servers: # Based on https://github.com/asyncapi/spec/pull/531
- asyncapi-kafka-test
subscribe:
message:
$ref: '#/components/messages/invalidMessage'
components:
messages:
# lightMeasured is copied from the Streetlights tutorial instead of using references due to a bug in parser-go: https://github.com/asyncapi/parser-go/issues/82
lightMeasured:
name: lightMeasured
title: Light measured
summary: Inform about environmental lighting conditions of a particular streetlight.
contentType: application/json
traits:
- $ref: '#/components/messageTraits/commonHeaders'
payload:
$ref: "#/components/schemas/lightMeasuredPayload"
invalidMessage:
title: Invalid message
summary: Message with a Validation Error.
contentType: application/json
payload:
$ref: "#/components/schemas/sentMessage"
headers:
type: object
properties:
_asyncapi_eg_channel:
type: string
description: AsyncAPI Channel where the message was published to.
_asyncapi_eg_validation_error:
$ref: '#/components/schemas/validationError'
schemas:
sentMessage:
type: object
properties:
UUID:
type: string
description: Unique identifier of message. I.e. Kafka message key.
Payload:
type: string
description: Message value. I.e. Kafka message (base64).
examples:
- UUID: 'YXN5bmNhcGktd2FzLWhlcmU='
Payload: 'eyJsdW1lbnMiOiAid2hhdGV2ZXIifQ=='
validationError:
type: object
properties:
ts:
type: string
description: RFC-3339 date-time. Date and time when the message was validated.
errors:
type: array
description: Array of string. Validation errors.
items:
type: string
examples:
- ts: '2021-09-10T12:04:18:475203609Z'
errors: [ 'lumens: Invalid type. Expected: integer, given: string' ]
lightMeasuredPayload:
type: object
properties:
lumens:
type: integer
minimum: 0
description: Light intensity measured in lumens.
sentAt:
$ref: "#/components/schemas/sentAt"
sentAt:
type: string
format: date-time
description: Date and time when the message was sent.
messageTraits:
commonHeaders:
headers:
type: object
properties:
my-app-header:
type: integer
minimum: 0
maximum: 100`}
</CodeBlock>

2. The gateway accepts the light measured `lightMeasured` as a message to inform about environmental lighting conditions of a particular streetlight.

<CodeBlock>
{`lumens integer >= 0
Light intensity measured in lumens.
sentAt string format: date-time uid: sentAt
Date and time when the message was sent.`}

</CodeBlock>

3. It returns the error when it detects the following invalid message `invalidMessage`

<CodeBlock>
{`UUID string
Unique identifier of message. I.e. Kafka message key.
Payload string
Message value. I.e. Kafka message (base64).`}
</CodeBlock>