Skip to content

Deprecated: payload must be valid JSON object #986

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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')
})

});