Check type of parsed JSON to determine it's an object and not a number.
This commit is contained in:
parent
5449ad7bff
commit
c20a0eabed
|
@ -28,9 +28,15 @@ module.exports = {
|
||||||
* @return {Object|boolean} false if the argument is not a JSON string.
|
* @return {Object|boolean} false if the argument is not a JSON string.
|
||||||
*/
|
*/
|
||||||
isValidJSON: function isValidJSON(arg) {
|
isValidJSON: function isValidJSON(arg) {
|
||||||
|
var parsed;
|
||||||
try {
|
try {
|
||||||
return JSON.parse(arg);
|
parsed = JSON.parse(arg);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
parsed = false;
|
||||||
|
}
|
||||||
|
if (typeof(parsed) === 'object') {
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
|
@ -32,7 +32,7 @@ describe('PrivateKey', function() {
|
||||||
should.exist(b.bn);
|
should.exist(b.bn);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should create a privatkey from hexa string', function() {
|
it('should create a privatekey from hexa string', function() {
|
||||||
var a = new PrivateKey(hex2);
|
var a = new PrivateKey(hex2);
|
||||||
should.exist(a);
|
should.exist(a);
|
||||||
should.exist(a.bn);
|
should.exist(a.bn);
|
||||||
|
|
|
@ -0,0 +1,29 @@
|
||||||
|
'use strict';
|
||||||
|
/* jshint unused: false */
|
||||||
|
|
||||||
|
var should = require('chai').should();
|
||||||
|
var expect = require('chai').expect;
|
||||||
|
|
||||||
|
var bitcore = require('../..');
|
||||||
|
var JSUtil = bitcore.util.js;
|
||||||
|
|
||||||
|
describe('js utils', function() {
|
||||||
|
|
||||||
|
describe('isValidJSON', function() {
|
||||||
|
|
||||||
|
var hexa = '8080808080808080808080808080808080808080808080808080808080808080';
|
||||||
|
var json = '{"key": ["value", "value2"]}';
|
||||||
|
|
||||||
|
it('does not mistake an integer as valid json object', function() {
|
||||||
|
var valid = JSUtil.isValidJSON(hexa);
|
||||||
|
valid.should.equal(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('correctly validates a json object', function() {
|
||||||
|
var valid = JSUtil.isValidJSON(json);
|
||||||
|
valid.should.equal(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
Loading…
Reference in New Issue