Skip to content

Commit

Permalink
log the name of the message
Browse files Browse the repository at this point in the history
  • Loading branch information
ibishal committed Jun 20, 2024
1 parent 1019f01 commit 8877f89
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"eslint-plugin-react": "^7.34.1",
"filenamify": "^4.3.0",
"js-beautify": "^1.15.1",
"js-yaml": "^4.1.0",
"lodash": "^4.17.21",
"markdown-toc": "^1.2.0"
},
Expand Down
48 changes: 40 additions & 8 deletions template/src/lib/message-validator.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,33 @@
const yaml = require('js-yaml');
const fs = require('fs');
const path = require('path');
const AsyncApiValidator = require('asyncapi-validator');

function validateMessageIdentifiers(asyncApiSpec) {
const messages = asyncApiSpec.components.messages;
const missingNames = [];

for (const [messageName, messageObj] of Object.entries(messages)) {
if (!messageObj.name) {
missingNames.push(messageName);
}
}

return missingNames;
}

function performPreGenValidation(asyncApiFilePath) {
const fileContents = fs.readFileSync(asyncApiFilePath, 'utf8');
const asyncApiSpec = yaml.load(fileContents);

const missingNames = validateMessageIdentifiers(asyncApiSpec);

if (missingNames.length > 0) {
const errorMessage = `msgIdentifier "name" does not exist for message(s): ${missingNames.join(', ')}`;
throw new Error(errorMessage);
}
}

// Try to parse the payload, and increment nValidated if parsing was successful.
module.exports.validateMessage = async (
payload,
Expand All @@ -9,12 +36,17 @@ module.exports.validateMessage = async (
operation,
nValidated = 0
) => {
const asyncApiFilePath = path.resolve(__dirname, '../../asyncapi.yaml');
const va = await AsyncApiValidator.fromSource(asyncApiFilePath, {
msgIdentifier: 'name',
});
va.validate(messageName, payload, channelName, operation);
nValidated++;

return nValidated;
try {
const asyncApiFilePath = path.resolve(__dirname, "../../asyncapi.yaml");
const va = await AsyncApiValidator.fromSource(asyncApiFilePath, {
msgIdentifier: "name",
});
va.validate(messageName, payload, channelName, operation);
nValidated++;

return nValidated;
} catch (error) {
error.name = "AsyncAPIValidationError";
throw error;
}
};

0 comments on commit 8877f89

Please sign in to comment.