File tree Expand file tree Collapse file tree 3 files changed +10
-39
lines changed Expand file tree Collapse file tree 3 files changed +10
-39
lines changed Original file line number Diff line number Diff line change 1
1
'use strict' ;
2
2
3
3
const ObjectId = require ( '../driver' ) . get ( ) . ObjectId ;
4
- const assert = require ( 'assert' ) ;
5
4
6
5
module . exports = function castObjectId ( value ) {
7
6
if ( value == null ) {
@@ -25,5 +24,5 @@ module.exports = function castObjectId(value) {
25
24
return new ObjectId ( value . toString ( ) ) ;
26
25
}
27
26
28
- assert . ok ( false ) ;
27
+ return new ObjectId ( value ) ;
29
28
} ;
Original file line number Diff line number Diff line change @@ -947,50 +947,21 @@ Mongoose.prototype.ObjectId = SchemaTypes.ObjectId;
947
947
*
948
948
* mongoose.isValidObjectId(new mongoose.Types.ObjectId()); // true
949
949
* mongoose.isValidObjectId('0123456789ab'); // true
950
- * mongoose.isValidObjectId(6); // false
950
+ * mongoose.isValidObjectId(6); // true
951
+ * mongoose.isValidObjectId({ test: 42 }); // false
951
952
*
952
953
* @method isValidObjectId
953
954
* @api public
954
955
*/
955
956
956
957
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 - 9 A - F a - 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 - 9 A - F a - f ] { 24 } $ / . test ( v ) ) {
990
- return true ;
958
+ try {
959
+ new this . Types . ObjectId ( v ) ;
960
+ } catch ( err ) {
961
+ return false ;
991
962
}
992
963
993
- return false ;
964
+ return true ;
994
965
} ;
995
966
996
967
Mongoose . prototype . syncIndexes = function ( ) {
Original file line number Diff line number Diff line change @@ -699,7 +699,8 @@ describe('mongoose module:', function() {
699
699
assert . ok ( mongoose . isValidObjectId ( '5f5c2d56f6e911019ec2acdc' ) ) ;
700
700
assert . ok ( mongoose . isValidObjectId ( '608DE01F32B6A93BBA314159' ) ) ;
701
701
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 } ) ) ;
703
704
} ) ;
704
705
it ( 'global `strictPopulate` works when false (gh-10694)' , async function ( ) {
705
706
const mongoose = new Mongoose ( ) ;
You can’t perform that action at this time.
0 commit comments