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

Kafka topics should not contain special characters #492

Merged
merged 10 commits into from
Oct 26, 2016
13 changes: 13 additions & 0 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,12 +349,25 @@ Client.prototype.loadMetadataForTopics = function (topics, cb) {
};

Client.prototype.createTopics = function (topics, isAsync, cb) {
function isValid (str) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could use validateConfig in lib/utils.js?. And it's might be easier to just check for legal characters than invalid ones. Check out the validation used in Topic.scala

Bonus points for:

  • Check length
  • Add topic validation to Consumer, HighLevelConsumer, and ConsumerGroup

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. Make sense.

return !/[~`!#$%\^&*+=\-\[\]\\';,/{}|\\":<>\?]/g.test(str);
}
topics = typeof topics === 'string' ? [topics] : topics;

if (typeof isAsync === 'function' && typeof cb === 'undefined') {
cb = isAsync;
isAsync = true;
}
var validTopics = true;
Object.keys(topics).map(function (elem) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Array.some could be used here and may not need to iterate through the entire list.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good one (Y)

if (!isValid(topics[elem])) {
validTopics = false;
}
});

if (!validTopics) {
return cb(null, 'Some topics contains special characters, please remove them');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets continue with the error first style and call the callback with a new Error as the first argument.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool.

}
var self = this;

// first, load metadata to create topics
Expand Down