Skip to content

Commit b8e77ab

Browse files
authored
Merge pull request #18 from jeskew/fix/json-messages
Update MessageValidator#validate to accept JSON-encoded messages
2 parents 761f13e + ae795b3 commit b8e77ab

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed

index.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,15 +189,23 @@ function MessageValidator(hostPattern, encoding) {
189189
* @param {validationCallback} cb
190190
*/
191191
MessageValidator.prototype.validate = function (hash, cb) {
192-
var hostPattern = this.hostPattern;
192+
if (typeof hash === 'string') {
193+
try {
194+
hash = JSON.parse(hash);
195+
} catch (err) {
196+
cb(err);
197+
return;
198+
}
199+
}
200+
193201
hash = convertLambdaMessage(hash);
194202

195203
if (!validateMessageStructure(hash)) {
196204
cb(new Error('Message missing required keys.'));
197205
return;
198206
}
199207

200-
if (!validateUrl(hash['SigningCertURL'], hostPattern)) {
208+
if (!validateUrl(hash['SigningCertURL'], this.hostPattern)) {
201209
cb(new Error('The certificate is located on an invalid domain.'));
202210
return;
203211
}

package.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77
"url": "https://github.com/aws/aws-js-sns-message-validator.git"
88
},
99
"main": "index.js",
10-
"directories": {
11-
"test": "test"
12-
},
1310
"devDependencies": {
1411
"chai": "^3.3.0",
1512
"mocha": "^2.3.3",
@@ -19,7 +16,7 @@
1916
"underscore": "^1.8.3"
2017
},
2118
"scripts": {
22-
"test": "node_modules/mocha/bin/mocha"
19+
"test": "mocha"
2320
},
2421
"keywords": [
2522
"AWS",

test/validator.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,11 @@ describe('Message Validator', function () {
210210
(new MessageValidator(/^localhost:56789$/))
211211
.validate(validMessage, done);
212212
});
213+
214+
it('should accept valid messages as JSON strings', function (done) {
215+
(new MessageValidator(/^localhost:56789$/))
216+
.validate(JSON.stringify(validMessage), done);
217+
});
213218
});
214219

215220
describe('subscription control message validation', function () {
@@ -239,7 +244,6 @@ describe('Message Validator', function () {
239244
});
240245

241246
describe('UTF8 message validation', function () {
242-
243247
it('should accept a valid UTF8 message', function (done) {
244248
(new MessageValidator(/^localhost:56789$/, 'utf8'))
245249
.validate(utf8Message, done);

0 commit comments

Comments
 (0)