Search code examples
scalacasbah

Casbah - check if string is parseable as ObjectId


Is there a simple built in way to check if a string is a valid ObjectId? I know if I try to construct an ObjectId from an invalid string it throws an exception, is there a way to circumvent the try-catch block?

One possible way would be to check against this regex: ^[0-9a-b]{48}$ Is there something more elegant?


Solution

  • There is method ObjectId.isValid(String s): docs