Skip to content

Commit 9992996

Browse files
committed
refactor(@nestjs/common): rename the ValidationPipe properties
adopt the class-validator naming scheme
1 parent eebd72c commit 9992996

2 files changed

Lines changed: 16 additions & 16 deletions

File tree

src/common/pipes/validation.pipe.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { validate } from 'class-validator';
1+
import { validate, ValidatorOptions } from 'class-validator';
22
import { classToPlain, plainToClass } from 'class-transformer';
33
import { PipeTransform } from '../interfaces/pipe-transform.interface';
44
import { ArgumentMetadata, BadRequestException } from '../index';
@@ -7,21 +7,21 @@ import { Pipe } from './../decorators/core/component.decorator';
77

88
export interface ValidationPipeOptions {
99
transform?: boolean;
10-
strip?: boolean;
11-
reject?: boolean;
10+
whitelist?: boolean;
11+
forbidNonWhitelisted?: boolean;
1212
}
1313

1414
@Pipe()
1515
export class ValidationPipe implements PipeTransform<any> {
1616

17-
private shouldTransform: boolean;
18-
private shouldStrip: boolean;
19-
private shouldReject: boolean;
17+
private returnTransformed: boolean;
18+
19+
private validatorOptions: ValidatorOptions = {};
2020

2121
constructor(options?: ValidationPipeOptions) {
22-
this.shouldTransform = (options && 'transform' in options) ? options.transform : true;
23-
this.shouldStrip = options && (options.strip || options.reject);
24-
this.shouldReject = options && options.reject;
22+
this.returnTransformed = (options && 'transform' in options) ? options.transform : true;
23+
this.validatorOptions.whitelist = options && (options.whitelist || options.forbidNonWhitelisted);
24+
this.validatorOptions.forbidNonWhitelisted = options && options.forbidNonWhitelisted;
2525
}
2626

2727
public async transform(value, metadata: ArgumentMetadata) {
@@ -30,12 +30,12 @@ export class ValidationPipe implements PipeTransform<any> {
3030
return value;
3131
}
3232
const entity = plainToClass(metatype, value);
33-
const errors = await validate(entity, { whitelist: this.shouldStrip, forbidNonWhitelisted: this.shouldReject });
33+
const errors = await validate(entity, this.validatorOptions);
3434
if (errors.length > 0) {
3535
throw new BadRequestException(errors);
3636
}
37-
return this.shouldTransform ? entity
38-
: this.shouldStrip ? classToPlain(entity)
37+
return this.returnTransformed ? entity
38+
: this.validatorOptions.whitelist ? classToPlain(entity)
3939
: value;
4040
}
4141

src/common/test/pipes/validation.pipe.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,15 @@ describe('ValidationPipe', () => {
4949
});
5050
describe('when validation strips', () => {
5151
it('should return a TestModel without extra properties', async () => {
52-
target = new ValidationPipe({strip: true});
52+
target = new ValidationPipe({whitelist: true});
5353
const testObj = { prop1: 'value1', prop2: 'value2', prop3: 'value3'};
5454
expect(await target.transform(testObj, metadata)).to.be.instanceOf(TestModel);
5555
expect(await target.transform(testObj, metadata)).to.not.have.property('prop3');
5656
});
5757
});
5858
describe('when validation rejects', () => {
5959
it('should throw an error', () => {
60-
target = new ValidationPipe({reject: true});
60+
target = new ValidationPipe({forbidNonWhitelisted: true});
6161
const testObj = { prop1: 'value1', prop2: 'value2', prop3: 'value3' };
6262
expect(target.transform(testObj, metadata)).to.be.rejected;
6363
});
@@ -66,15 +66,15 @@ describe('ValidationPipe', () => {
6666
describe('when validation does\'t transform', () => {
6767
describe('when validation strips', () => {
6868
it('should return a plain object without extra properties', async () => {
69-
target = new ValidationPipe({transform: false, strip: true});
69+
target = new ValidationPipe({transform: false, whitelist: true});
7070
const testObj = { prop1: 'value1', prop2: 'value2', prop3: 'value3'};
7171
expect(await target.transform(testObj, metadata)).to.not.be.instanceOf(TestModel);
7272
expect(await target.transform(testObj, metadata)).to.not.have.property('prop3');
7373
});
7474
});
7575
describe('when validation rejects', () => {
7676
it('should throw an error', () => {
77-
target = new ValidationPipe({transform: false, reject: true});
77+
target = new ValidationPipe({transform: false, forbidNonWhitelisted: true});
7878
const testObj = { prop1: 'value1', prop2: 'value2', prop3: 'value3' };
7979
expect(target.transform(testObj, metadata)).to.be.rejected;
8080
});

0 commit comments

Comments
 (0)