Mongoose ObjectID Validator

JS
S
JavaScript

Simple snippet to validate if a token is a mongoose object id or not.

1if (id.match(/^[0-9a-fA-F]{24}$/)) {
2  // Yes, it's a valid ObjectId, proceed with `findById` call.
3}

Created on 7/6/2018