Skip to content

Commit 67e28f4

Browse files
Merge branch 'InSantoshMahto-master'
2 parents 808bf41 + ee6cae0 commit 67e28f4

File tree

4 files changed

+37
-0
lines changed

4 files changed

+37
-0
lines changed

lib/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ export * from './errors';
44
export * from './factories';
55
export * from './interfaces';
66
export * from './mongoose.module';
7+
export * from './pipes';
78
export * from './utils';

lib/pipes/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './is-object-id.pipe';
2+
export * from './parse-object-id.pipe';

lib/pipes/is-object-id.pipe.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { BadRequestException, Injectable, PipeTransform } from '@nestjs/common';
2+
import { Types } from 'mongoose';
3+
4+
@Injectable()
5+
export class IsObjectIdPipe implements PipeTransform {
6+
transform(value: string): string {
7+
const isValidObjectId = Types.ObjectId.isValid(value);
8+
9+
if (!isValidObjectId) {
10+
throw new BadRequestException(
11+
`Invalid ObjectId: '${value}' is not a valid MongoDB ObjectId`,
12+
);
13+
}
14+
15+
return value;
16+
}
17+
}

lib/pipes/parse-object-id.pipe.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { BadRequestException, Injectable, PipeTransform } from '@nestjs/common';
2+
import { Types } from 'mongoose';
3+
4+
@Injectable()
5+
export class ParseObjectIdPipe implements PipeTransform {
6+
transform(value: string): Types.ObjectId {
7+
const isValidObjectId = Types.ObjectId.isValid(value);
8+
9+
if (!isValidObjectId) {
10+
throw new BadRequestException(
11+
`Invalid ObjectId: '${value}' is not a valid MongoDB ObjectId`,
12+
);
13+
}
14+
15+
return new Types.ObjectId(value);
16+
}
17+
}

0 commit comments

Comments
 (0)