Check Valid MongoDb _id?

What is the best way to check valid mongodb id, Currenlty i am using BSON.ObjectId.decode!(id) to decode string mongo db like 590ffa5c421aa91ece8bec01 to #BSON.ObjectId<590ffa5c421aa91ece8bec01>, but when i am trying to pass invalid id i am getting FunctionClauseError, so what is the best way to way to check?

By using regex ~r/[0-9a-f]{24}/ we can pattern match and check valid id

There are some points to be made:

  • this doesn’t have anything to do with Phoenix, it’s related to the library you’re using.
  • when asking something related to a library, please, name it on the post. people won’t guess what lib are you talking about.
  • remember that people won’t have the context of your application and I’m quite sure they won’t be able to read your mind to know it. It would be really nice if you first explain your context before ask something.

Actually the problem is that the library you’re using only implement decode!, following the Elixir expected behavior it would be nice to ask for the maintainer of the lib for a decode function, or you can contribute and do it. :smiley:

2 Likes