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

Adding new util function for topics validations #495

Merged
merged 3 commits into from
Oct 25, 2016
Merged
Changes from all 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._-]*$');
const allowedTopicLength = 249;

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 validateTopicNames (topics) {
// 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`);
}
if (!legalChars.test(topic)) {
throw new InvalidConfigError(`topic name ${topic} is illegal, contains a character other than ASCII alphanumerics .,_ and -`);
}
});
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,
validateTopicNames: validateTopicNames
};