Registry / database / nestjs-object-id

nestjs-object-id

JSON →
library4.0.4jsnpmunverified

nestjs-object-id (v4.0.4) provides MongoDB ObjectId validation and parsing for NestJS applications, supporting REST APIs, GraphQL, DTOs, and Microservices. It includes pipes (IsObjectIdPipe, ParseObjectIdPipe) and decorators (@IsObjectId, @ParseObjectId) that integrate seamlessly with NestJS's validation pipeline. Requires @nestjs/common ^10 || ^11, class-validator ^0.14.1, class-transformer ^0.5.1, and mongoose ^7 || ^8 || ^9. Maintained with regular updates; the latest version requires @nestjs/common >=10.2.7 for pipes to work correctly.

npm install nestjs-object-id
INSTALL
IMPORT
SIG · NESTJS-OBJECT-ID
N
nestjs-object-id
databasejavascriptv4.0.4
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.

IsObjectIdPipe
✓ import { IsObjectIdPipe } from 'nestjs-object-id';
✗ const IsObjectIdPipe = require('nestjs-object-id').IsObjectIdPipe;
ESM import. CommonJS require is also supported but not recommended for NestJS projects using TypeScript.
ParseObjectIdPipe
✓ import { ParseObjectIdPipe } from 'nestjs-object-id';
✗ import { ParseObjectIdPipe } from 'nestjs-object-id/pipes';
Named export from the main package. No subpath exports.
IsObjectId
✓ import { IsObjectId } from 'nestjs-object-id';
✗ import { IsObjectId } from 'nestjs-object-id/decorators';
Decorator is exported from the package root. Works with class-validator.
ParseObjectId
✓ import { ParseObjectId } from 'nestjs-object-id';
✗ import { ParseObjectId } from 'class-transformer';
Custom decorator for transforming strings to ObjectId instances.
Types
✓ import { Types } from 'mongoose';
✗ import { Types } from 'nestjs-object-id';
Types.ObjectId is from mongoose, not from this package.

Shows usage of IsObjectIdPipe and ParseObjectIdPipe for validating and parsing MongoDB ObjectIds in NestJS route parameters.

import { IsObjectIdPipe, ParseObjectIdPipe } from 'nestjs-object-id'; import { Types } from 'mongoose'; @Controller('items') export class ItemsController { // Validate route param as ObjectId @Get(':id') getItem(@Param('id', IsObjectIdPipe) id: string) { return this.itemsService.findById(id); } // Directly parse to Types.ObjectId @Get(':id/details') getDetails(@Param('id', ParseObjectIdPipe) id: Types.ObjectId) { return this.itemsService.findById(id.toString()); } }
Debug
Known issues
breakingRequires @nestjs/common >=10.2.7 for pipe support.
fix
Update @nestjs/common to version 10.2.7 or higher.
affects: <4.0.0
breakingPeer dependency mongoose version updated from ^6 to ^7 in v4 - ensure migration.
fix
Upgrade mongoose to version 7.x or higher.
affects: >=4.0.0
deprecatedOlder versions used 'IsObjectIdPipe' from subpath 'nestjs-object-id/pipes' - this is no longer supported.
fix
Import both pipes and decorators from the main package: 'nestjs-object-id'.
affects: <4.0.0
gotchaWhen using ParseObjectIdPipe, the transformed parameter is of type Types.ObjectId, not string.
fix
Ensure your service methods accept or convert Types.ObjectId as needed.
affects: >=3.0.0
gotchaThe @IsObjectId decorator from this package overrides class-validator's custom validation - ensure it's listed after other decorators.
fix
Place @IsObjectId() after other class-validator decorators like @IsString() to avoid validation order issues.
affects: >=1.0.0
Errors
Common errors & fixes
Cannot find module 'nestjs-object-id/pipes' or its corresponding type declarations.
Importing from a subpath that doesn't exist in the package.
fix
Import from the package root: import { IsObjectIdPipe } from 'nestjs-object-id';
UnhandledPromiseRejectionWarning: BadRequestException: Invalid ObjectId
Pipe validation failed for a route parameter that is not a valid MongoDB ObjectId.
fix
Ensure the route parameter is a 24-character hex string or a valid ObjectId string.
Type 'string' is not assignable to type 'ObjectId'.
Using ParseObjectIdPipe but the parameter type is declared as string instead of Types.ObjectId.
fix
Change the parameter type to Types.ObjectId or use IsObjectIdPipe to keep it as string.
Validation failed (expected type is ObjectId)
The @IsObjectId decorator is rejecting a valid ObjectId due to incorrect format (e.g., missing quotes in DTO).
fix
Ensure the value is a string representation of an ObjectId (24 hex characters).
Upgrade
Version history
4.0.4latest on npm
Audit
Dependencies
@nestjs/commonrequiredCore NestJS framework dependency; pipes require @nestjs/common for parameter decorators
class-validatorrequiredRequired for @IsObjectId decorator to work with DTO validation
class-transformerrequiredRequired for @ParseObjectId decorator to transform strings to ObjectId instances
mongooserequiredRequired for Types.ObjectId type and ObjectId validation logic
Agent activity
10 hits · last 30 days
node
8
Resources
nestjs-object-id — npm install nestjs-object-id · libregistry