Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(message): publish the array bounds the validators enforce
Yudhi Armyndharisae08bb0ab169
diff --git a/src/modules/message/dto/bulk-message.dto.spec.ts b/src/modules/message/dto/bulk-message.dto.spec.tsindex 31b3a80b8..82a10d919 100644--- a/src/modules/message/dto/bulk-message.dto.spec.ts+++ b/src/modules/message/dto/bulk-message.dto.spec.ts@@ -1,6 +1,8 @@import { BadRequestException, ValidationPipe } from '@nestjs/common';+import { DECORATORS } from '@nestjs/swagger';import { GLOBAL_VALIDATION_OPTIONS } from '../../../config/app-validation';-import { SendBulkMessageDto } from './bulk-message.dto';+import { BULK_MESSAGES_MAX, BulkMessageContentDto, SendBulkMessageDto } from './bulk-message.dto';+import { MENTIONS_MAX, MENTION_WID_MAX_LENGTH } from './send-message.dto';// The production pipe, built from the options main.ts uses, so these cases cannot assert a contract// the running app does not apply (restated options left out implicit conversion). Its whitelist +@@ -72,3 +74,21 @@ describe('SendBulkMessageDto media url', () => {expect(dto.messages[0].content.image?.url).toBe('123');});});++// @nestjs/swagger does not derive bounds from the validators, so the published schema has to declare+// them itself, or a client generated from it accepts arrays the server answers with a 400.+describe('SendBulkMessageDto published schema', () => {+ const published = (dto: object, key: string): Record<string, unknown> | undefined =>+ Reflect.getMetadata(DECORATORS.API_MODEL_PROPERTIES, dto, key) as Record<string, unknown> | undefined;++ it('publishes the batch size cap', () => {+ expect(published(SendBulkMessageDto.prototype, 'messages')?.maxItems).toBe(BULK_MESSAGES_MAX);+ });++ it('publishes the mention list bounds', () => {+ expect(published(BulkMessageContentDto.prototype, 'mentions')).toMatchObject({+ maxItems: MENTIONS_MAX,+ items: { type: 'string', maxLength: MENTION_WID_MAX_LENGTH },+ });+ });+});diff --git a/src/modules/message/dto/bulk-message.dto.ts b/src/modules/message/dto/bulk-message.dto.tsindex 0bc0fba1f..66d4b9949 100644--- a/src/modules/message/dto/bulk-message.dto.ts+++ b/src/modules/message/dto/bulk-message.dto.ts@@ -54,7 +54,7 @@ class BulkMediaDto {ptt?: boolean;}-class BulkMessageContentDto {+export class BulkMessageContentDto {@ApiPropertyOptional({ description: 'Text content for text messages', maxLength: MESSAGE_TEXT_MAX_LENGTH })@IsOptional()@IsString()@@ -97,7 +97,13 @@ class BulkMessageContentDto {// Applies to the text body and to a media caption alike, matching the single-send routes. Every// item in a batch names its own list: a batch fans out to many chats, and a WID is only taggable// in a chat the participant is in.- @ApiPropertyOptional({ description: MENTIONS_DESCRIPTION, example: ['[email protected]'], type: [String] })+ @ApiPropertyOptional({+ description: MENTIONS_DESCRIPTION,+ example: ['[email protected]'],+ type: [String],+ maxItems: MENTIONS_MAX,+ items: { type: 'string', maxLength: MENTION_WID_MAX_LENGTH },+ })@IsOptional()@IsArray()@ArrayMaxSize(MENTIONS_MAX)@@ -167,6 +173,7 @@ export class SendBulkMessageDto {description:'Array of messages (max 100 per request; exact duplicate entries are collapsed — first occurrence wins)',type: [BulkMessageItemDto],+ maxItems: BULK_MESSAGES_MAX,})@IsArray()@ArrayMaxSize(BULK_MESSAGES_MAX)diff --git a/src/modules/message/dto/message-actions.dto.spec.ts b/src/modules/message/dto/message-actions.dto.spec.tsindex a1816c1e0..dca19ee89 100644--- a/src/modules/message/dto/message-actions.dto.spec.ts+++ b/src/modules/message/dto/message-actions.dto.spec.ts@@ -1,4 +1,5 @@import { ValidationPipe } from '@nestjs/common';+import { DECORATORS } from '@nestjs/swagger';import { plainToInstance } from 'class-transformer';import { validate, ValidationError } from 'class-validator';import {@@ -10,6 +11,7 @@ import {EditMessageDto,ReplyMessageDto,} from './message-actions.dto';+import { MENTIONS_MAX, MENTION_WID_MAX_LENGTH } from './send-message.dto';import { GLOBAL_VALIDATION_OPTIONS } from '../../../config/app-validation';/**@@ -188,3 +190,28 @@ describe('mentions on the quoted-send and edit routes (whitelist behaviour)', ()await expect(through(EditMessageDto, { ...EDIT, mentions: ['not-a-wid'] })).rejects.toBeDefined();});});++// @nestjs/swagger does not derive bounds from the validators, so the published schema has to declare+// them itself, or a client generated from it accepts arrays the server answers with a 400.+describe('message action DTO published schema', () => {+ const published = (dto: object, key: string): Record<string, unknown> | undefined =>+ Reflect.getMetadata(DECORATORS.API_MODEL_PROPERTIES, dto, key) as Record<string, unknown> | undefined;++ it('publishes the poll option bounds the validators enforce', () => {+ expect(published(SendPollDto.prototype, 'options')).toMatchObject({+ minItems: 2,+ maxItems: 12,+ items: { type: 'string', maxLength: 100 },+ });+ });++ it.each([+ ['ReplyMessageDto', ReplyMessageDto],+ ['EditMessageDto', EditMessageDto],+ ])('%s publishes the mention list bounds', (_name, dto) => {+ expect(published(dto.prototype, 'mentions')).toMatchObject({+ maxItems: MENTIONS_MAX,+ items: { type: 'string', maxLength: MENTION_WID_MAX_LENGTH },+ });+ });+});diff --git a/src/modules/message/dto/message-actions.dto.ts b/src/modules/message/dto/message-actions.dto.tsindex 146c0c1d2..ca12666ae 100644--- a/src/modules/message/dto/message-actions.dto.ts+++ b/src/modules/message/dto/message-actions.dto.ts@@ -115,6 +115,9 @@ export class SendPollDto {@ApiProperty({description: 'Options to vote on (WhatsApp allows between 2 and 12)',type: [String],+ minItems: 2,+ maxItems: 12,+ items: { type: 'string', maxLength: 100 },example: ['Park', 'Beach', 'Downtown'],})@IsArray()@@ -159,7 +162,13 @@ export class ReplyMessageDto {@MaxLength(MESSAGE_TEXT_MAX_LENGTH)text!: string;- @ApiPropertyOptional({ description: MENTIONS_DESCRIPTION, example: ['[email protected]'], type: [String] })+ @ApiPropertyOptional({+ description: MENTIONS_DESCRIPTION,+ example: ['[email protected]'],+ type: [String],+ maxItems: MENTIONS_MAX,+ items: { type: 'string', maxLength: MENTION_WID_MAX_LENGTH },+ })@IsOptional()@IsArray()@ArrayMaxSize(MENTIONS_MAX)@@ -374,7 +383,13 @@ export class EditMessageDto {// An edit REPLACES the message content, so tags are re-applied rather than preserved: omitting// this drops whatever the original body carried.- @ApiPropertyOptional({ description: MENTIONS_DESCRIPTION, example: ['[email protected]'], type: [String] })+ @ApiPropertyOptional({+ description: MENTIONS_DESCRIPTION,+ example: ['[email protected]'],+ type: [String],+ maxItems: MENTIONS_MAX,+ items: { type: 'string', maxLength: MENTION_WID_MAX_LENGTH },+ })@IsOptional()@IsArray()@ArrayMaxSize(MENTIONS_MAX)