Registry / database / joi-mongodb-objectid

joi-mongodb-objectid

JSON →
library0.1.0jsnpmunverified

Joi extension to validate MongoDB ObjectId. Version 0.1.0 is stable, released in 2018, with no further updates. Provides a single `.objectId()` Joi type that validates BSON ObjectId instances. Depends on `joi >= 13` and `bson`. Minimal alternative to custom validators in Joi schemas for MongoDB applications.

npm install joi-mongodb-objectid
INSTALL
IMPORT
SIG · JOI-MONGODB-OBJECT
J
joi-mongodb-objectid
databasejavascriptv0.1.0
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-mongodb-objectid
const JoiObjectId = require('joi-mongodb-objectid')
const { JoiObjectId } = require('joi-mongodb-objectid')
CommonJS default export. No named export.
ObjectId
const ObjectId = require('bson').ObjectId
const ObjectId = require('mongodb').ObjectId
ObjectId comes from 'bson', not 'mongodb' driver.
objectId()
Joi.objectId().validate(someValue)
Joi.objectId(someValue).validate()
objectId() is a Joi type (chainable).

Shows how to extend Joi with the ObjectId type and validate a valid and invalid ObjectId.

const { ObjectId } = require('bson'); const JoiBase = require('joi'); const JoiObjectId = require('joi-mongodb-objectid'); const Joi = JoiBase.extend(JoiObjectId); const schema = Joi.objectId(); const validId = ObjectId(); const invalidId = '507f1f77bcf86cd799439011'; console.log(schema.validate(validId).error); // null console.log(schema.validate(invalidId).error); // ValidationError
Debug
Known issues
gotchaThe package is not updated to work with Joi v17+; it may have compatibility issues.
fix
Use an updated fork or implement custom validation.
affects: >=0.1.0
deprecatedrequire('joi') syntax; Joi v17 dropped support for legacy extensions API.
fix
Use 'joi@16' or adopt Joi v17 with new extension format.
affects: >=0.1.0
gotchaobjectId() only validates BSON ObjectId instances, not hex strings.
fix
Convert hex strings to ObjectId before validation or use a different validator.
affects: >=0.1.0
Errors
Common errors & fixes
Error: Unknown helper: "objectId"
Forgot to extend Joi base with JoiObjectId.
fix
const Joi = require('joi').extend(require('joi-mongodb-objectid'));
ValidationError: "value" must be an ObjectId
Passed a hex string instead of ObjectId instance.
fix
const objectId = new (require('bson').ObjectId)(hexString); then validate.
Cannot find module 'bson'
Missing dependency bson.
fix
npm install bson
Upgrade
Version history
0.1.0latest on npm
Audit
Dependencies
joirequiredPeer dependency: >=13 is required for Joi extension system.
bsonrequiredRuntime dependency for ObjectId class.
Agent activity
4 hits · last 30 days
node
4
Resources
joi-mongodb-objectid — npm install joi-mongodb-objectid · libregistry