chore: validate poll payload - mention #2261
This commit is contained in:
1 parent
300f300a02
commit
b5702f1a48
3 files changed
+49
No files matched your search
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user