3 回答

TA貢獻2041條經驗 獲得超4個贊
我得到了問題的解決方案。
我應該將我的包裝類更改為:
export class SequenceQueries{
@ValidateNested({ each: true })
@Type(() => SequenceQuery) // added @Type
queries:SequenceQuery[];
}
但我將保留這個問題,以防萬一有人有替代解決方案,例如不必創建包裝類。

TA貢獻1966條經驗 獲得超4個贊
Nestjs 中有我完整的解決方案/實現
首先創建我的 DTO 類
export class WebhookDto {
@IsString()
@IsEnum(WebHookType)
type: string;
@IsString()
@IsUrl()
url: string;
@IsBoolean()
active: boolean;
}
export class WebhookDtoArray {
@IsArray()
@ValidateNested({ each: true })
@Type(() => WebhookDto)
webhooks: WebhookDto[];
}
將我的 DTO 類放入我的控制器定義中
@MessagePattern('set_webhooks')
async setWebhooks(
@Payload('body') webhookDtoArray: WebhookDtoArray,
@Payload() data,
): Promise<Store> {
return this.storeManagementService.setWebhooks(
data.userPayload.id,
webhookDtoArray,
);
}
郵遞員中我應該發送的正文的示例
{
"webhooks": [{
"type": "InvoiceCreated",
"url": "https://test.free.beeceptor.com",
"active": true
},
{
"type": "InvoiceSettled",
"url": "https://test.free.beeceptor.com",
"active": true
},
{
"type": "InvoiceExpired",
"url": "https://test.free.beeceptor.com",
"active": true
}
]
}

TA貢獻1775條經驗 獲得超8個贊
class-validator 確實支持數組驗證,您只需添加您在 @ValidateNested( { every: true } ) 中所做的操作,您只需將 every 添加到集合元素中:
export class SequenceQuery {? ?
@MinLength(10, {
? ? each: true,
? ? message: 'collection name is too short',
? })
collection: string;
identifier: string;
count: number;
}
添加回答
舉報