Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/nestjs/nest
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Jul 29, 2020
2 parents 9ac0cf1 + 6450757 commit 4c816a7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
7 changes: 6 additions & 1 deletion packages/common/pipes/validation.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { ArgumentMetadata, ValidationError } from '../index';
import { ClassTransformOptions } from '../interfaces/external/class-transform-options.interface';
import { ValidatorOptions } from '../interfaces/external/validator-options.interface';
import { PipeTransform } from '../interfaces/features/pipe-transform.interface';
import { Type } from '../interfaces/type.interface';
import {
ErrorHttpStatusCode,
HttpErrorByCode,
Expand All @@ -20,6 +21,7 @@ export interface ValidationPipeOptions extends ValidatorOptions {
errorHttpStatusCode?: ErrorHttpStatusCode;
exceptionFactory?: (errors: ValidationError[]) => any;
validateCustomDecorators?: boolean;
expectedType?: Type<any>;
}

let classValidator: any = {};
Expand All @@ -32,6 +34,7 @@ export class ValidationPipe implements PipeTransform<any> {
protected validatorOptions: ValidatorOptions;
protected transformOptions: ClassTransformOptions;
protected errorHttpStatusCode: ErrorHttpStatusCode;
protected expectedType: Type<any>;
protected exceptionFactory: (errors: ValidationError[]) => any;
protected validateCustomDecorators: boolean;

Expand All @@ -41,6 +44,7 @@ export class ValidationPipe implements PipeTransform<any> {
transform,
disableErrorMessages,
errorHttpStatusCode,
expectedType,
transformOptions,
validateCustomDecorators,
...validatorOptions
Expand All @@ -52,6 +56,7 @@ export class ValidationPipe implements PipeTransform<any> {
this.isDetailedOutputDisabled = disableErrorMessages;
this.validateCustomDecorators = validateCustomDecorators || false;
this.errorHttpStatusCode = errorHttpStatusCode || HttpStatus.BAD_REQUEST;
this.expectedType = expectedType;
this.exceptionFactory =
options.exceptionFactory || this.createExceptionFactory();

Expand All @@ -64,7 +69,7 @@ export class ValidationPipe implements PipeTransform<any> {
}

public async transform(value: any, metadata: ArgumentMetadata) {
const { metatype } = metadata;
const metatype = this.expectedType || metadata.metatype;
if (!metatype || !this.toValidate(metadata)) {
return this.isTransformEnabled
? this.transformPrimitive(value, metadata)
Expand Down
27 changes: 27 additions & 0 deletions packages/common/test/pipes/validation.pipe.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,4 +396,31 @@ describe('ValidationPipe', () => {
});
});
});

describe('option: "expectedType"', () => {
class TestModel2 {
@IsString()
public prop1: string;

@IsBoolean()
public prop2: string;

@IsOptional()
@IsString()
public optionalProp: string;
}

it('should validate against the expected type if presented', async () => {
const m: ArgumentMetadata = {
type: 'body',
metatype: TestModel2,
data: '',
};

target = new ValidationPipe({ expectedType: TestModel });
const testObj = { prop1: 'value1', prop2: 'value2' };

expect(await target.transform(testObj, m)).to.equal(testObj);
});
});
});

0 comments on commit 4c816a7

Please sign in to comment.