Skip to content

Commit

Permalink
Merge pull request #10953 from micalevisk/fix/partial-issue-10246
Browse files Browse the repository at this point in the history
fix(common)!: when transforming optional boolean parameters on `ValidationPipe`
  • Loading branch information
kamilmysliwiec committed Apr 5, 2023
2 parents 90a3416 + 14a0f06 commit 252e864
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 3 deletions.
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';

/**
* @publicApi
Expand Down Expand Up @@ -193,6 +193,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
// optional booleans shouldn't be parsed to a valid boolean when
// they were not defined
return undefined;
}
// Any fasly value but `undefined` will be parsed to `false`
return value === true || value === 'true';
}
if (metatype === Number) {
Expand Down
76 changes: 74 additions & 2 deletions packages/common/test/pipes/validation.pipe.spec.ts
Expand Up @@ -221,7 +221,7 @@ describe('ValidationPipe', () => {
});
});
describe('when input is a query parameter (boolean)', () => {
it('should parse to boolean', async () => {
it('should parse the string "true" to the boolean true', async () => {
target = new ValidationPipe({ transform: true });
const value = 'true';

Expand All @@ -233,9 +233,45 @@ describe('ValidationPipe', () => {
}),
).to.be.true;
});
it('should parse the string "false" to the boolean false', async () => {
target = new ValidationPipe({ transform: true });
const value = 'false';

expect(
await target.transform(value, {
metatype: Boolean,
data: 'test',
type: 'query',
}),
).to.be.false;
});
it('should parse an empty string to false', async () => {
target = new ValidationPipe({ transform: true });
const value = '';

expect(
await target.transform(value, {
metatype: Boolean,
data: 'test',
type: 'query',
}),
).to.be.false;
});
it('should parse undefined to undefined', async () => {
target = new ValidationPipe({ transform: true });
const value = undefined;

expect(
await target.transform(value, {
metatype: Boolean,
data: 'test',
type: 'query',
}),
).to.be.undefined;
});
});
describe('when input is a path parameter (boolean)', () => {
it('should parse to boolean', async () => {
it('should parse the string "true" to boolean true', async () => {
target = new ValidationPipe({ transform: true });
const value = 'true';

Expand All @@ -247,6 +283,42 @@ describe('ValidationPipe', () => {
}),
).to.be.true;
});
it('should parse the string "false" to boolean false', async () => {
target = new ValidationPipe({ transform: true });
const value = 'false';

expect(
await target.transform(value, {
metatype: Boolean,
data: 'test',
type: 'param',
}),
).to.be.false;
});
it('should parse an empty string to false', async () => {
target = new ValidationPipe({ transform: true });
const value = '';

expect(
await target.transform(value, {
metatype: Boolean,
data: 'test',
type: 'param',
}),
).to.be.false;
});
it('should parse undefined to undefined', async () => {
target = new ValidationPipe({ transform: true });
const value = undefined;

expect(
await target.transform(value, {
metatype: Boolean,
data: 'test',
type: 'param',
}),
).to.be.undefined;
});
});
describe('when validation strips', () => {
it('should return a TestModel without extra properties', async () => {
Expand Down

0 comments on commit 252e864

Please sign in to comment.