From eef7ce2fca78213b85a797ef2119635d37109fd5 Mon Sep 17 00:00:00 2001 From: Stephen Sawchuk Date: Wed, 15 Apr 2015 10:18:21 -0400 Subject: [PATCH] check for data property --- lib/common/util.js | 4 +--- lib/pubsub/topic.js | 10 ++++------ test/pubsub/topic.js | 8 ++++---- 3 files changed, 9 insertions(+), 13 deletions(-) diff --git a/lib/common/util.js b/lib/common/util.js index d75de15747d..da91181546c 100644 --- a/lib/common/util.js +++ b/lib/common/util.js @@ -245,9 +245,7 @@ function getType(value) { */ function prop(name) { return function(item) { - if (name in item) { - return item[name]; - } + return item[name]; }; } diff --git a/lib/pubsub/topic.js b/lib/pubsub/topic.js index a47cda5140b..2479cf791cb 100644 --- a/lib/pubsub/topic.js +++ b/lib/pubsub/topic.js @@ -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. @@ -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; diff --git a/test/pubsub/topic.js b/test/pubsub/topic.js index d4a4fbd98f5..9f281f0dd4f 100644 --- a/test/pubsub/topic.js +++ b/test/pubsub/topic.js @@ -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) {