File tree Expand file tree Collapse file tree 1 file changed +11
-1
lines changed Expand file tree Collapse file tree 1 file changed +11
-1
lines changed Original file line number Diff line number Diff line change @@ -112,7 +112,7 @@ export class ObjectId extends BSONValue {
112112 // If intstanceof matches we can escape calling ensure buffer in Node.js environments
113113 this . buffer = ByteUtils . toLocalBufferType ( workingId ) ;
114114 } else if ( typeof workingId === 'string' ) {
115- if ( workingId . length === 24 && checkForHexRegExp . test ( workingId ) ) {
115+ if ( ObjectId . validateHexString ( workingId ) ) {
116116 this . buffer = ByteUtils . fromHex ( workingId ) ;
117117 } else {
118118 throw new BSONError (
@@ -143,6 +143,15 @@ export class ObjectId extends BSONValue {
143143 }
144144 }
145145
146+ /**
147+ * @internal
148+ * Validates the input string is a valid hex representation of an ObjectId.
149+ */
150+ private static validateHexString ( input : string ) : boolean {
151+ if ( input == null || input . length !== 24 ) return false ;
152+ return checkForHexRegExp . test ( input ) ;
153+ }
154+
146155 /** Returns the ObjectId id as a 24 lowercase character hex string representation */
147156 toHexString ( ) : string {
148157 if ( ObjectId . cacheHexString && this . __id ) {
@@ -329,6 +338,7 @@ export class ObjectId extends BSONValue {
329338 */
330339 static isValid ( id : string | number | ObjectId | ObjectIdLike | Uint8Array ) : boolean {
331340 if ( id == null ) return false ;
341+ if ( typeof id === 'string' ) return ObjectId . validateHexString ( id ) ;
332342
333343 try {
334344 new ObjectId ( id ) ;
You can’t perform that action at this time.
0 commit comments