Skip to content

Commit

Permalink
fix(common): when transforming invalid boolean values
Browse files Browse the repository at this point in the history
on `ValidationPipe`, it must not return a valid boolean
  • Loading branch information
micalevisk committed Jan 26, 2023
1 parent bcf4bf9 commit 1abd56a
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion packages/common/pipes/validation.pipe.ts
Expand Up @@ -18,7 +18,7 @@ import {
HttpErrorByCode,
} from '../utils/http-error-by-code.util';
import { loadPackage } from '../utils/load-package.util';
import { isNil } from '../utils/shared.utils';
import { isNil, isUndefined } from '../utils/shared.utils';

export interface ValidationPipeOptions extends ValidatorOptions {
transform?: boolean;
Expand Down Expand Up @@ -179,6 +179,13 @@ export class ValidationPipe implements PipeTransform<any> {
return value;
}
if (metatype === Boolean) {
if (isUndefined(value)) {
// This is an workaround to deal with optional boolean values since
// the optional booleans shouldn't be parsed to a valid boolean when
// they are not defined
return undefined;
}
// Any fasly value but `undefined` will be parsed to `false`
return value === true || value === 'true';
}
if (metatype === Number) {
Expand Down

0 comments on commit 1abd56a

Please sign in to comment.