Skip to content

Commit

Permalink
check for data property
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenplusplus committed Apr 15, 2015
1 parent b6ba3e9 commit eef7ce2
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 13 deletions.
4 changes: 1 addition & 3 deletions lib/common/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,7 @@ function getType(value) {
*/
function prop(name) {
return function(item) {
if (name in item) {
return item[name];
}
return item[name];
};
}

Expand Down
10 changes: 4 additions & 6 deletions lib/pubsub/topic.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ Topic.prototype.autoCreateWrapper_ = function(method, path, q, body, callback) {
* messageIds is returned in the response.
*
* @throws {Error} If no message is provided.
* @throws {Error} If a message is not an object.
* @throws {Error} If a message is missing a data property.
*
* @param {object|object[]} message - The message(s) to publish.
* @param {*} message.data - The contents of the message.
Expand Down Expand Up @@ -179,11 +179,9 @@ Topic.prototype.publish = function(messages, callback) {
throw new Error('Cannot publish without a message.');
}

messages.forEach(function(message) {
if (!util.is(message, 'object')) {
throw new Error('Cannot publish message:\n\t' + JSON.stringify(message));
}
});
if (!messages.every(util.prop('data'))) {
throw new Error('Cannot publish message without a `data` property.');
}

callback = callback || util.noop;

Expand Down
8 changes: 4 additions & 4 deletions test/pubsub/topic.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,17 +129,17 @@ describe('Topic', function() {
it('should throw if no message is provided', function() {
assert.throws(function() {
topic.publish();
}, /Cannot publish/);
}, /Cannot publish without a message/);

assert.throws(function() {
topic.publish([]);
}, /Cannot publish/);
}, /Cannot publish without a message/);
});

it('should throw if a message is not an object', function() {
it('should throw if a message has no data', function() {
assert.throws(function() {
topic.publish(message);
}, /Cannot publish message/);
}, /Cannot publish message without a `data` property/);
});

it('should send correct api request', function(done) {
Expand Down

0 comments on commit eef7ce2

Please sign in to comment.