@Injectable()
데코레이터로 주석이 달린 클래스.
PipeTransform
인터페이스를 구현해야한다.
-
변환
: 입력 데이터를 원하는 출력으로 변환
-
검증
: 입력 데이터를 평가하고 유효하다면 변경없이 그대로 전달합니다. 그렇지 않으면 데이터가 정확하지 않을 때 예외를 던집니
Object schema validation
Joi library
를 사용하자.
$ npm install --save @hapi/joi
$ npm install --save-dev @types/hapi__joi
import { PipeTransform, Injectable, ArgumentMetadata, BadRequestException } from '@nestjs/common';
@Injectable()
export class JoiValidationPipe implements PipeTransform {
constructor(private readonly schema: Object) {}
transform(value: any, metadata: ArgumentMetadata) {
const { error } = this.schema.validate(value);
if (error) {
throw new BadRequestException('Validation failed');
}
return value;
}
}
Binding pipes
@Post()
@UsePipes(new JoiValidationPipe(createCatSchema))
async create(@Body() createCatDto: CreateCatDto) {
this.catsService.create(createCatDto);
}
Class validator
class-validator library를 잘 쓸 수 있음.
$ npm i --save class-validator class-transformer
create-cat.dto.ts
import { IsString, IsInt } from 'class-validator';
export class CreateCatDto {
@IsString()
readonly name: string;
@IsInt()
readonly age: number;
@IsString()
readonly breed: string;
}
자세한 데코레이터 : https://github.com/typestack/class-validator#usage
validation.pipe.ts
import { PipeTransform, Injectable, ArgumentMetadata, BadRequestException } from '@nestjs/common';
import { validate } from 'class-validator';
import { plainToClass } from 'class-transformer';
@Injectable()
export class ValidationPipe implements PipeTransform<any> {
async transform(value: any, { metatype }: ArgumentMetadata) {
if (!metatype || !this.toValidate(metatype)) {
return value;
}
const object = plainToClass(metatype, value);
const errors = await validate(object);
if (errors.length > 0) {
throw new BadRequestException('Validation failed');
}
return value;
}
private toValidate(metatype: Function): boolean {
const types: Function[] = [String, Boolean, Number, Array, Object];
return !types.includes(metatype);
}
}
cats.controller.ts
@Post()
async create(
@Body(new ValidationPipe()) createCatDto: CreateCatDto,
) {
this.catsService.create(createCatDto);
}
or
@Post()
@UsePipes(new ValidationPipe())
async create(@Body() createCatDto: CreateCatDto) {
this.catsService.create(createCatDto);
}
global pipe
main.ts
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.useGlobalPipes(new ValidationPipe());
await app.listen(3000);
}
bootstrap();
'Nestjs' 카테고리의 다른 글
Guards (0) | 2019.11.05 |
---|---|
Middleware (0) | 2019.11.05 |
Modules (0) | 2019.11.05 |
Providers (0) | 2019.11.05 |
Controller (0) | 2019.11.05 |