Skip to content

Commit

Permalink
Merge pull request #28 from niftylettuce/master
Browse files Browse the repository at this point in the history
fix: fixed ObjectID.isValid logic (closes #27)
  • Loading branch information
williamkapke authored Jun 18, 2019
2 parents 37ab6eb + 6695135 commit e5e6986
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion objectid.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ ObjectID.createFromHexString = function(hexString) {
* http://mongodb.github.io/node-mongodb-native/api-bson-generated/objectid.html#objectid-isvalid
*/
ObjectID.isValid = function(objectid) {
if(!objectid) return false;
if(!objectid || (typeof objectid !== 'string' && (typeof objectid !== 'object' || typeof objectid.toString !== 'function'))) return false;

//call .toString() to get the hex if we're
// working with an instance of ObjectID
Expand Down
6 changes: 6 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,5 +174,11 @@ describe("ObjectIDs", function() {

result[2].should.eql(mid.toString(16));
});

it("should not throw an error for objects without toString", function() {
var obj = Object.create({}, { toString: { value: false, writeable: false } });
obj.toString.should.not.be.ok;
ObjectID.isValid(obj).should.not.be.ok;
});
});

0 comments on commit e5e6986

Please sign in to comment.