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

fix: log the name of the message if it is absent #304

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
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;
}
};
Loading