ajout du modèle DTO pour users, et d'une liste blanche pour empêcher les payload malicieux
This commit is contained in:
@@ -1,9 +1,22 @@
|
||||
import { NestFactory } from '@nestjs/core';
|
||||
import { ValidationPipe } from '@nestjs/common';
|
||||
import { AppModule } from './app.module';
|
||||
|
||||
async function bootstrap() {
|
||||
|
||||
const app = await NestFactory.create(AppModule);
|
||||
// module afin de créer un pipe de validation qui va nous aider
|
||||
// à valider les données qui sont envoyées par les utilisateurs
|
||||
app.useGlobalPipes(
|
||||
new ValidationPipe({
|
||||
//permet une liste blanche
|
||||
whitelist: true,
|
||||
//interdit les propriétés non autorisées
|
||||
forbidNonWhitelisted: true,
|
||||
//permet de transformer les données en fonction de leur type
|
||||
transform: true,
|
||||
}),
|
||||
);
|
||||
await app.listen(3000);
|
||||
}
|
||||
bootstrap();
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
import { IsString } from 'class-validator';
|
||||
|
||||
export class CreateUsersDto {
|
||||
@IsString()
|
||||
readonly name: string;
|
||||
@IsString()
|
||||
readonly username: string;
|
||||
@IsString()
|
||||
readonly password: string;
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
// Les types partiels permettent d'importer toutes les variables d'une classe
|
||||
// et de les mettre comme optionnelles. De plus on peut hériter
|
||||
// des décorateurs de la classe parente (par exemple @IsString()).
|
||||
|
||||
import { PartialType } from "@nestjs/mapped-types";
|
||||
import { CreateUsersDto } from "./create-users.dto";
|
||||
|
||||
export class UpdateUsersDto extends PartialType(CreateUsersDto){}
|
||||
@@ -1,5 +1,6 @@
|
||||
export class User {
|
||||
userId: string;
|
||||
name: string;
|
||||
username: string;
|
||||
password: string;
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@ import {
|
||||
Body, Controller, Delete, Get, HttpCode,
|
||||
HttpStatus, Param, Patch, Post, Query
|
||||
} from '@nestjs/common';
|
||||
import { CreateUsersDto } from './dto/create-users.dto';
|
||||
import { UpdateUsersDto } from './dto/update-users.dto';
|
||||
|
||||
import { UsersService } from './users.service';
|
||||
|
||||
@@ -22,13 +24,13 @@ export class UsersController {
|
||||
|
||||
@Post()
|
||||
@HttpCode(HttpStatus.GONE)
|
||||
create(@Body() body) {
|
||||
return this.usersService.create(body);
|
||||
create(@Body() createUsersDto : CreateUsersDto ) {
|
||||
return this.usersService.create(createUsersDto);
|
||||
}
|
||||
|
||||
@Patch(':id')
|
||||
update(@Param('id') id: string, @Body() body) {
|
||||
return this.usersService.update(id, body);
|
||||
update(@Param('id') id: string, @Body() usersUpdateDto: UpdateUsersDto) {
|
||||
return this.usersService.update(id, usersUpdateDto);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
|
||||
@@ -7,12 +7,14 @@ export class UsersService {
|
||||
private users:User[] = [
|
||||
{
|
||||
userId: '1',
|
||||
username: 'john',
|
||||
name: 'John',
|
||||
username: 'TheBoss',
|
||||
password: 'changeme',
|
||||
},
|
||||
{
|
||||
userId: '2',
|
||||
username: 'maria',
|
||||
name: 'Jane',
|
||||
username: 'Jane2000',
|
||||
password: 'guess',
|
||||
},
|
||||
];
|
||||
@@ -34,7 +36,7 @@ export class UsersService {
|
||||
const user = this.users.push(createUserDto);
|
||||
if (!user)
|
||||
throw new HttpException(`The user could not be created.`,HttpStatus.NOT_FOUND);
|
||||
return 'User has been created';
|
||||
return createUserDto.username + ' has been created';
|
||||
}
|
||||
|
||||
update(id: string, updateUserDto: any) {
|
||||
|
||||
Reference in New Issue
Block a user