Skip to content

Commit d7ecd4c

Browse files
KhudaDad414derberg
andauthored
feat: integrate asyncapi-validator to validate incomming messages (#53)
Co-authored-by: Lukasz Gornicki <lpgornicki@gmail.com>
1 parent f457609 commit d7ecd4c

File tree

5 files changed

+24
-13
lines changed

5 files changed

+24
-13
lines changed

README.md

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
- [How to use the template](#how-to-use-the-template)
1919
* [CLI](#cli)
2020
- [Template configuration](#template-configuration)
21-
- [Custom hooks that you can disable](#custom-hooks-that-you-can-disable)
2221
- [Development](#development)
2322
- [Contributors](#contributors)
2423

@@ -86,10 +85,14 @@ npm start
8685
#for testing your server you can use mqtt client. open a new terminal and install it using:
8786
npm install mqtt -g
8887

89-
#publish your message.
90-
mqtt pub -t 'smartylighting/streetlights/1/0/event/{streetlightId}/lighting/measured' -h 'test.mosquitto.org' -m '{"id": 1, "lumens": 3, "sentAt": "2017-06-07T12:34:32.000Z"}'
88+
#publish an invalid message.
89+
mqtt pub -t 'smartylighting/streetlights/1/0/event/123/lighting/measured' -h 'test.mosquitto.org' -m '{"id": 1, "lumens": "3", "sentAt": "2017-06-07T12:34:32.000Z"}'
9190

92-
# You should see the sent message in the logs of the previously started server.
91+
#publish a valid message
92+
mqtt pub -t 'smartylighting/streetlights/1/0/event/123/lighting/measured' -h 'test.mosquitto.org' -m '{"id": 1, "lumens": 3, "sentAt": "2017-06-07T12:34:32.000Z"}'
93+
94+
#You should see the sent message in the logs of the previously started server.
95+
#Notice that the server automatically validates incoming messages and logs out validation errors
9396
```
9497

9598

@@ -102,13 +105,6 @@ You can configure this template by passing different parameters in the Generator
102105
|---|---|---|---|
103106
|server|The server you want to use in the code.|Yes|`production`|
104107

105-
## Custom hooks that you can disable
106-
107-
The functionality of this template is extended with different hooks that you can disable like this in the Generator CLI: `-d HOOK_TYPE=HOOK_NAME`
108-
109-
Type | Name | Description
110-
---|---|---
111-
generate:after | createAsyncapiFile | It creates AsyncAPI file with content of the spec file passed to the generator
112108

113109
## Development
114110

template/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"dotenv": "^8.1.0",
1111
"hermesjs": "2.x",
1212
"hermesjs-router": "1.x",
13+
"asyncapi-validator": "^3.0.0",
1314
{% if asyncapi.server(params.server).protocol() === 'mqtt' -%}
1415
"hermesjs-mqtt": "2.x",
1516
{%- endif -%}
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
const { red, gray } = require('colors/safe');
22

33
module.exports = (err, message, next) => {
4-
console.error(red(`❗ ${err.message}`));
5-
if (err.stack) console.error(gray(err.stack.substr(err.stack.indexOf('\n')+1)));
4+
if (err.name === 'AsyncAPIValidationError'){
5+
console.error(red(`❗ Message Rejected. ${err.message}`));
6+
}
7+
else {
8+
console.error(red(`❗ ${err.message}`));
9+
if (err.stack) console.error(gray(err.stack.substr(err.stack.indexOf('\n')+1)));
10+
}
611
next();
712
};

template/src/api/routes/$$channel$$.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const Router = require('hermesjs/lib/router');
2+
const {validateMessage} = require('../../lib/message-validator');
23
const router = new Router();
34
const {{ channelName | camelCase }}Handler = require('../handlers/{{ channelName | convertToFilename }}');
45
module.exports = router;
@@ -10,6 +11,7 @@ module.exports = router;
1011
{%- endif %}
1112
router.use('{{ channelName | toHermesTopic }}', async (message, next) => {
1213
try {
14+
await validateMessage(message.payload,'{{ channelName }}','{{ channel.publish().message().name() }}','publish');
1315
await {{ channelName | camelCase }}Handler.{{ channel.publish().id() }}({message});
1416
next();
1517
} catch (e) {
@@ -26,6 +28,7 @@ router.use('{{ channelName | toHermesTopic }}', async (message, next) => {
2628
{%- endif %}
2729
router.useOutbound('{{ channelName | toHermesTopic }}', async (message, next) => {
2830
try {
31+
await validateMessage(message.payload,'{{ channelName }}','{{ channel.subscribe().message().name() }}','subscribe');
2932
await {{ channelName | camelCase }}Handler.{{ channel.subscribe().id() }}({message});
3033
next();
3134
} catch (e) {
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const AsyncApiValidator = require('asyncapi-validator');
2+
3+
module.exports.validateMessage = async (payload, channel,name,operation)=> {
4+
let va = await AsyncApiValidator.fromSource('./asyncapi.yaml', {msgIdentifier: 'name'});
5+
va.validate(name, payload, channel, operation);
6+
}

0 commit comments

Comments
 (0)