error validation

This commit is contained in:
2025-05-15 14:44:16 +07:00
parent 87a7eb0931
commit 301501dbac
14 changed files with 151 additions and 4 deletions

View File

@@ -6,6 +6,7 @@ import { ConfigModule, ConfigService } from '@nestjs/config';
import * as Joi from 'joi'; import * as Joi from 'joi';
import {UsersModule} from "./users/user.module"; import {UsersModule} from "./users/user.module";
import {AuthenticationModule} from "./authentication/authentication.module"; import {AuthenticationModule} from "./authentication/authentication.module";
import { PostsModule } from './posts/posts.module';
@Module({ @Module({
imports: [ imports: [
@@ -33,7 +34,8 @@ import {AuthenticationModule} from "./authentication/authentication.module";
}) })
}), }),
UsersModule, UsersModule,
AuthenticationModule AuthenticationModule,
PostsModule
], ],
controllers: [AppController], controllers: [AppController],
providers: [AppService], providers: [AppService],

View File

@@ -7,7 +7,7 @@ import {
UseGuards, UseGuards,
Get, Get,
ClassSerializerInterceptor, ClassSerializerInterceptor,
UseInterceptors, UseInterceptors, SerializeOptions,
} from '@nestjs/common'; } from '@nestjs/common';
import {AuthenticationService} from './authentication.service'; import {AuthenticationService} from './authentication.service';
import RegisterDto from './dto/register.dto'; import RegisterDto from './dto/register.dto';
@@ -21,6 +21,9 @@ import { UsersService } from 'src/users/user.service';
@Controller('authentication') @Controller('authentication')
@UseInterceptors(ClassSerializerInterceptor) @UseInterceptors(ClassSerializerInterceptor)
@SerializeOptions({
strategy: 'excludeAll'
})
export class AuthenticationController { export class AuthenticationController {
constructor( constructor(
private readonly authenticationService: AuthenticationService, private readonly authenticationService: AuthenticationService,

View File

@@ -1,10 +1,20 @@
import {NestFactory} from '@nestjs/core'; import {HttpAdapterHost, NestFactory, Reflector} from '@nestjs/core';
import {AppModule} from './app.module'; import {AppModule} from './app.module';
import * as cookieParser from 'cookie-parser'; import * as cookieParser from 'cookie-parser';
import {ClassSerializerInterceptor, ValidationPipe} from "@nestjs/common";
import {ExceptionsLoggerFilter} from "./utils/exceptionsLogger.filter";
async function bootstrap() { async function bootstrap() {
const app = await NestFactory.create(AppModule); const app = await NestFactory.create(AppModule);
app.use(cookieParser()); app.use(cookieParser());
app.useGlobalPipes(new ValidationPipe());
const {httpAdapter} = app.get(HttpAdapterHost);
app.useGlobalFilters(new ExceptionsLoggerFilter(httpAdapter));
app.useGlobalInterceptors(new ClassSerializerInterceptor(
app.get(Reflector))
);
await app.listen(process.env.PORT ?? 3000); await app.listen(process.env.PORT ?? 3000);
} }

View File

@@ -0,0 +1 @@
export class CreatePostDto {}

View File

@@ -0,0 +1,4 @@
import { PartialType } from '@nestjs/swagger';
import { CreatePostDto } from './create-post.dto';
export class UpdatePostDto extends PartialType(CreatePostDto) {}

View File

@@ -0,0 +1 @@
export class Post {}

View File

@@ -0,0 +1,7 @@
import { NotFoundException } from '@nestjs/common';
class PostNotFoundException extends NotFoundException {
constructor(postId: number) {
super(`Post with id ${postId} not found`);
}
}

View File

@@ -0,0 +1,20 @@
import { Test, TestingModule } from '@nestjs/testing';
import { PostsController } from './posts.controller';
import { PostsService } from './posts.service';
describe('PostsController', () => {
let controller: PostsController;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [PostsController],
providers: [PostsService],
}).compile();
controller = module.get<PostsController>(PostsController);
});
it('should be defined', () => {
expect(controller).toBeDefined();
});
});

View File

@@ -0,0 +1,34 @@
import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common';
import { PostsService } from './posts.service';
import { CreatePostDto } from './dto/create-post.dto';
import { UpdatePostDto } from './dto/update-post.dto';
@Controller('posts')
export class PostsController {
constructor(private readonly postsService: PostsService) {}
@Post()
create(@Body() createPostDto: CreatePostDto) {
return this.postsService.create(createPostDto);
}
@Get()
findAll() {
return this.postsService.findAll();
}
@Get(':id')
findOne(@Param('id') id: string) {
return this.postsService.findOne(+id);
}
@Patch(':id')
update(@Param('id') id: string, @Body() updatePostDto: UpdatePostDto) {
return this.postsService.update(+id, updatePostDto);
}
@Delete(':id')
remove(@Param('id') id: string) {
return this.postsService.remove(+id);
}
}

View File

@@ -0,0 +1,9 @@
import { Module } from '@nestjs/common';
import { PostsService } from './posts.service';
import { PostsController } from './posts.controller';
@Module({
controllers: [PostsController],
providers: [PostsService],
})
export class PostsModule {}

View File

@@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { PostsService } from './posts.service';
describe('PostsService', () => {
let service: PostsService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [PostsService],
}).compile();
service = module.get<PostsService>(PostsService);
});
it('should be defined', () => {
expect(service).toBeDefined();
});
});

View File

@@ -0,0 +1,26 @@
import { Injectable } from '@nestjs/common';
import { CreatePostDto } from './dto/create-post.dto';
import { UpdatePostDto } from './dto/update-post.dto';
@Injectable()
export class PostsService {
create(createPostDto: CreatePostDto) {
return 'This action adds a new post';
}
findAll() {
return `This action returns all posts`;
}
findOne(id: number) {
return `This action returns a #${id} post`;
}
update(id: number, updatePostDto: UpdatePostDto) {
return `This action updates a #${id} post`;
}
remove(id: number) {
return `This action removes a #${id} post`;
}
}

View File

@@ -1,5 +1,5 @@
import {Column, Entity, PrimaryGeneratedColumn} from 'typeorm'; import {Column, Entity, PrimaryGeneratedColumn} from 'typeorm';
import {Exclude} from 'class-transformer'; import {Exclude, Expose} from 'class-transformer';
@Entity() @Entity()
class User { class User {
@@ -7,9 +7,11 @@ class User {
public id?: number; public id?: number;
@Column({unique: true}) @Column({unique: true})
@Expose()
public email: string; public email: string;
@Column() @Column()
@Expose()
public name: string; public name: string;
@Column() @Column()

View File

@@ -0,0 +1,10 @@
import { Catch, ArgumentsHost } from '@nestjs/common';
import { BaseExceptionFilter } from '@nestjs/core';
@Catch()
export class ExceptionsLoggerFilter extends BaseExceptionFilter {
catch(exception: unknown, host: ArgumentsHost) {
console.log('Exception thrown', exception);
super.catch(exception, host);
}
}