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 {
112
112
// If intstanceof matches we can escape calling ensure buffer in Node.js environments
113
113
this . buffer = ByteUtils . toLocalBufferType ( workingId ) ;
114
114
} else if ( typeof workingId === 'string' ) {
115
- if ( workingId . length === 24 && checkForHexRegExp . test ( workingId ) ) {
115
+ if ( ObjectId . validateHexString ( workingId ) ) {
116
116
this . buffer = ByteUtils . fromHex ( workingId ) ;
117
117
} else {
118
118
throw new BSONError (
@@ -143,6 +143,15 @@ export class ObjectId extends BSONValue {
143
143
}
144
144
}
145
145
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
+
146
155
/** Returns the ObjectId id as a 24 lowercase character hex string representation */
147
156
toHexString ( ) : string {
148
157
if ( ObjectId . cacheHexString && this . __id ) {
@@ -329,6 +338,7 @@ export class ObjectId extends BSONValue {
329
338
*/
330
339
static isValid ( id : string | number | ObjectId | ObjectIdLike | Uint8Array ) : boolean {
331
340
if ( id == null ) return false ;
341
+ if ( typeof id === 'string' ) return ObjectId . validateHexString ( id ) ;
332
342
333
343
try {
334
344
new ObjectId ( id ) ;
You can’t perform that action at this time.
0 commit comments