mongoose objectId 验证无法正常工作

mongoose objectId validation doesnt work correctly

我尝试了类似这些字符串的东西,结果 return true 出乎意料!!

这个功能靠谱吗?

mongoose.Types.ObjectId.isValid('mohamma22145')

ObjectId.isValid(id) return 即使对于长度为 12 的无效字符串也是如此。 例如:

String ID                |   ObjectId.isValid(id)  |    Expected Validation               
---------------------------------------------------------------------------
594ced02ed345b2b049222c5 |  true                   |  true
dania                    |  false                  |  false
toptoptoptop             |  true(but isnt)         |  false
daniandanian             |  true(but isnt)         |  false
---------------------------------------------------------------------------

为了防止这种情况,可以在默认验证器之后添加另一个检查,这将 return 根据条件判断真假,(从字符串创建的新 ObjectId)转换为字符串 === 字符串 即 (String)(new ObjectId(id)) === id

可以创建如下函数来检查字符串是否为有效的 ObjectId:

// Requiring ObjectId from mongoose npm package
const ObjectId = require('mongoose').Types.ObjectId;
  
// Validator function
function isValidObjectId(id){
      
    if(ObjectId.isValid(id)){
        if((String)(new ObjectId(id)) === id)
            return true;        
        return false;
    }
    return false;
}
  
// Loading testcases into array
const testStrings = [ "594ced02ed345b2b049222c5","geeks",  
                      "toptoptoptop","geeksfogeeks"];
  
// Validating each test case
for(const testcase of testStrings){
  
    if(isValidObjectId(testcase))
        console.log(testcase + " is a valid MongodbID");
    else
        console.log(testcase + " is not a valid MongodbID");
  
}

结果将是:

String ID                |   ObjectId.isValid(id)  |    Expected Validation               
---------------------------------------------------------------------------
594ced02ed345b2b049222c5 |  true                   |  true
dania                    |  false                  |  false
toptoptoptop             |  false                  |  false
daniandanian             |  false                  |  false

isValid()isValidObjectId() returns true 即使字符串不是 ObjectId。根据docs

Returns true if Mongoose can cast the given value to an ObjectId, or false otherwise.

如果你想检查是否是一个真正的 ObjectId,你可以使用这个正则表达式:/^[a-f\d]{24}$/i

此处示例:

var regex = new RegExp(/^[a-f\d]{24}$/i);
const ids = ['1', 'mohamma22145', '5a934e000102030405000000', 'FFFFFFFFFFFFFFFFFFFFFFFF']
ids.forEach(id => console.log(`${id} is an ObjectId: ${regex.test(id)}`))