npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

joi-objectid-ext

v1.0.2

Published

A Joi extension for validating MongoDB ObjectIDs

Readme

joi-objectid-ext

A Joi extension for validating MongoDB ObjectIDs in your validation schemas.

Features

  • Validates ObjectID strings (24 character hex strings)
  • Works with actual MongoDB ObjectId instances
  • Compatible with multiple ObjectId implementations:
    • MongoDB native driver (mongodb)
    • BSON package (bson)
    • Mongoose ObjectIds (mongoose.Types.ObjectId)
  • TypeScript support
  • Zero dependencies
  • Simple API

Installation

npm install joi-objectid-ext --save
# or
yarn add joi-objectid-ext

Usage

Basic usage

import Joi from "joi";
import { extendJoiWithObjectId } from "joi-objectid-ext";

// Extend Joi with the ObjectId type
const ExtendedJoi = extendJoiWithObjectId(Joi);

// Create a schema with ObjectId validation
const schema = ExtendedJoi.object({
  id: ExtendedJoi.objectId().required(),
  optionalId: ExtendedJoi.objectId(),
});

// Validate a string ObjectId
const result1 = schema.validate({
  id: "507f191e810c19729de860ea",
  optionalId: "507f191e810c19729de860eb",
});
// result1.error is undefined (validation passes)

// Validate with invalid ObjectId
const result2 = schema.validate({
  id: "not-an-objectid",
});
// result2.error contains validation error

Using with MongoDB's ObjectId

import Joi from "joi";
import { ObjectId } from "mongodb";
import { extendJoiWithObjectId } from "joi-objectid-ext";

const ExtendedJoi = extendJoiWithObjectId(Joi);

const schema = ExtendedJoi.object({
  id: ExtendedJoi.objectId().required(),
});

// Create a MongoDB ObjectId instance
const objectId = new ObjectId("507f191e810c19729de860ea");

// Validate the ObjectId instance
const result = schema.validate({ id: objectId });
// result.error is undefined (validation passes)

Using with Mongoose

import Joi from "joi";
import mongoose from "mongoose";
import { extendJoiWithObjectId } from "joi-objectid-ext";

const ExtendedJoi = extendJoiWithObjectId(Joi);

const schema = ExtendedJoi.object({
  id: ExtendedJoi.objectId().required(),
});

// Create a Mongoose ObjectId
const objectId = new mongoose.Types.ObjectId("507f191e810c19729de860ea");

// Validate the Mongoose ObjectId
const result = schema.validate({ id: objectId });
// result.error is undefined (validation passes)

Using with BSON package

import Joi from "joi";
import { ObjectId } from "bson";
import { extendJoiWithObjectId } from "joi-objectid-ext";

const ExtendedJoi = extendJoiWithObjectId(Joi);

const schema = ExtendedJoi.object({
  id: ExtendedJoi.objectId().required(),
});

// Create a BSON ObjectId
const objectId = new ObjectId("507f191e810c19729de860ea");

// Validate the BSON ObjectId
const result = schema.validate({ id: objectId });
// result.error is undefined (validation passes)

API

extendJoiWithObjectId(joi)

Extends a Joi instance with the ObjectId extension.

Parameters:

  • joi: Joi module to extend

Returns: Extended Joi with ObjectId validation support

Joi.objectId()

Creates a Joi schema that validates ObjectIds.

  • Validates strings of exactly 24 hex characters
  • Works with MongoDB ObjectId instances, Mongoose ObjectIds, and BSON package ObjectIds

Compatibility

  • Works with Joi v17+
  • Compatible with Node.js 12+
  • TypeScript 4.0+

License

MIT

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.