Registry / database / joi-objectid

joi-objectid

JSON →
library4.0.2jsnpmunverified

A Joi extension for validating MongoDB ObjectId strings. Version 4.0.2 works with Joi v17+ (Hapi ecosystem). Validates that a value is a 24-character hex string. Lightweight, zero extra dependencies beyond Joi. Not needed for mongoose schemas but useful for plain Joi validation.

npm install joi-objectid
INSTALL
IMPORT
SIG · JOI-OBJECTID
J
joi-objectid
databasejavascriptv4.0.2
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Joi.objectId()
const Joi = require('@hapi/joi'); const objectId = require('joi-objectid')(Joi); Joi.objectId = objectId;
const Joi = require('joi-objectid');
The package exports a function that must be called with the Joi instance to extend it.
joi-objectid module
import objectId from 'joi-objectid'; const Joi = require('@hapi/joi'); Joi.objectId = objectId(Joi);
import JoiObjectId from 'joi-objectid';
ESM import works but the default is a function; must be invoked with Joi.
Joi.objectId type declaration
declare module '@hapi/joi' { interface StringSchema { objectId(): this; } }
// no direct wrong, but types must be augmented manually
No bundled TypeScript types; users need to augment Joi's types.

Shows how to extend Joi with the ObjectId validator and use it in a schema.

const Joi = require('@hapi/joi'); const objectId = require('joi-objectid')(Joi); Joi.objectId = objectId; const schema = Joi.object({ id: Joi.objectId(), name: Joi.string().max(100), date: Joi.date() }); const result = schema.validate({ id: '507f1f77bcf86cd799439011', name: 'test', date: new Date() }); console.log(result.error ? result.error.details : 'Valid');
Debug
Known issues
breakingVersion 4.0.0+ requires Joi v17+ (Hapi ecosystem). Does not work with Joi v16 or earlier.
fix
Use @hapi/joi v17+ or switch to joi-objectid v3.x for Joi v16.
affects: >=4.0.0
gotchaThe module must be invoked with a Joi instance; forgetting to call the exported function will cause 'objectId is not a function' error.
fix
Always call require('joi-objectid')(Joi) and assign Joi.objectId.
affects: >=1.0.0
deprecatedUsage with 'joi' (npm package, not @hapi/joi) is deprecated. Hapi v19 onwards uses @hapi/joi.
fix
Switch to import from @hapi/joi.
affects: >=4.0.0
gotchaThe validator only accepts a 24-character hex string. Does not validate if the ObjectId is a valid BSON type.
fix
This is by design; use mongodb's ObjectID constructor for deeper validation.
affects: >=1.0.0
gotchaNo built-in TypeScript support; Joi.ObjectId type augmentation is required for TS users.
fix
Add a declaration file that augments @hapi/joi's StringSchema.
affects: >=4.0.0
deprecatedJoi.objectId with hyphen (Joi.object-id) was a typo in earlier docs; use Joi.objectId.
fix
Use Joi.objectId() (no hyphen).
affects: <4.0.0
Errors
Common errors & fixes
TypeError: Joi.objectId is not a function
Not calling the exported function with Joi instance, or using wrong version of Joi.
fix
Ensure you call require('joi-objectid')(Joi) and assign to Joi.objectId, and use @hapi/joi v17+.
ValidationError: "value" must be a valid ObjectId
Value is not a 24-character hex string.
fix
Ensure the value is exactly 24 hex characters [0-9a-fA-F].
Cannot find module '@hapi/joi'
Joi is not installed or wrong package name used.
fix
Install @hapi/joi: npm install @hapi/joi
joi-objectid: Joi must be provided
Exported function called without arguments.
fix
Pass the Joi instance: const objectId = require('joi-objectid')(Joi);
Upgrade
Version history
4.0.2latest on npm
Audit
Dependencies
@hapi/joirequiredpeer dependency; extension is built for Hapi's Joi fork (v17+)
Agent activity
4 hits · last 30 days
node
4
Resources
joi-objectid — npm install joi-objectid · libregistry