Skip to content

Commit

Permalink
Deprecated: payload must be valid JSON object
Browse files Browse the repository at this point in the history
  • Loading branch information
maulanasatyaadi committed Feb 2, 2025
1 parent bc28861 commit df67307
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
4 changes: 4 additions & 0 deletions sign.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ const options_for_objects = [
];

module.exports = function (payload, secretOrPrivateKey, options, callback) {
if (typeof payload !== 'object') {
throw Error('Payload must be valid JSON object')
}

if (typeof options === 'function') {
callback = options;
options = {};
Expand Down
34 changes: 26 additions & 8 deletions test/non_object_values.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,34 @@ var expect = require('chai').expect;

describe('non_object_values values', function() {

it('should work with string', function () {
var token = jwt.sign('hello', '123');
var result = jwt.verify(token, '123');
expect(result).to.equal('hello');
it('should does not work with string', function () {
expect(function () {
jwt.sign('hello', '123');
}).to.throw('Payload must be valid JSON object');
});

it('should work with number', function () {
var token = jwt.sign(123, '123');
var result = jwt.verify(token, '123');
expect(result).to.equal('123');
it('should does not work with number', function () {
expect(function () {
jwt.sign(123, '123');
}).to.throw('Payload must be valid JSON object');
});

it('should does not work with function', function () {
expect(function () {
jwt.sign(function () { return 0 }, '123')
}).to.throw('Payload must be valid JSON object')
})

it('should does not work with boolean', function () {
expect(function () {
jwt.sign(true, '123')
}).to.throw('Payload must be valid JSON object')
})

it('should does not work with undefined', function () {
expect(function () {
jwt.sign(undefined, '123')
}).to.throw('Payload must be valid JSON object')
})

});

0 comments on commit df67307

Please sign in to comment.