Skip to content

Commit

Permalink
Поправит валидационные сообщения метода createOffer, осуществит валид…
Browse files Browse the repository at this point in the history
…ацию для комментариев и пользователей
  • Loading branch information
AdonaiJehosua committed Nov 4, 2024
1 parent cdbd2c9 commit 73fed94
Show file tree
Hide file tree
Showing 11 changed files with 118 additions and 14 deletions.
17 changes: 14 additions & 3 deletions src/shared/modules/comment/comment.controller.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import {inject, injectable} from 'inversify';
import {Request, Response} from 'express';
import {BaseController, HttpMethod, ValidateObjectIdMiddleware} from '../../libs/rest/index.js';
import {BaseController, HttpMethod, ValidateDtoMiddleware, ValidateObjectIdMiddleware} from '../../libs/rest/index.js';
import {Logger} from '../../libs/logger/index.js';
import {Component} from '../../types/index.js';
import {CommentService} from './comment-service.interface.js';
import {CommentRdo} from './rdo/comment.rdo.js';
import {fillDTO} from '../../helpers/index.js';
import {CreateCommentDto} from './dto/create-comment.dto.js';
import {CreateOfferDto} from '../offer/index.js';

@injectable()
export class CommentController extends BaseController {
Expand All @@ -18,8 +19,18 @@ export class CommentController extends BaseController {

this.logger.info('Register routes for CommentController…');

this.addRoute({path: '/:offerId', method: HttpMethod.Get, handler: this.index, middlewares: [new ValidateObjectIdMiddleware('offerId')]});
this.addRoute({path: '/:offerId', method: HttpMethod.Post, handler: this.create, middlewares: [new ValidateObjectIdMiddleware('offerId')]});
this.addRoute({
path: '/:offerId',
method: HttpMethod.Get,
handler: this.index,
middlewares: [new ValidateObjectIdMiddleware('offerId')]
});
this.addRoute({
path: '/:offerId',
method: HttpMethod.Post,
handler: this.create,
middlewares: [new ValidateObjectIdMiddleware('offerId'), new ValidateDtoMiddleware(CreateOfferDto)]
});
}

public async index(req: Request, res: Response): Promise<void> {
Expand Down
15 changes: 15 additions & 0 deletions src/shared/modules/comment/dto/create-comment.dto.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
import {IsMongoId, IsNumber, IsString, Max, MaxLength, Min, MinLength} from 'class-validator';

import {CreateCommentValidationMessages} from './create-comment.messages.js';

export class CreateCommentDto {
@IsString({message: CreateCommentValidationMessages.text.invalidFormat})
@MinLength(10, {message: CreateCommentValidationMessages.text.minLength})
@MaxLength(100, {message: CreateCommentValidationMessages.text.maxLength})
public text: string;

@IsNumber({maxDecimalPlaces: 1}, {message: CreateCommentValidationMessages.rating.invalidFormat})
@Min(1, {message: CreateCommentValidationMessages.rating.minValue})
@Max(5, {message: CreateCommentValidationMessages.rating.maxValue})
public rating: number;

@IsMongoId({message: CreateCommentValidationMessages.offerId.invalidId})
public authorId: string;

@IsMongoId({message: CreateCommentValidationMessages.offerId.invalidId})
public offerId: string;
}
18 changes: 18 additions & 0 deletions src/shared/modules/comment/dto/create-comment.messages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export const CreateCommentValidationMessages = {
text: {
invalidFormat: 'text must be a string',
minLength: 'minimum text length must be 5 characters',
maxLength: 'maximum text length must be 1024 characters',
},
rating: {
invalidFormat: 'rating must be a number with maximum 1 decimal place',
minValue: 'minimum rating is 1',
maxValue: 'maximum rating is 5',
},
authorId: {
invalidId: 'author id field must be a valid id',
},
offerId: {
invalidId: 'offer id field must be a valid id',
}
} as const;
6 changes: 3 additions & 3 deletions src/shared/modules/offer/dto/create-offer.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {CityAndLocationValidationMessages} from './city-and-location.messages.js
import {CityValidate, LocationValidate} from './city-and-location.dto.js';

export class CreateOfferDto {
@IsString({message: CreateOfferValidationMessage.title.minLength})
@IsString({message: CreateOfferValidationMessage.title.invalidFormat})
@MinLength(10, {message: CreateOfferValidationMessage.title.minLength})
@MaxLength(100, {message: CreateOfferValidationMessage.title.maxLength})
public title: string;
Expand Down Expand Up @@ -48,8 +48,8 @@ export class CreateOfferDto {
public isPremium: boolean;

@IsNumber({maxDecimalPlaces: 1}, {message: CreateOfferValidationMessage.rating.invalidFormat})
@Min(1, {message: CreateOfferValidationMessage.price.minValue})
@Max(5, {message: CreateOfferValidationMessage.price.maxValue})
@Min(1, {message: CreateOfferValidationMessage.rating.minValue})
@Max(5, {message: CreateOfferValidationMessage.rating.maxValue})
public rating: number;

@IsString({message: CreateOfferValidationMessage.description.invalidFormat})
Expand Down
6 changes: 3 additions & 3 deletions src/shared/modules/offer/dto/create-offer.messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ export const CreateOfferValidationMessage = {
},
rating: {
invalidFormat: 'rating must be a number with maximum 1 decimal place',
minValue: 'minimum price is 100',
maxValue: 'maximum price is 200000',
minValue: 'minimum rating is 1',
maxValue: 'maximum rating is 5',
},
description: {
invalidFormat: 'description must be a string',
Expand All @@ -34,7 +34,7 @@ export const CreateOfferValidationMessage = {
invalidItem: 'items of images array must be a string type'
},
image: {
invalidFormat: 'image must be a string type',
invalidFormat: 'image must be a string',
},
offerGood: {
invalid: 'offerGood must be an array',
Expand Down
2 changes: 1 addition & 1 deletion src/shared/modules/offer/offer.http
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ PATCH http://localhost:4000/offers/6717e369c934030b401bf05e HTTP/1.1
Content-Type: application/json

{
"title": "Vovovo"
"title": "Vovovfdfjhdfjo"
}

###
Expand Down
16 changes: 15 additions & 1 deletion src/shared/modules/user/dto/create-user.dto.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
import {IsBoolean, IsEmail, IsString, MaxLength, MinLength} from 'class-validator';

import {CreateUserValidationMessages} from './create-user.messages.js';

export class CreateUserDto {
@IsString({message: CreateUserValidationMessages.name.invalidFormat})
@MinLength(10, {message: CreateUserValidationMessages.name.minLength})
@MaxLength(100, {message: CreateUserValidationMessages.name.maxLength})
public name: string;

@IsString({message: CreateUserValidationMessages.avatarUrl.invalidFormat})
public avatarUrl: string;

@IsBoolean({message: CreateUserValidationMessages.isPro.invalidFormat})
public isPro: boolean;

@IsEmail({}, {message: CreateUserValidationMessages.email.invalidFormat})
public email: string;
public token: string;

@IsString({message: CreateUserValidationMessages.email.invalidFormat})
public password: string;
}
19 changes: 19 additions & 0 deletions src/shared/modules/user/dto/create-user.messages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export const CreateUserValidationMessages = {
name: {
invalidFormat: 'name must be a string',
minLength: 'minimum name length must be 1',
maxLength: 'maximum name length must be 15',
},
avatarUrl: {
invalidFormat: 'avatar url must be a string',
},
isPro: {
invalidFormat: 'isPro flag must be a boolean',
},
email: {
invalidFormat: 'email email must be in the correct format',
},
password: {
invalidFormat: 'password must be a string',
}
} as const;
7 changes: 7 additions & 0 deletions src/shared/modules/user/dto/login-user.dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import {IsEmail, IsString} from 'class-validator';

import {LoginUserValidationMessages} from './login-user.messages.js';

export class LoginUserDto {
@IsEmail({}, {message: LoginUserValidationMessages.email.invalidFormat})
public email: string;

@IsString({message: LoginUserValidationMessages.password.invalidFormat})
public password: string;
}
8 changes: 8 additions & 0 deletions src/shared/modules/user/dto/login-user.messages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export const LoginUserValidationMessages = {
email: {
invalidFormat: 'email email must be in the correct format'
},
password: {
invalidFormat: 'password must be a string'
}
} as const;
18 changes: 15 additions & 3 deletions src/shared/modules/user/user.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {inject, injectable} from 'inversify';
import {Response} from 'express';
import {BaseController, HttpError, HttpMethod} from '../../libs/rest/index.js';
import {BaseController, HttpError, HttpMethod, ValidateDtoMiddleware} from '../../libs/rest/index.js';
import {Logger} from '../../libs/logger/index.js';
import {Component} from '../../types/index.js';
import {CreateUserRequest} from './create-user-request.type.js';
Expand All @@ -10,6 +10,8 @@ import {StatusCodes} from 'http-status-codes';
import {fillDTO} from '../../helpers/index.js';
import {UserRdo} from './rdo/user.rdo.js';
import {LoginUserRequest} from './login-user-request.type.js';
import {CreateUserDto} from './dto/create-user.dto.js';
import {LoginUserDto} from './dto/login-user.dto.js';

@injectable()
export class UserController extends BaseController {
Expand All @@ -21,8 +23,18 @@ export class UserController extends BaseController {
super(logger);
this.logger.info('Register routes for UserController...');

this.addRoute({path: '/register', method: HttpMethod.Post, handler: this.create});
this.addRoute({path: '/login', method: HttpMethod.Post, handler: this.login});
this.addRoute({
path: '/register',
method: HttpMethod.Post,
handler: this.create,
middlewares: [new ValidateDtoMiddleware(CreateUserDto)]
});
this.addRoute({
path: '/login',
method: HttpMethod.Post,
handler: this.login,
middlewares: [new ValidateDtoMiddleware(LoginUserDto)]
});
}

public async create(
Expand Down

0 comments on commit 73fed94

Please sign in to comment.