Skip to content

Commit e310ee1

Browse files
committed
refactor(utils): refactor all validator code
1 parent 3030c2d commit e310ee1

File tree

76 files changed

+679
-295
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+679
-295
lines changed

package-lock.json

Lines changed: 180 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
"convert-pino-request-to-curl": "^1.0.8",
5959
"handlebars": "^4.7.8",
6060
"helmet": "^8.0.0",
61+
"i18next": "^24.2.2",
6162
"js-yaml": "^4.1.0",
6263
"jsonwebtoken": "^9.0.2",
6364
"luxon": "^3.5.0",
@@ -89,7 +90,8 @@
8990
"v8": "^0.1.0",
9091
"write-excel-file": "^2.3.1",
9192
"yaml": "^2.7.0",
92-
"zod": "^3.24.2"
93+
"zod": "^3.24.2",
94+
"zod-i18n-map": "^2.27.0"
9395
},
9496
"devDependencies": {
9597
"@commitlint/cli": "^19.7.1",

src/core/cat/entity/cat.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
import { z } from 'zod';
2-
31
import { BaseEntity } from '@/utils/entity';
2+
import { Infer, InputValidator } from '@/utils/validator';
43

5-
const ID = z.string().uuid();
6-
const Name = z.string().trim().min(1).max(200);
7-
const Breed = z.string().trim().min(1).max(200);
8-
const Age = z.number().min(0).max(30);
9-
const CreatedAt = z.date().nullish();
10-
const UpdatedAt = z.date().nullish();
11-
const DeletedAt = z.date().nullish();
4+
const ID = InputValidator.string().uuid();
5+
const Name = InputValidator.string().trim().min(1).max(200);
6+
const Breed = InputValidator.string().trim().min(1).max(200);
7+
const Age = InputValidator.number().min(0).max(30);
8+
const CreatedAt = InputValidator.date().nullish();
9+
const UpdatedAt = InputValidator.date().nullish();
10+
const DeletedAt = InputValidator.date().nullish();
1211

13-
export const CatEntitySchema = z.object({
12+
export const CatEntitySchema = InputValidator.object({
1413
id: ID,
1514
name: Name,
1615
breed: Breed,
@@ -20,7 +19,7 @@ export const CatEntitySchema = z.object({
2019
deletedAt: DeletedAt
2120
});
2221

23-
type Cat = z.infer<typeof CatEntitySchema>;
22+
type Cat = Infer<typeof CatEntitySchema>;
2423

2524
export class CatEntity extends BaseEntity<CatEntity>() {
2625
name!: string;

src/core/cat/use-cases/__tests__/cat-create.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { Test } from '@nestjs/testing';
22
import { TestMock } from 'test/mock';
3-
import { ZodIssue } from 'zod';
43

54
import { CreatedModel } from '@/infra/repository';
65
import { ICatCreateAdapter } from '@/modules/cat/adapter';
76
import { ApiInternalServerException } from '@/utils/exception';
7+
import { ZodExceptionIssue } from '@/utils/validator';
88

99
import { CatEntity } from '../../entity/cat';
1010
import { ICatRepository } from '../../repository/cat';
@@ -39,7 +39,7 @@ describe(CatCreateUsecase.name, () => {
3939
test('when no input is specified, should expect an error', async () => {
4040
await TestMock.expectZodError(
4141
() => usecase.execute({} as CatCreateInput, TestMock.getMockTracing()),
42-
(issues: ZodIssue[]) => {
42+
(issues: ZodExceptionIssue[]) => {
4343
expect(issues).toEqual([
4444
{ message: 'Required', path: TestMock.nameOf<CatCreateInput>('name') },
4545
{ message: 'Required', path: TestMock.nameOf<CatCreateInput>('breed') },

src/core/cat/use-cases/__tests__/cat-delete.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { Test } from '@nestjs/testing';
22
import { TestMock } from 'test/mock';
3-
import { ZodIssue } from 'zod';
43

54
import { CatDeleteInput, CatDeleteUsecase } from '@/core/cat/use-cases/cat-delete';
65
import { ILoggerAdapter, LoggerModule } from '@/infra/logger';
76
import { UpdatedModel } from '@/infra/repository';
87
import { ICatDeleteAdapter } from '@/modules/cat/adapter';
98
import { ApiNotFoundException } from '@/utils/exception';
9+
import { ZodExceptionIssue } from '@/utils/validator';
1010

1111
import { CatEntity } from '../../entity/cat';
1212
import { ICatRepository } from '../../repository/cat';
@@ -40,7 +40,7 @@ describe(CatDeleteUsecase.name, () => {
4040
test('when no input is specified, should expect an error', async () => {
4141
await TestMock.expectZodError(
4242
() => usecase.execute({} as CatDeleteInput, TestMock.getMockTracing()),
43-
(issues: ZodIssue[]) => {
43+
(issues: ZodExceptionIssue[]) => {
4444
expect(issues).toEqual([{ message: 'Required', path: TestMock.nameOf<CatDeleteInput>('id') }]);
4545
}
4646
);

src/core/cat/use-cases/__tests__/cat-get-by-id.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { Test } from '@nestjs/testing';
22
import { TestMock } from 'test/mock';
3-
import { ZodIssue } from 'zod';
43

54
import { ILoggerAdapter, LoggerModule } from '@/infra/logger';
65
import { ICatGetByIdAdapter } from '@/modules/cat/adapter';
76
import { ApiNotFoundException } from '@/utils/exception';
7+
import { ZodExceptionIssue } from '@/utils/validator';
88

99
import { CatEntity } from '../../entity/cat';
1010
import { ICatRepository } from '../../repository/cat';
@@ -39,7 +39,7 @@ describe(CatGetByIdUsecase.name, () => {
3939
test('when no input is specified, should expect an error', async () => {
4040
await TestMock.expectZodError(
4141
() => usecase.execute({} as CatGetByIdInput),
42-
(issues: ZodIssue[]) => {
42+
(issues: ZodExceptionIssue[]) => {
4343
expect(issues).toEqual([{ message: 'Required', path: TestMock.nameOf<CatGetByIdInput>('id') }]);
4444
}
4545
);

src/core/cat/use-cases/__tests__/cat-list.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { Test } from '@nestjs/testing';
22
import { TestMock } from 'test/mock';
3-
import { ZodIssue } from 'zod';
43

54
import { CatListInput, CatListOutput, CatListUsecase } from '@/core/cat/use-cases/cat-list';
65
import { ILoggerAdapter, LoggerModule } from '@/infra/logger';
76
import { ICatListAdapter } from '@/modules/cat/adapter';
7+
import { ZodExceptionIssue } from '@/utils/validator';
88

99
import { CatEntity } from '../../entity/cat';
1010
import { ICatRepository } from '../../repository/cat';
@@ -38,7 +38,7 @@ describe(CatListUsecase.name, () => {
3838
test('when no input is specified, should expect an error', async () => {
3939
await TestMock.expectZodError(
4040
() => usecase.execute({} as CatListInput),
41-
(issues: ZodIssue[]) => {
41+
(issues: ZodExceptionIssue[]) => {
4242
expect(issues).toEqual([{ message: 'Required', path: TestMock.nameOf<CatListInput>('search') }]);
4343
}
4444
);

src/core/cat/use-cases/__tests__/cat-update.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { Test } from '@nestjs/testing';
22
import { TestMock } from 'test/mock';
3-
import { ZodIssue } from 'zod';
43

54
import { ILoggerAdapter, LoggerModule } from '@/infra/logger';
65
import { UpdatedModel } from '@/infra/repository';
76
import { ICatUpdateAdapter } from '@/modules/cat/adapter';
87
import { ApiNotFoundException } from '@/utils/exception';
8+
import { ZodExceptionIssue } from '@/utils/validator';
99

1010
import { CatEntity } from '../../entity/cat';
1111
import { ICatRepository } from '../../repository/cat';
@@ -40,7 +40,7 @@ describe(CatUpdateUsecase.name, () => {
4040
test('when no input is specified, should expect an error', async () => {
4141
await TestMock.expectZodError(
4242
() => usecase.execute({} as CatUpdateInput, TestMock.getMockTracing()),
43-
(issues: ZodIssue[]) => {
43+
(issues: ZodExceptionIssue[]) => {
4444
expect(issues).toEqual([{ message: 'Required', path: TestMock.nameOf<CatUpdateInput>('id') }]);
4545
}
4646
);

src/core/cat/use-cases/cat-create.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import { z } from 'zod';
2-
31
import { CreatedModel } from '@/infra/repository';
42
import { ValidateSchema } from '@/utils/decorators';
53
import { ApiTrancingInput } from '@/utils/request';
64
import { IUsecase } from '@/utils/usecase';
75
import { UUIDUtils } from '@/utils/uuid';
6+
import { Infer } from '@/utils/validator';
87

98
import { CatEntity, CatEntitySchema } from '../entity/cat';
109
import { ICatRepository } from '../repository/cat';
@@ -30,5 +29,5 @@ export class CatCreateUsecase implements IUsecase {
3029
}
3130
}
3231

33-
export type CatCreateInput = z.infer<typeof CatCreateSchema>;
32+
export type CatCreateInput = Infer<typeof CatCreateSchema>;
3433
export type CatCreateOutput = CreatedModel;

src/core/cat/use-cases/cat-delete.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import { z } from 'zod';
2-
31
import { ICatRepository } from '@/core/cat/repository/cat';
42
import { ValidateSchema } from '@/utils/decorators';
53
import { ApiNotFoundException } from '@/utils/exception';
64
import { ApiTrancingInput } from '@/utils/request';
75
import { IUsecase } from '@/utils/usecase';
6+
import { Infer } from '@/utils/validator';
87

98
import { CatEntity, CatEntitySchema } from '../entity/cat';
109

@@ -34,5 +33,5 @@ export class CatDeleteUsecase implements IUsecase {
3433
}
3534
}
3635

37-
export type CatDeleteInput = z.infer<typeof CatDeleteSchema>;
36+
export type CatDeleteInput = Infer<typeof CatDeleteSchema>;
3837
export type CatDeleteOutput = CatEntity;

0 commit comments

Comments
 (0)