mongodb isValid function returns true for invalid ObjectIDs

hrp8sfH4xQ4

so I have to check if a given input is a valid ObjectID and if that returns true I want to transform the value to a ObjectID. Unfortunately the validation returns true for "zzzzzzzzzzzz" and throws an exception while transforming the value.

I get the following error

Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters

I tried to reproduce the problem:

const { ObjectID } = require("mongodb");

const value = "zzzzzzzzzzzz";
const isValid = ObjectID.isValid(value);

if (!isValid) { // this is true
  throw new Error("invalid ID");
}

ObjectID.createFromHexString(value); // throws exception

So why does the validation return true then? How can I fix that? My real project is a NestJs application so it would be nice to cast a value of type any to a typesafe ObjectID.

Kelsnare

So why does the validation return true then?

Because the code. Line 338

Basically any valid input to the ObjectID constructor will return true with ObjectID.isValid()

Hence, for your case ObjectID.isValid("zzzzzzzzzzzz") is true because new ObjectID("zzzzzzzzzzzz") is a valid operation.

More from an old documentation of node-mongodb-native driver

how about this:

const { ObjectID } = require("mongodb");
function isValidObjectID(id) {
  try{
    ObjectID.createFromHexString(id)
  }catch(e){
    return false
  }
  return true
}

console.log(`zzzzzzzzzzzz
isValid: ${isValidObjectID("zzzzzzzzzzzz")}`) // false

console.log(`5beec2f547643846af1d4dc3
isValid: ${isValidObjectID("5beec2f547643846af1d4dc3")}`) // true

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Testing a REST endpoint with Spring, MongoDB using ObjectIds

@Value boolean returns 'invalid boolean value' of value true

Using UUIDs instead of ObjectIDs in MongoDB

Check if function returns true to execute another function

ASP.NET 5, MVC6, WebAPI -> ModelState.IsValid always returns true

Javascript Function that returns true if a letter?

ASP.NET and EF Core - ModelState.IsValid always returns true for models generated by Scaffold-DbContext

Json.NET Schema IsValid returns true even if there is different type

get function returns invalid value

ggsurvplot_facet returns: "Error in grDevices::col2rgb(colour, TRUE) : invalid color name" when used inside a function

ModelState.IsValid is true, but why?

Powershell function returns an invalid object

The function returns invalid values

Unit testing Mvc.Compare Attribute incorrectly returns model isValid = true

Knockout validation plugin - isValid( ) always returns true

isset() function always returns true

OpenProcess function returns invalid handles

printDialog.PrinterSettings.IsValid returns true always

Which type to store an Array of MongoDB ObjectIds?

why does Powershell Test-Path -Isvalid always returns true

Using MongoDB ObjectIds with lodash

Function returns invalid literal for int() with base 10: ''

isValid() function from moment.js library returns false for something that should be true

Email validation using jquery returns true even for invalid email

MongoDB aggregate match by array of ObjectIds

Vaadin-flow non-critical validation that still returns isValid=true

ModelState.IsValid show true for invalid entry on Test

mongoDB listCollections() returns empty if nameOnly is set to true

Why PHP function returns TRUE?