This package provides an efficient way to validate and parse ObjectIds for NestJS applications that interact with MongoDB and supports a wide range of architectures and patterns, including REST APIs, GraphQL, DTOs and Microservices.
npm install nestjs-object-id
IsObjectIdPipe is a pipe for validating MongoDB ObjectIds in route parameters.
import { IsObjectIdPipe } from 'nestjs-object-id';
@Controller('posts')
export class PostsController {
constructor(private readonly postsService: PostsService) {}
@Get(':id')
findOne(@Param('id', IsObjectIdPipe) id: string) {
return this.postsService.findOne(id);
}
}
WARNING To work with Pipes correctly, make sure you have @nestjs/common@10.2.7 or higher installed.
If an invalid 'id' is received, an error will be thrown:
{
"message": "Invalid ObjectId",
"error": "Bad Request",
"statusCode": 400
}
ParseObjectIdPipe extends the functionality of IsObjectIdPipe by converting string parameters into Types.ObjectId instances.
import { ParseObjectIdPipe } from 'nestjs-object-id';
import { Types } from 'mongoose';
@Controller('posts')
export class PostsController {
constructor(private readonly postsService: PostsService) {}
@Get(':id')
findOne(@Param('id', ParseObjectIdPipe) id: Types.ObjectId) {
return this.postsService.findOne(id);
}
}
HINT To log request and response activity clearly and efficiently, you can install nesjs-http-logger.
You can use IsObjectIdPipe and ParseObjectIdPipe with GraphQL.
Example of using IsObjectIdPipe with GraphQL:
import { IsObjectIdPipe } from 'nestjs-object-id';
import { Args, Query, Resolver } from '@nestjs/graphql';
@Resolver(() => Post)
export class PostsResolver {
constructor(private readonly postsService: PostsService) {}
@Query(() => Post)
post(@Args('id', IsObjectIdPipe) id: string) {
return this.postsService.findOne(id);
}
}
You can use IsObjectIdPipe and ParseObjectIdPipe with Microservices.
Example of using IsObjectIdPipe with Microservices:
import { IsObjectIdPipe } from 'nestjs-object-id';
import { MessagePattern, Payload } from '@nestjs/microservices';
@Controller('posts')
export class PostsController {
constructor(private readonly postsService: PostsService) {}
@MessagePattern('find_post')
findOne(@Payload('id', IsObjectIdPipe) id: string) {
return this.postsService.findOne(id);
}
}
@IsObjectId() is a decorator for validating MongoDB ObjectIds in DTOs.
Here is an example along with commonly used IsString
and IsNotEmpty
from class-validator package.
import { IsObjectId } from 'nestjs-object-id';
import { IsString, IsNotEmpty } from 'class-validator';
class CreatePostDto {
@IsObjectId()
authorId: string;
@IsString()
@IsNotEmpty()
title: string;
}
If an invalid 'authorId' is received, an error will be thrown:
{
message: ["Invalid ObjectId for property \"authorId\": 0000000000000000",],
error: "Bad Request",
statusCode: 400
}
@ParseObjectId() is a decorator for parsing MongoDB ObjectIds in DTOs.
Here is an example along with commonly used IsString
and IsNotEmpty
from class-validator package.
import { IsObjectId } from 'nestjs-object-id';
import { IsString, IsNotEmpty } from 'class-validator';
import { Types } from 'mongoose';
class CreatePostDto {
@ParseObjectId()
authorId: Types.ObjectId;;
@IsString()
@IsNotEmpty()
title: string;
}
If an invalid 'authorId' is received, an error will be thrown:
{
message: ["Invalid ObjectId for property \"authorId\": 0000000000000000",],
error: "Bad Request",
statusCode: 400
}
Vladyslav Braslavskyi GitHub
Licensed under the MIT License - see the LICENSE file for details.