Skip to content
New issue

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

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

Already on GitHub? # to your account

Adding new util function for topics validations #495

Merged
merged 3 commits into from
Oct 25, 2016
Merged
Changes from 2 commits
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
24 changes: 23 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,34 @@
var assert = require('assert');
var InvalidConfigError = require('./errors/InvalidConfigError');
var legalChars = new RegExp('^[a-zA-Z0-9._-]*$');
var allowedTopicLength = 249;
Copy link
Collaborator

Choose a reason for hiding this comment

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

const ?


function validateConfig (property, value) {
if (!legalChars.test(value)) {
throw new InvalidConfigError([property, value, "is illegal, contains a character other than ASCII alphanumerics, '.', '_' and '-'"].join(' '));
}
}

function validateCreateTopics (topics) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe rename this to validateTopicNames since this isn't specific to just creating topics but we can reuse this to validate topics in the other classes.

// Rewriting same validations done by Apache Kafka team for topics
// iterating over topics
topics.some(function (topic) {
if (topic.length <= 0) {
throw new InvalidConfigError('topic name is illegal, cannot be empty');
}
if (topic === '.' || topic === '..') {
throw new InvalidConfigError('topic name cannot be . or ..');
}
if (topic.length > allowedTopicLength) {
throw new InvalidConfigError('topic name is illegal, cannot be longer than ' + allowedTopicLength + ' characters');
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do you mind changing to a template literal? We support node 4 now.

}
if (!legalChars.test(topic)) {
throw new InvalidConfigError('topic name ' + topic + 'is illegal, contains a character other than ASCII alphanumerics .,_ and -');
Copy link
Collaborator

Choose a reason for hiding this comment

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

template literal here as well.

}
});
return true;
}

function validateTopics (topics) {
if (topics.some(function (topic) {
if ('partition' in topic) {
Expand Down Expand Up @@ -79,5 +100,6 @@ module.exports = {
validateConfig: validateConfig,
validateTopics: validateTopics,
groupPartitionsByTopic: groupPartitionsByTopic,
createTopicPartitionList: createTopicPartitionList
createTopicPartitionList: createTopicPartitionList,
validateCreateTopics: validateCreateTopics
};