File tree Expand file tree Collapse file tree 3 files changed +20
-2
lines changed Expand file tree Collapse file tree 3 files changed +20
-2
lines changed Original file line number Diff line number Diff line change @@ -298,7 +298,12 @@ export class ObjectId {
298
298
}
299
299
300
300
if ( typeof id === 'string' ) {
301
- return id . length === 12 || ( id . length === 24 && checkForHexRegExp . test ( id ) ) ;
301
+ if ( id . length === 12 ) {
302
+ const bytes = Buffer . from ( id ) ;
303
+ return bytes . byteLength === 12 ;
304
+ } else if ( id . length === 24 ) {
305
+ return checkForHexRegExp . test ( id ) ;
306
+ }
302
307
}
303
308
304
309
if ( id instanceof ObjectId ) {
Original file line number Diff line number Diff line change 2
2
--require chai/register-expect
3
3
--require source-map-support/register
4
4
--timeout 10000
5
- --throw-deprecation
Original file line number Diff line number Diff line change @@ -269,6 +269,20 @@ describe('ObjectId', function () {
269
269
) ;
270
270
} ) ;
271
271
272
+ it ( 'should have isValid be true for 12-char length and 12-byte length string' , function ( ) {
273
+ const plainASCIIstr = 'aaaaaaaaaaaa' ;
274
+ expect ( ObjectId . isValid ( plainASCIIstr ) ) . to . be . true ;
275
+ } ) ;
276
+
277
+ it ( 'should have isValid be false for 12-char length but non-12-byte length string' , function ( ) {
278
+ const characterCodesLargerThan256 = 'abcdefŽhijkl' ;
279
+ const length12Not12Bytest1 = '🐶🐶🐶🐶🐶🐶' ;
280
+ const length12Not12Bytest2 = 'value with é' ;
281
+ expect ( ObjectId . isValid ( characterCodesLargerThan256 ) ) . to . be . false ;
282
+ expect ( ObjectId . isValid ( length12Not12Bytest1 ) ) . to . be . false ;
283
+ expect ( ObjectId . isValid ( length12Not12Bytest2 ) ) . to . be . false ;
284
+ } ) ;
285
+
272
286
it ( 'should correctly interpret timestamps beyond 2038' , function ( ) {
273
287
const farFuture = new Date ( '2040-01-01T00:00:00.000Z' ) . getTime ( ) ;
274
288
expect (
You can’t perform that action at this time.
0 commit comments