diff --git a/src/api/chatting.controller.ts b/src/api/chatting.controller.ts index f8e825d6..343d8565 100644 --- a/src/api/chatting.controller.ts +++ b/src/api/chatting.controller.ts @@ -296,6 +296,7 @@ export class ChattingController { description: 'You can use it as buttons or list replacement', }) @CheckPolicies(CanSession(Action.Send, FromBody('session'))) + @UsePipes(new WAHAValidationPipe()) async sendPoll(@Body() request: MessagePollRequest) { const whatsapp = await this.manager.getWorkingSession(request.session); return whatsapp.sendPoll(request); diff --git a/src/nestjs/validation/TrimString.ts b/src/nestjs/validation/TrimString.ts new file mode 100644 index 00000000..5987562f --- /dev/null +++ b/src/nestjs/validation/TrimString.ts @@ -0,0 +1,23 @@ +/** + * Trim string, leave other values as is + * @param value + * @constructor + */ +export function TrimString({ value }: { value: any }) { + if (typeof value === 'string') { + return value.trim(); + } + return value; +} + +/** + * Trim every string in the array, leave other values as is + * @param value + * @constructor + */ +export function TrimStrings({ value }: { value: any }) { + if (Array.isArray(value)) { + return value.map((item) => TrimString({ value: item })); + } + return value; +} diff --git a/src/structures/chatting.dto.ts b/src/structures/chatting.dto.ts index b94844a1..f31f1995 100644 --- a/src/structures/chatting.dto.ts +++ b/src/structures/chatting.dto.ts @@ -5,9 +5,13 @@ import { getSchemaPath, } from '@nestjs/swagger'; import { IsFileType } from '@waha/nestjs/validation/IsFileType'; +import { TrimString, TrimStrings } from '@waha/nestjs/validation/TrimString'; import { GetChatMessagesQuery } from '@waha/structures/chats.dto'; import { plainToInstance, Transform, Type } from 'class-transformer'; import { + ArrayMaxSize, + ArrayMinSize, + ArrayUnique, IsArray, IsBoolean, IsNotEmpty, @@ -15,6 +19,7 @@ import { IsOptional, IsString, IsUrl, + MaxLength, ValidateNested, } from 'class-validator'; @@ -441,20 +446,38 @@ export class MessagePoll { @ApiProperty({ example: 'How are you?', }) + @Transform(TrimString) + @IsString() + @IsNotEmpty() + @MaxLength(255) name: string; @ApiProperty({ example: ['Awesome!', 'Good!', 'Not bad!'], }) + @Transform(TrimStrings) + @IsArray() + @ArrayMinSize(2) + @ArrayMaxSize(12) + @ArrayUnique() + @IsString({ each: true }) + @IsNotEmpty({ each: true }) + @MaxLength(100, { each: true }) options: string[]; + @IsOptional() + @IsBoolean() multipleAnswers = false; } export class MessagePollRequest extends ChatRequest { @GeneratedMessageIdProperty() + @IsOptional() + @IsString() id?: string; + @ValidateNested() + @Type(() => MessagePoll) poll: MessagePoll; @ApiProperty({ @@ -462,6 +485,8 @@ export class MessagePollRequest extends ChatRequest { 'The ID of the message to reply to - false_11111111111@c.us_AAAAAAAAAAAAAAAAAAAA', example: null, }) + @IsOptional() + @IsString() reply_to?: string; }