mongodbjoiobjectid

How to validate for ObjectID


Using Joi schema validation, is it possible to validate against MongoDB ObjectID's?

Something like this could be great:

_id: Joi.ObjectId().required().error(errorParser),

Solution

  • const Joi = require('@hapi/joi')
    Joi.objectId = require('joi-objectid')(Joi)
     
    const schema = Joi.object({
      id: Joi.objectId(),
      name: Joi.string().max(100),
      date: Joi.date()
    })
    

    checkout https://www.npmjs.com/package/joi-objectid