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());
}
}
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.
fixImport 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.
fixEnsure 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.
fixChange 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).
fixEnsure the value is a string representation of an ObjectId (24 hex characters).
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