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

Add empty string check for collection name passed #14806

Merged
merged 2 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ exports.toCollectionName = function(name, pluralize) {
return name;
}
if (typeof pluralize === 'function') {
if (typeof name !== 'string') {
throw new TypeError('Collection name must be a string');
}
if (name.length === 0) {
throw new TypeError('Collection name cannot be empty');
}
return pluralize(name);
}
return name;
Expand Down
27 changes: 27 additions & 0 deletions test/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -324,4 +324,31 @@ describe('utils', function() {
assert.deepEqual(pojoError.metadata, { hello: 'world' });
});
});

describe('toCollectionName', function() {
it('returns the same name for system.profile', function() {
assert.equal(utils.toCollectionName('system.profile'), 'system.profile');
});

it('returns the same name for system.indexes', function() {
assert.equal(utils.toCollectionName('system.indexes'), 'system.indexes');
});

it('throws an error when name is not a string', function() {
assert.throws(() => {
utils.toCollectionName(123, () => {});
}, /Collection name must be a string/);
});

it('throws an error when name is an empty string', function() {
assert.throws(() => {
utils.toCollectionName('', () => {});
}, /Collection name cannot be empty/);
});

it('uses the pluralize function when provided', function() {
const pluralize = (name) => name + 's';
assert.equal(utils.toCollectionName('test', pluralize), 'tests');
});
});
});