laci37
laci37

Reputation: 490

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?

Upvotes: 0

Views: 441

Answers (1)

Infinity
Infinity

Reputation: 3441

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

Upvotes: 2

Related Questions