Skip to content

Commit

Permalink
Merge pull request #4116 from nestjs/revert-4110-feat/transform-primi…
Browse files Browse the repository at this point in the history
…tives

Revert "feat(common): transform primitives (number, bool) with validation pipe"
  • Loading branch information
kamilmysliwiec authored Feb 20, 2020
2 parents 997f667 + 57ff808 commit de8987f
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 77 deletions.
22 changes: 1 addition & 21 deletions packages/common/pipes/validation.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ export class ValidationPipe implements PipeTransform<any> {
public async transform(value: any, metadata: ArgumentMetadata) {
const { metatype } = metadata;
if (!metatype || !this.toValidate(metadata)) {
return this.isTransformEnabled
? this.transformPrimitive(value, metadata)
: value;
return value;
}
const originalValue = value;
value = this.toEmptyIfNil(value);
Expand Down Expand Up @@ -121,24 +119,6 @@ export class ValidationPipe implements PipeTransform<any> {
return !types.some(t => metatype === t) && !isNil(metatype);
}

private transformPrimitive(value: any, metadata: ArgumentMetadata) {
if (!metadata.data) {
// leave top-level query/param objects unmodified
return value;
}
const { type, metatype } = metadata;
if (type !== 'param' && type !== 'query') {
return value;
}
if (metatype === Boolean) {
return value === true || value === 'true';
}
if (metatype === Number) {
return +value;
}
return value;
}

private toEmptyIfNil<T = any, R = any>(value: T): R | {} {
return isNil(value) ? {} : value;
}
Expand Down
56 changes: 0 additions & 56 deletions packages/common/test/pipes/validation.pipe.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,62 +119,6 @@ describe('ValidationPipe', () => {
TestModel,
);
});
describe('when input is a query parameter (number)', () => {
it('should parse to number', async () => {
target = new ValidationPipe({ transform: true });
const value = '3.14';

expect(
await target.transform(value, {
metatype: Number,
data: 'test',
type: 'query',
}),
).to.be.equal(+value);
});
});
describe('when input is a path parameter (number)', () => {
it('should parse to number', async () => {
target = new ValidationPipe({ transform: true });
const value = '3.14';

expect(
await target.transform(value, {
metatype: Number,
data: 'test',
type: 'param',
}),
).to.be.equal(+value);
});
});
describe('when input is a query parameter (boolean)', () => {
it('should parse to boolean', async () => {
target = new ValidationPipe({ transform: true });
const value = 'true';

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

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

0 comments on commit de8987f

Please sign in to comment.