From 3c296147608722d9c34ac82054f4bab09a70558c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emanuel=20Lindstr=C3=B6m?= Date: Thu, 20 Jan 2022 12:22:32 +0100 Subject: [PATCH] Update isValid function to match the bson node library The bson node library recently updated the `ObjectID.isValid` method. This PR updates the method to match those changes. The bson library method can be seen here, at line 293: https://github.com/mongodb/js-bson/blob/master/src/objectid.ts --- objectid.js | 27 ++++----------------------- 1 file changed, 4 insertions(+), 23 deletions(-) diff --git a/objectid.js b/objectid.js index 1112244..541f5ad 100644 --- a/objectid.js +++ b/objectid.js @@ -132,31 +132,12 @@ ObjectID.createFromHexString = function(hexString) { ObjectID.isValid = function(id) { if (id == null) return false; - if (typeof id === 'number') { + try { + new ObjectId(id); return true; + } catch { + return false; } - - if (typeof id === 'string') { - return id.length === 12 || (id.length === 24 && checkForHexRegExp.test(id)); - } - - if (id instanceof ObjectID) { - return true; - } - - if (isBuffer(id)) { - return true; - } - - // Duck-Typing detection of ObjectId like objects - if ( - typeof id.toHexString === 'function' && - (id.id instanceof _Buffer || typeof id.id === 'string') - ) { - return id.id.length === 12 || (id.id.length === 24 && checkForHexRegExp.test(id.id)); - } - - return false; }; ObjectID.prototype = {