| 1 | import { Injectable, PipeTransform } from '@nestjs/common' |
| 2 | import { isValidObjectId } from 'mongoose' |
| 3 | import { ResponseCode } from '../enums' |
| 4 | import { AppException } from '../exceptions' |
| 5 | |
| 6 | @Injectable() |
| 7 | export class ParseObjectIdPipe implements PipeTransform<string | undefined> { |
| 8 | transform(value: string | undefined): string | undefined { |
| 9 | if (value === undefined || value === null) |
| 10 | return value |
| 11 | if (!isValidObjectId(value)) { |
| 12 | throw new AppException(ResponseCode.ValidationFailed) |
| 13 | } |
| 14 | return value |
| 15 | } |
| 16 | } |
| 17 |