diff --git a/src/main.ts b/src/main.ts index 3f5bdc2..a06390f 100644 --- a/src/main.ts +++ b/src/main.ts @@ -21,7 +21,7 @@ function swagger(app: INestApplication) { async function bootstrap() { const app = await NestFactory.create(AppModule); - app.useGlobalPipes(new ValidationPipe()); + // app.useGlobalPipes(new ValidationPipe()); swagger(app); diff --git a/src/modules/author/author.module.ts b/src/modules/author/author.module.ts index 7b4b45b..92cc988 100644 --- a/src/modules/author/author.module.ts +++ b/src/modules/author/author.module.ts @@ -3,10 +3,11 @@ import { AuthorService } from './service/author.service'; import { AuthorController } from './controller/author.controller'; import { TypeOrmModule } from '@nestjs/typeorm'; import { Author } from './interfaces/author.entity'; +import { AuthorValidationService } from './service/author-validation.service'; @Module({ imports: [TypeOrmModule.forFeature([Author])], - providers: [AuthorService], + providers: [AuthorService, AuthorValidationService], controllers: [AuthorController], }) export class AuthorModule {} diff --git a/src/modules/author/controller/author.controller.ts b/src/modules/author/controller/author.controller.ts index bac816d..26e949f 100644 --- a/src/modules/author/controller/author.controller.ts +++ b/src/modules/author/controller/author.controller.ts @@ -66,7 +66,8 @@ export class AuthorController { }) @Post() async createOneAuthor( - @Body(CreateAuthorValidationPipe) author: CreateAuthorDto, + // @Body(CreateAuthorValidationPipe) author: CreateAuthorDto, + @Body() author: CreateAuthorDto, ) { return this.authorService.create(author); } diff --git a/src/modules/author/service/author-validation.service.ts b/src/modules/author/service/author-validation.service.ts new file mode 100644 index 0000000..fe691f0 --- /dev/null +++ b/src/modules/author/service/author-validation.service.ts @@ -0,0 +1,23 @@ +import { Injectable } from '@nestjs/common'; +import { plainToInstance } from 'class-transformer'; +import { validate } from 'class-validator'; +import { CreateAuthorDto } from '../interfaces/author.dto'; + +@Injectable() +export class AuthorValidationService { + public async createValidation(plainObject: Partial) { + const instance = plainToInstance(CreateAuthorDto, plainObject); + + const err = await validate(instance); + + if (err.length) { + console.log( + err.reduce((acc, e) => { + acc.push(e.constraints); + return acc; + }, []), + ); + throw new Error(err.toString()); + } + } +} diff --git a/src/modules/author/service/author.service.ts b/src/modules/author/service/author.service.ts index 56c2e09..e520698 100644 --- a/src/modules/author/service/author.service.ts +++ b/src/modules/author/service/author.service.ts @@ -1,13 +1,19 @@ -import { Injectable, NotFoundException } from '@nestjs/common'; +import { + BadRequestException, + Injectable, + NotFoundException, +} from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; import { CreateAuthorDto, UpdateAuthorDto } from '../interfaces/author.dto'; import { Author } from '../interfaces/author.entity'; +import { AuthorValidationService } from './author-validation.service'; @Injectable() export class AuthorService { constructor( @InjectRepository(Author) private authorRepository: Repository, + private authorValidator: AuthorValidationService, ) {} public async findAll(relations: string[]) { @@ -25,6 +31,12 @@ export class AuthorService { } public async create(authorDto: CreateAuthorDto) { + try { + await this.authorValidator.createValidation(authorDto); + } catch (err) { + throw new BadRequestException(err.message); + } + return this.authorRepository.save(this.authorRepository.create(authorDto)); }