Skip to content

Instantly share code, notes, and snippets.

@Isradeveloper
Created July 19, 2024 15:04
Show Gist options
  • Select an option

  • Save Isradeveloper/7f7df8eeed32b9e3a2055f39ba42e177 to your computer and use it in GitHub Desktop.

Select an option

Save Isradeveloper/7f7df8eeed32b9e3a2055f39ba42e177 to your computer and use it in GitHub Desktop.
NEST JS- Pipe para la validación de MongoID
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