Created
July 19, 2024 15:04
-
-
Save Isradeveloper/7f7df8eeed32b9e3a2055f39ba42e177 to your computer and use it in GitHub Desktop.
NEST JS- Pipe para la validación de MongoID
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 { | |
| ArgumentMetadata, | |
| BadRequestException, | |
| Injectable, | |
| PipeTransform, | |
| } from '@nestjs/common'; | |
| import { isValidObjectId } from 'mongoose'; | |
| @Injectable() | |
| export class ParseMongoIdPipe implements PipeTransform { | |
| transform(value: any, metadata: ArgumentMetadata) { | |
| // console.log({ value, metadata }); | |
| if (!isValidObjectId(value)) { | |
| throw new BadRequestException(`${value} is not a valid MongoID`); | |
| } | |
| return value; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment