Skip to content

Commit ec7d3e6

Browse files
dtos with i18n validation message
1 parent 19decf1 commit ec7d3e6

File tree

6 files changed

+20
-14
lines changed

6 files changed

+20
-14
lines changed

src/application/dtos/create-user.dto.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
11
import { IsEmail, IsString } from 'class-validator';
22

33
import { ApiProperty } from '@nestjs/swagger';
4+
import { i18nValidationMessage } from 'nestjs-i18n';
45

56
export class CreateUserDto {
67
@ApiProperty({
78
description: 'the username of the user',
89
example: 'john_doe',
910
})
10-
@IsString()
11+
@IsString({ message: 'validation.NAME_REQUIRED' })
1112
username: string;
1213

1314
@ApiProperty({
1415
description: 'the password of the user',
1516
example: 'StrongP@ssw0rd!',
1617
})
17-
@IsString()
18+
@IsString({ message: i18nValidationMessage('validation.PASSWORD_REQUIRED') })
1819
password: string;
1920

2021
@ApiProperty({
2122
description: 'the email of the user',
2223
example: 'anyName@domain.com',
2324
})
24-
@IsEmail()
25+
@IsEmail({}, { message: i18nValidationMessage('validation.EMAIL_INVALID') })
2526
email: string;
2627
@IsString()
2728
salt?: string;

src/application/dtos/generate-recipe.dto.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ export class generateRecipeDto {
66
description: 'the recipe name',
77
example: 'Pancakes',
88
})
9-
@IsString()
9+
@IsString({ message: 'validation.FIELD_REQUIRED' })
1010
title: string;
1111
}

src/application/dtos/login-user.dto.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export class LoginDto {
1414
description: 'the user password',
1515
example: 'strongPassword123',
1616
})
17-
@IsString({ message: i18nValidationMessage('validation.password') })
17+
@IsString({ message: i18nValidationMessage('auth.PASSWORD_INCORRECT') })
1818
password: string;
1919

2020
constructor(email: string, password: string) {

src/application/dtos/recipe.dto.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ export class RecipeDto extends CreateRecipeDto {
1515

1616
@ApiProperty({
1717
description: 'Step-by-step cooking instructions',
18-
example: '1. Preheat the oven to 350°F (175°C). 2. Mix all ingredients...'
19-
})
18+
example: '1. Preheat the oven to 350°F (175°C). 2. Mix all ingredients...',
19+
})
2020
@IsString()
2121
instructions: string;
2222

@@ -25,16 +25,15 @@ export class RecipeDto extends CreateRecipeDto {
2525

2626
@IsDate()
2727
updatedAt?: Date;
28-
user?: UserDto; // Relacionamento com o usuário
29-
// Relacionamento com o usuário
28+
user?: UserDto;
3029
constructor(
3130
id: number,
3231
title: string,
3332
ingredients: string,
3433
instructions: string,
3534
createdAt: Date,
3635
updatedAt?: Date,
37-
user?: UserDto, // Relacionamento com o usuário
36+
user?: UserDto,
3837
) {
3938
super(title);
4039
this.ingredients = ingredients;
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,30 @@
11
import { ApiProperty } from '@nestjs/swagger';
22
import { IsNumber, IsString } from 'class-validator';
3+
import { i18nValidationMessage } from 'nestjs-i18n';
34

45
export class UpdateRecipeDto {
56
@ApiProperty()
6-
@IsNumber()
7+
@IsNumber({}, { message: i18nValidationMessage('common.INVALID_INPUT') })
78
id: number;
89

910
@ApiProperty({
1011
description: 'the recipe name',
1112
example: 'Pancakes',
1213
})
13-
@IsString()
14+
@IsString({ message: i18nValidationMessage('recipe.INVALID_RECIPE_NAME') })
1415
title?: string;
1516

1617
@ApiProperty({
1718
description: 'the recipe ingredients',
1819
example: 'Flour, Eggs, Milk, Sugar, Baking Powder',
1920
})
20-
@IsString()
21+
@IsString({ message: i18nValidationMessage('recipe.INGREDIENTS_REQUIRED') })
2122
ingredients?: string;
2223

2324
@ApiProperty({
2425
description: 'the recipe instructions',
2526
example: '1. Mix ingredients. 2. Cook on a griddle until golden brown.',
2627
})
27-
@IsString()
28+
@IsString({ message: i18nValidationMessage('recipe.INSTRUCTIONS_REQUIRED') })
2829
instructions?: string;
2930
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,29 @@
11
import { ApiProperty } from '@nestjs/swagger';
2+
import { IsNumber, IsString } from 'class-validator';
23

34
export class updateUserDto {
45
@ApiProperty({
56
description: 'the user id',
67
example: 1,
78
})
9+
@IsNumber()
810
id: number;
911
@ApiProperty({
1012
description: 'the user name',
1113
example: 'John Doe',
1214
})
15+
@IsString()
1316
name?: string;
1417
@ApiProperty({
1518
description: 'the user email',
1619
example: 'anyName@domain.com',
1720
})
21+
@IsString()
1822
email?: string;
1923
@ApiProperty({
2024
description: 'the user password',
2125
example: 'strongPassword123',
2226
})
27+
@IsString()
2328
password?: string;
2429
}

0 commit comments

Comments
 (0)