File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1- import { BadRequestException } from '../exceptions/bad-request.exception' ;
2- import { PipeTransform } from '../interfaces/pipe-transform.interface' ;
3- import { Pipe , ArgumentMetadata } from '../index' ;
1+ import { BadRequestException } from '../exceptions/bad-request.exception' ;
2+ import { PipeTransform } from '../interfaces/pipe-transform.interface' ;
3+ import { ArgumentMetadata , Pipe } from '../index' ;
44
55@Pipe ( )
66export class ParseIntPipe implements PipeTransform < string > {
7- public async transform ( value : string , metadata : ArgumentMetadata ) {
8- const val = parseInt ( value , 10 ) ;
9- if ( isNaN ( val ) ) {
10- throw new BadRequestException ( 'Validation failed' ) ;
7+ async transform ( value : string , metadata : ArgumentMetadata ) : Promise < number > {
8+ if ( 'string' === typeof value && ! isNaN ( parseFloat ( value ) ) && isFinite ( value as any ) ) {
9+ return parseInt ( value , 10 ) ;
10+ }
11+ else {
12+ throw new BadRequestException ( 'Numeric string is expected' ) ;
1113 }
12- return val ;
1314 }
14- }
15+ }
Original file line number Diff line number Diff line change @@ -17,9 +17,9 @@ describe('ParseIntPipe', () => {
1717 ) ;
1818 } ) ;
1919 } ) ;
20- describe ( 'when validation vails ' , ( ) => {
20+ describe ( 'when validation fails ' , ( ) => {
2121 it ( 'should throw an error' , async ( ) => {
22- return expect ( target . transform ( 'notanumber! ' , { } as any ) ) . to . be
22+ return expect ( target . transform ( '123abc ' , { } as any ) ) . to . be
2323 . rejected ;
2424 } ) ;
2525 } ) ;
You can’t perform that action at this time.
0 commit comments