Last active
January 27, 2022 00:20
-
-
Save redeemefy/c0c8606fb7efbbe62116bfda2ee83096 to your computer and use it in GitHub Desktop.
Validate MongodbId automatically with ValidationPipe and a class dto
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { ValidationPipe } from '@nestjs/common'; | |
import { NestFactory } from '@nestjs/core'; | |
import { AppModule } from './app.module'; | |
async function bootstrap() { | |
const app = await NestFactory.create(AppModule); | |
app.useGlobalPipes(new ValidationPipe({ // class-validator class-transformer for DTOs | |
transform: true, // converts payload to DTO classes in the controller | |
whitelist: true, // strip out props are not in DTOs | |
forbidNonWhitelisted: true, // return error if props not in DTOs are in payload | |
})) | |
await app.listen(3000); | |
} | |
bootstrap(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { IsMongoId } from "class-validator"; | |
import { ObjectId } from "mongoose"; | |
export class UpdateResourceParamDto { | |
@IsMongoId() | |
readonly id: string; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Controller, Get } from '@nestjs/common'; | |
import { UpdateResourceParamDto } from './dto/update-resource-param.dto'; | |
import { ResourceService } from './resource.service'; | |
@Controller('questions') | |
export class QuestionsController { | |
constructor(private readonly resourceService: ResourceService) {} | |
@Get(':id') | |
findOne(@Param() { id }: UpdateResourceParamDto) { | |
return this.resourceService.findOne(id) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable, NotFoundException } from '@nestjs/common'; | |
import { InjectModel } from '@nestjs/mongoose'; | |
import { Model } from 'mongoose'; | |
import { Resource } from './schemas/resource.schema'; | |
@Injectable() | |
export class ResourceService { | |
constructor(@InjectModel(Resourse.name) private readonly resourceModel: Model<Resource>){} | |
... | |
async findOne(id: string) { // There is no dto here | |
const resource = await this.resourceModel.findById(id) | |
if(!resource) { | |
throw new NotFoundException(`Resource id: ${id} not found.`) | |
} | |
return resource | |
} | |
... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment