| 1 | import { Param, PipeTransform, BadRequestException } from '@nestjs/common'; |
| 2 | import { isValidObjectId } from 'mongoose'; |
| 3 | |
| 4 | export class ObjectIdPipe implements PipeTransform { |
| 5 | transform(value: string) { |
| 6 | if (!isValidObjectId(value)) { |
| 7 | throw new BadRequestException('Invalid ObjectId'); |
| 8 | } |
| 9 | return value; |
| 10 | } |
| 11 | } |
| 12 | |
| 13 | export function ParamObjectId(property: string = 'id') { |
| 14 | return Param(property, new ObjectIdPipe()); |
| 15 | } |
| 16 |