Skip to Content
DTO

Hello World with a Service example

Form input handling and class validation with vovk-dto. The request input is validated on the client-side before being sent to the server where it is validated again. Notice that the input data needs to be transformed with class-transformer into a DTO class in order to be validated on the client-side.

Result

Code

UserDto.ts
UserDtoController.ts
DtoFormExample.tsx
1import { IsString, IsNumber, Min, IsEmail, IsUUID, IsIn, IsBoolean, Max } from 'class-validator';
2import { JSONSchema } from 'class-validator-jsonschema';
3
4@JSONSchema({ description: 'User object' })
5export class UpdateUserBodyDto {
6 @IsString()
7 @JSONSchema({ description: 'User full name' })
8 name!: string;
9
10 @IsNumber()
11 @Min(0)
12 @Max(120)
13 @JSONSchema({ description: 'User age' })
14 age!: number;
15
16 @IsEmail()
17 @JSONSchema({ description: 'User email' })
18 email!: string;
19}
20
21@JSONSchema({ description: 'Path parameters' })
22export class UpdateUserParamsDto {
23 @IsUUID()
24 @JSONSchema({ description: 'User ID' })
25 id!: string;
26}
27
28@JSONSchema({ description: 'Query parameters' })
29export class UpdateUserQueryDto {
30 @IsIn(['email', 'push', 'none'])
31 @JSONSchema({ description: 'Notification type' })
32 notify!: string;
33}
34
35@JSONSchema({ description: 'Response object' })
36export class UpdateUserResponseDto {
37 @IsBoolean()
38 @JSONSchema({ description: 'Success status' })
39 success!: boolean;
40}
1import { IsString, IsNumber, Min, IsEmail, IsUUID, IsIn, IsBoolean, Max } from 'class-validator';
2import { JSONSchema } from 'class-validator-jsonschema';
3
4@JSONSchema({ description: 'User object' })
5export class UpdateUserBodyDto {
6 @IsString()
7 @JSONSchema({ description: 'User full name' })
8 name!: string;
9
10 @IsNumber()
11 @Min(0)
12 @Max(120)
13 @JSONSchema({ description: 'User age' })
14 age!: number;
15
16 @IsEmail()
17 @JSONSchema({ description: 'User email' })
18 email!: string;
19}
20
21@JSONSchema({ description: 'Path parameters' })
22export class UpdateUserParamsDto {
23 @IsUUID()
24 @JSONSchema({ description: 'User ID' })
25 id!: string;
26}
27
28@JSONSchema({ description: 'Query parameters' })
29export class UpdateUserQueryDto {
30 @IsIn(['email', 'push', 'none'])
31 @JSONSchema({ description: 'Notification type' })
32 notify!: string;
33}
34
35@JSONSchema({ description: 'Response object' })
36export class UpdateUserResponseDto {
37 @IsBoolean()
38 @JSONSchema({ description: 'Success status' })
39 success!: boolean;
40}
Last updated on