File tree Expand file tree Collapse file tree 3 files changed +32
-0
lines changed
Expand file tree Collapse file tree 3 files changed +32
-0
lines changed Original file line number Diff line number Diff line change @@ -40,6 +40,7 @@ export type { VirtualizationModuleOptions } from './modules/virtualization/virtu
4040export { VirtualizationModule } from './modules/virtualization/virtualization.module.js' ;
4141export { VirtualizationService } from './modules/virtualization/virtualization.service.js' ;
4242export { ParseSchemaPipe } from './pipes/parse-schema.pipe.js' ;
43+ export { ValidObjectIdPipe } from './pipes/valid-object-id.pipe.js' ;
4344export { $BaseEnv } from './schemas/env.schema.js' ;
4445export type { BaseEnv } from './schemas/env.schema.js' ;
4546export type { BaseEnvSchema } from './utils/env.utils.js' ;
Original file line number Diff line number Diff line change 1+ import { BadRequestException } from '@nestjs/common' ;
2+ import { ObjectId } from 'mongodb' ;
3+ import { describe , expect , it } from 'vitest' ;
4+
5+ import { ValidObjectIdPipe } from '../valid-object-id.pipe.js' ;
6+
7+ describe ( 'ValidObjectIdPipe' , ( ) => {
8+ it ( 'should return the input value if it is a valid object id' , ( ) => {
9+ const pipe = new ValidObjectIdPipe ( ) ;
10+ const value = new ObjectId ( ) . toString ( ) ;
11+ expect ( pipe . transform ( value ) ) . toEqual ( value ) ;
12+ } ) ;
13+ it ( 'should throw BadRequestException if validation fails' , ( ) => {
14+ const pipe = new ValidObjectIdPipe ( ) ;
15+ const value = crypto . randomUUID ( ) ;
16+ expect ( ( ) => pipe . transform ( value ) ) . toThrow ( BadRequestException ) ;
17+ } ) ;
18+ } ) ;
Original file line number Diff line number Diff line change 1+ import { BadRequestException , Injectable } from '@nestjs/common' ;
2+ import type { PipeTransform } from '@nestjs/common' ;
3+ import { ObjectId } from 'mongodb' ;
4+
5+ @Injectable ( )
6+ export class ValidObjectIdPipe implements PipeTransform < string > {
7+ transform ( value : unknown ) : string {
8+ if ( ! ( typeof value === 'string' && ObjectId . isValid ( value ) ) ) {
9+ throw new BadRequestException ( `Invalid ObjectID: ${ String ( value ) } ` ) ;
10+ }
11+ return value ;
12+ }
13+ }
You can’t perform that action at this time.
0 commit comments