3 回答

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

TA貢獻(xiàn)1966條經(jīng)驗(yàn) 獲得超4個(gè)贊
Nestjs 中有我完整的解決方案/實(shí)現(xiàn)
首先創(chuàng)建我的 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,
);
}
郵遞員中我應(yīng)該發(fā)送的正文的示例
{
"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貢獻(xiàn)1775條經(jīng)驗(yàn) 獲得超8個(gè)贊
class-validator 確實(shí)支持?jǐn)?shù)組驗(yàn)證,您只需添加您在 @ValidateNested( { every: true } ) 中所做的操作,您只需將 every 添加到集合元素中:
export class SequenceQuery {? ?
@MinLength(10, {
? ? each: true,
? ? message: 'collection name is too short',
? })
collection: string;
identifier: string;
count: number;
}
添加回答
舉報(bào)