Skip to content

Commit eef7ce2

Browse files
check for data property
1 parent b6ba3e9 commit eef7ce2

File tree

3 files changed

+9
-13
lines changed

3 files changed

+9
-13
lines changed

lib/common/util.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,9 +245,7 @@ function getType(value) {
245245
*/
246246
function prop(name) {
247247
return function(item) {
248-
if (name in item) {
249-
return item[name];
250-
}
248+
return item[name];
251249
};
252250
}
253251

lib/pubsub/topic.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ Topic.prototype.autoCreateWrapper_ = function(method, path, q, body, callback) {
134134
* messageIds is returned in the response.
135135
*
136136
* @throws {Error} If no message is provided.
137-
* @throws {Error} If a message is not an object.
137+
* @throws {Error} If a message is missing a data property.
138138
*
139139
* @param {object|object[]} message - The message(s) to publish.
140140
* @param {*} message.data - The contents of the message.
@@ -179,11 +179,9 @@ Topic.prototype.publish = function(messages, callback) {
179179
throw new Error('Cannot publish without a message.');
180180
}
181181

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

188186
callback = callback || util.noop;
189187

test/pubsub/topic.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,17 +129,17 @@ describe('Topic', function() {
129129
it('should throw if no message is provided', function() {
130130
assert.throws(function() {
131131
topic.publish();
132-
}, /Cannot publish/);
132+
}, /Cannot publish without a message/);
133133

134134
assert.throws(function() {
135135
topic.publish([]);
136-
}, /Cannot publish/);
136+
}, /Cannot publish without a message/);
137137
});
138138

139-
it('should throw if a message is not an object', function() {
139+
it('should throw if a message has no data', function() {
140140
assert.throws(function() {
141141
topic.publish(message);
142-
}, /Cannot publish message/);
142+
}, /Cannot publish message without a `data` property/);
143143
});
144144

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

0 commit comments

Comments
 (0)