如何在没有 DTO 的情况下验证参数字符串是否为 Nestjs 中的 MongoId
How to validate if a Param string is a MongoId in Nestjs without DTO
我的控制器中有请求,@Param
是 MongoId 的字符串版本。
如果我使用无效的字符串格式调用此请求,而不匹配 MongoId 格式,则请求将一直进行到 MongoDB 调用引发内部服务器错误。
我如何验证 "aaa"
或“ANWPINREBAFSOFASD”未验证并在我的请求中尽早停止
当前控制器端点:
@Get(':id')
@ApiOperation({ summary: 'Get nice information' })
findOne(
@Param('id') id: string) {
return this.niceService.findOne(id);
}
调用的服务:
async findOne(id: string): Promise<NiceDocument> {
const niceResult: NiceDocument = await this.NiceSchema.findById(id)
if (!niceResult) {
throw new NotFoundException()
}
return table
}
答案是使用自定义验证管道:
创建管道并导出它:
import { ArgumentMetadata, BadRequestException, Injectable, PipeTransform } from "@nestjs/common";
import {ObjectId} from 'mongodb'
@Injectable()
export class ValidateMongoId implements PipeTransform<string> {
transform(value: string, metadata: ArgumentMetadata): string{ // Optional casting into ObjectId if wanted!
if(ObjectId.isValid(value)){
if((String)(new ObjectId(value)) === value)
return value;
throw new BadRequestException
}
throw new BadRequestException
};
}
使用控制器中的管道来验证字符串
@Get(':id')
@ApiOperation({ summary: 'Get nice information' })
findOne(
@Param('id', ValidateMongoId) id: string) {
return this.niceService.findOne(id);
}
或者,如果您使用 mongoDB 而不是猫鼬,您可以将管道中的返回类型从字符串更改为 ObjectId,猫鼬支持带有字符串格式 ID 的请求
中使用class验证器
通过使用@IsMongoIdObject()
像这样:
class ParamDTO{
@IsMongoIdObject()
id:string
}
----你的功能---
@Get(':id')
@ApiOperation({ summary: 'Get nice information' })
findOne(
@Param() id: ParamDTO) {
return this.niceService.findOne(id.id);
}
我的控制器中有请求,@Param
是 MongoId 的字符串版本。
如果我使用无效的字符串格式调用此请求,而不匹配 MongoId 格式,则请求将一直进行到 MongoDB 调用引发内部服务器错误。
我如何验证 "aaa"
或“ANWPINREBAFSOFASD”未验证并在我的请求中尽早停止
当前控制器端点:
@Get(':id')
@ApiOperation({ summary: 'Get nice information' })
findOne(
@Param('id') id: string) {
return this.niceService.findOne(id);
}
调用的服务:
async findOne(id: string): Promise<NiceDocument> {
const niceResult: NiceDocument = await this.NiceSchema.findById(id)
if (!niceResult) {
throw new NotFoundException()
}
return table
}
答案是使用自定义验证管道:
创建管道并导出它:
import { ArgumentMetadata, BadRequestException, Injectable, PipeTransform } from "@nestjs/common";
import {ObjectId} from 'mongodb'
@Injectable()
export class ValidateMongoId implements PipeTransform<string> {
transform(value: string, metadata: ArgumentMetadata): string{ // Optional casting into ObjectId if wanted!
if(ObjectId.isValid(value)){
if((String)(new ObjectId(value)) === value)
return value;
throw new BadRequestException
}
throw new BadRequestException
};
}
使用控制器中的管道来验证字符串
@Get(':id')
@ApiOperation({ summary: 'Get nice information' })
findOne(
@Param('id', ValidateMongoId) id: string) {
return this.niceService.findOne(id);
}
或者,如果您使用 mongoDB 而不是猫鼬,您可以将管道中的返回类型从字符串更改为 ObjectId,猫鼬支持带有字符串格式 ID 的请求
通过使用@IsMongoIdObject()
像这样:
class ParamDTO{
@IsMongoIdObject()
id:string
}
----你的功能---
@Get(':id')
@ApiOperation({ summary: 'Get nice information' })
findOne(
@Param() id: ParamDTO) {
return this.niceService.findOne(id.id);
}