diff --git a/packages/common/pipes/parse-bool.pipe.ts b/packages/common/pipes/parse-bool.pipe.ts index 5876144d94c..a5b6bcb6ddd 100644 --- a/packages/common/pipes/parse-bool.pipe.ts +++ b/packages/common/pipes/parse-bool.pipe.ts @@ -48,14 +48,32 @@ export class ParseBoolPipe value: string | boolean, metadata: ArgumentMetadata, ): Promise { - if (value === true || value === 'true') { + if (this.isTrue(value)) { return true; } - if (value === false || value === 'false') { + if (this.isFalse(value)) { return false; } throw this.exceptionFactory( 'Validation failed (boolean string is expected)', ); } + + /** + * @param value currently processed route argument + * @returns `true` if `value` is said 'true', ie., if it is equal to the boolean + * `true` or the string `"true"` + */ + protected isTrue(value: string | boolean): boolean { + return value === true || value === 'true'; + } + + /** + * @param value currently processed route argument + * @returns `true` if `value` is said 'false', ie., if it is equal to the boolean + * `false` or the string `"false"` + */ + protected isFalse(value: string | boolean): boolean { + return value === false || value === 'false'; + } } diff --git a/packages/common/pipes/parse-float.pipe.ts b/packages/common/pipes/parse-float.pipe.ts index 086152a9b00..1f0fdb0212c 100644 --- a/packages/common/pipes/parse-float.pipe.ts +++ b/packages/common/pipes/parse-float.pipe.ts @@ -39,15 +39,23 @@ export class ParseFloatPipe implements PipeTransform { * @param metadata contains metadata about the currently processed route argument */ async transform(value: string, metadata: ArgumentMetadata): Promise { - const isNumeric = - ['string', 'number'].includes(typeof value) && - !isNaN(parseFloat(value)) && - isFinite(value as any); - if (!isNumeric) { + if (!this.isNumeric(value)) { throw this.exceptionFactory( 'Validation failed (numeric string is expected)', ); } return parseFloat(value); } + + /** + * @param value currently processed route argument + * @returns `true` if `value` is a valid float number + */ + protected isNumeric(value: string): boolean { + return ( + ['string', 'number'].includes(typeof value) && + !isNaN(parseFloat(value)) && + isFinite(value as any) + ); + } } diff --git a/packages/common/pipes/parse-int.pipe.ts b/packages/common/pipes/parse-int.pipe.ts index 2ba713f85ae..9d79a2b4c57 100644 --- a/packages/common/pipes/parse-int.pipe.ts +++ b/packages/common/pipes/parse-int.pipe.ts @@ -44,15 +44,23 @@ export class ParseIntPipe implements PipeTransform { * @param metadata contains metadata about the currently processed route argument */ async transform(value: string, metadata: ArgumentMetadata): Promise { - const isNumeric = - ['string', 'number'].includes(typeof value) && - /^-?\d+$/.test(value) && - isFinite(value as any); - if (!isNumeric) { + if (!this.isNumeric(value)) { throw this.exceptionFactory( 'Validation failed (numeric string is expected)', ); } return parseInt(value, 10); } + + /** + * @param value currently processed route argument + * @returns `true` if `value` is a valid integer number + */ + protected isNumeric(value: string): boolean { + return ( + ['string', 'number'].includes(typeof value) && + /^-?\d+$/.test(value) && + isFinite(value as any) + ); + } }