{"id":42896,"library":"joi-objectid","title":"joi-objectid","description":"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.","status":"active","version":"4.0.2","language":"javascript","source_language":"en","source_url":"https://github.com/mkg20001/joi-objectid","tags":["javascript"],"install":[{"cmd":"npm install joi-objectid","lang":"bash","label":"npm"},{"cmd":"yarn add joi-objectid","lang":"bash","label":"yarn"},{"cmd":"pnpm add joi-objectid","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"peer dependency; extension is built for Hapi's Joi fork (v17+)","package":"@hapi/joi","optional":false}],"imports":[{"note":"The package exports a function that must be called with the Joi instance to extend it.","wrong":"const Joi = require('joi-objectid');","symbol":"Joi.objectId()","correct":"const Joi = require('@hapi/joi'); const objectId = require('joi-objectid')(Joi); Joi.objectId = objectId;"},{"note":"ESM import works but the default is a function; must be invoked with Joi.","wrong":"import JoiObjectId from 'joi-objectid';","symbol":"joi-objectid module","correct":"import objectId from 'joi-objectid'; const Joi = require('@hapi/joi'); Joi.objectId = objectId(Joi);"},{"note":"No bundled TypeScript types; users need to augment Joi's types.","wrong":"// no direct wrong, but types must be augmented manually","symbol":"Joi.objectId type declaration","correct":"declare module '@hapi/joi' { interface StringSchema { objectId(): this; } }"}],"quickstart":{"code":"const Joi = require('@hapi/joi');\nconst objectId = require('joi-objectid')(Joi);\n\nJoi.objectId = objectId;\n\nconst schema = Joi.object({\n  id: Joi.objectId(),\n  name: Joi.string().max(100),\n  date: Joi.date()\n});\n\nconst result = schema.validate({ id: '507f1f77bcf86cd799439011', name: 'test', date: new Date() });\nconsole.log(result.error ? result.error.details : 'Valid');","lang":"javascript","description":"Shows how to extend Joi with the ObjectId validator and use it in a schema."},"warnings":[{"fix":"Use @hapi/joi v17+ or switch to joi-objectid v3.x for Joi v16.","message":"Version 4.0.0+ requires Joi v17+ (Hapi ecosystem). Does not work with Joi v16 or earlier.","severity":"breaking","affected_versions":">=4.0.0"},{"fix":"Always call require('joi-objectid')(Joi) and assign Joi.objectId.","message":"The module must be invoked with a Joi instance; forgetting to call the exported function will cause 'objectId is not a function' error.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Switch to import from @hapi/joi.","message":"Usage with 'joi' (npm package, not @hapi/joi) is deprecated. Hapi v19 onwards uses @hapi/joi.","severity":"deprecated","affected_versions":">=4.0.0"},{"fix":"This is by design; use mongodb's ObjectID constructor for deeper validation.","message":"The validator only accepts a 24-character hex string. Does not validate if the ObjectId is a valid BSON type.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Add a declaration file that augments @hapi/joi's StringSchema.","message":"No built-in TypeScript support; Joi.ObjectId type augmentation is required for TS users.","severity":"gotcha","affected_versions":">=4.0.0"},{"fix":"Use Joi.objectId() (no hyphen).","message":"Joi.objectId with hyphen (Joi.object-id) was a typo in earlier docs; use Joi.objectId.","severity":"deprecated","affected_versions":"<4.0.0"}],"env_vars":null,"search_vec":"'24':26 '4.0.2':13 'beyond':34 'charact':27 'depend':33 'ecosystem':19 'extens':6 'extra':32 'hapi':18 'hex':28 'javascript':47 'joi':2,5,16,35,45 'joi-objectid':1 'lightweight':30 'mongodb':9 'mongoos':39 'need':37 'objectid':3,10 'plain':44 'schema':40 'string':11,29 'use':42 'v17':17 'valid':8,20,46 'valu':23 'version':12 'work':14 'zero':31","created_at":"2026-06-05T16:57:11.351218+00:00","updated_at":"2026-06-05T16:57:11.351218+00:00","problems":[{"fix":"Ensure you call require('joi-objectid')(Joi) and assign to Joi.objectId, and use @hapi/joi v17+.","cause":"Not calling the exported function with Joi instance, or using wrong version of Joi.","error":"TypeError: Joi.objectId is not a function"},{"fix":"Ensure the value is exactly 24 hex characters [0-9a-fA-F].","cause":"Value is not a 24-character hex string.","error":"ValidationError: \"value\" must be a valid ObjectId"},{"fix":"Install @hapi/joi: npm install @hapi/joi","cause":"Joi is not installed or wrong package name used.","error":"Cannot find module '@hapi/joi'"},{"fix":"Pass the Joi instance: const objectId = require('joi-objectid')(Joi);","cause":"Exported function called without arguments.","error":"joi-objectid: Joi must be provided"}],"ecosystem":"npm","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":null,"cli_version":null,"type":"library","homepage":"https://github.com/mkg20001/joi-objectid#readme","github":"https://github.com/mkg20001/joi-objectid","docs":null,"changelog":null,"pypi":null,"npm":"joi-objectid","openapi_spec":null,"status_page":null,"smithery":null,"categories":["database"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-05","next_check":"2026-09-03","install_tag":null}}