Skip to content

Commit 61d01fa

Browse files
committed
fix(mongoose): make isValidObjectId() consistent with isValid(), make ObjectId() casting more flexible
Re: Automattic#11209
1 parent b72f061 commit 61d01fa

File tree

3 files changed

+10
-39
lines changed

3 files changed

+10
-39
lines changed

lib/cast/objectid.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22

33
const ObjectId = require('../driver').get().ObjectId;
4-
const assert = require('assert');
54

65
module.exports = function castObjectId(value) {
76
if (value == null) {
@@ -25,5 +24,5 @@ module.exports = function castObjectId(value) {
2524
return new ObjectId(value.toString());
2625
}
2726

28-
assert.ok(false);
27+
return new ObjectId(value);
2928
};

lib/index.js

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -947,50 +947,21 @@ Mongoose.prototype.ObjectId = SchemaTypes.ObjectId;
947947
*
948948
* mongoose.isValidObjectId(new mongoose.Types.ObjectId()); // true
949949
* mongoose.isValidObjectId('0123456789ab'); // true
950-
* mongoose.isValidObjectId(6); // false
950+
* mongoose.isValidObjectId(6); // true
951+
* mongoose.isValidObjectId({ test: 42 }); // false
951952
*
952953
* @method isValidObjectId
953954
* @api public
954955
*/
955956

956957
Mongoose.prototype.isValidObjectId = function(v) {
957-
if (v == null) {
958-
return true;
959-
}
960-
const base = this || mongoose;
961-
const ObjectId = base.driver.get().ObjectId;
962-
if (v instanceof ObjectId) {
963-
return true;
964-
}
965-
966-
if (v._id != null) {
967-
if (v._id instanceof ObjectId) {
968-
return true;
969-
}
970-
if (v._id.toString instanceof Function) {
971-
v = v._id.toString();
972-
if (typeof v === 'string' && v.length === 12) {
973-
return true;
974-
}
975-
if (typeof v === 'string' && /^[0-9A-Fa-f]{24}$/.test(v)) {
976-
return true;
977-
}
978-
return false;
979-
}
980-
}
981-
982-
if (v.toString instanceof Function) {
983-
v = v.toString();
984-
}
985-
986-
if (typeof v === 'string' && v.length === 12) {
987-
return true;
988-
}
989-
if (typeof v === 'string' && /^[0-9A-Fa-f]{24}$/.test(v)) {
990-
return true;
958+
try {
959+
new this.Types.ObjectId(v);
960+
} catch (err) {
961+
return false;
991962
}
992963

993-
return false;
964+
return true;
994965
};
995966

996967
Mongoose.prototype.syncIndexes = function() {

test/index.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,8 @@ describe('mongoose module:', function() {
699699
assert.ok(mongoose.isValidObjectId('5f5c2d56f6e911019ec2acdc'));
700700
assert.ok(mongoose.isValidObjectId('608DE01F32B6A93BBA314159'));
701701
assert.ok(mongoose.isValidObjectId(new mongoose.Types.ObjectId()));
702-
assert.ok(!mongoose.isValidObjectId(6));
702+
assert.ok(mongoose.isValidObjectId(6));
703+
assert.ok(!mongoose.isValidObjectId({ test: 42 }));
703704
});
704705
it('global `strictPopulate` works when false (gh-10694)', async function() {
705706
const mongoose = new Mongoose();

0 commit comments

Comments
 (0)