0

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?

laci37
  • 510
  • 4
  • 17

1 Answers1

2

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

Infinity
  • 3,431
  • 3
  • 25
  • 46
  • I thought that maybe Casbah somewhere had a method that would yield an Option[ObjectId] but it seems that I will have to implement it. – laci37 Jul 08 '13 at 09:50