Skip to content

Commit

Permalink
Merge e089a47 into 68edfda
Browse files Browse the repository at this point in the history
  • Loading branch information
oschweitzer committed Feb 5, 2018
2 parents 68edfda + e089a47 commit 74bec57
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
19 changes: 10 additions & 9 deletions src/common/pipes/parse-int.pipe.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { BadRequestException } from '../exceptions/bad-request.exception';
import { PipeTransform } from '../interfaces/pipe-transform.interface';
import { Pipe, ArgumentMetadata } from '../index';
import {BadRequestException} from '../exceptions/bad-request.exception';
import {PipeTransform} from '../interfaces/pipe-transform.interface';
import {ArgumentMetadata, Pipe} from '../index';

@Pipe()
export class ParseIntPipe implements PipeTransform<string> {
public async transform(value: string, metadata: ArgumentMetadata) {
const val = parseInt(value, 10);
if (isNaN(val)) {
throw new BadRequestException('Validation failed');
async transform(value: string, metadata: ArgumentMetadata): Promise<number> {
if ('string' === typeof value && !isNaN(parseFloat(value)) && isFinite(value as any)) {
return parseInt(value, 10);
}
else {
throw new BadRequestException('Numeric string is expected');
}
return val;
}
}
}
4 changes: 2 additions & 2 deletions src/common/test/pipes/parse-int.pipe.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ describe('ParseIntPipe', () => {
);
});
});
describe('when validation vails', () => {
describe('when validation fails', () => {
it('should throw an error', async () => {
return expect(target.transform('notanumber!', {} as any)).to.be
return expect(target.transform('123abc', {} as any)).to.be
.rejected;
});
});
Expand Down

0 comments on commit 74bec57

Please sign in to comment.