我需要创建一个包含对象数组、字符串和数字的验证器。
下面是我发现的一个类似的问题::Class-validator - validate array of objects
下面是应该如何过滤数据......这意味着这些是我希望从请求正文中提取的好参数
{
"call_transfers": [
{"bid":33,"bid_id":"1239dasdj8","carrier":"Jimmies pizza", "qualification_duration":222},
{"bid":28,"bid_id":"3234sdfJIA","carrier":"Krusty crabs", "qualification_duration":22},
{"bid":19,"bid_id":"9anHXawefa","carrier":"Bikiny bottom", "qualification_duration":33},
],
"ping_id": "adi2837dasdjc8",
"time" : 0.123
}
我创建以下请求来测试类验证器是否按预期工作:
{
"name" : "Juan",
"email": "jcasdasda@gmail.com",
"token" : "1oap8X8ase@#*&",
"call_transfers": [
{"bid":33,"bid_id":"1239dasdj8","carrier":"Jimmies pizza", "qualification_duration":222, "fakeParam":123},
{"bid":28,"bid_id":"3234sdfJIA","carrier":"Krusty crabs", "qualification_duration":22, "fakeParam":123 },
{"bid":19,"bid_id":"9anHXawefa","carrier":"Bikiny bottom", "qualification_duration":33, "fakeParam":123}
],
"ping_id": "adi2837dasdjc8",
"time" : 0.123,
"authParameters": [{"id":123,"type":"user","value":"chocolates"}]
}
但是,我无法正确过滤所需的参数!
这是我正在使用的控制器逻辑:
setFlagsOnBid(
@Body('call_transfers') call_transfers: MediaAlphaResponseItem,
@Body('ping_id') ping_id: MediaAlphaResponseItem,
@Body('time') time: MediaAlphaResponseItem,
){
const filteredData = { call_transfers , ping_id , time };
return filteredData;
}
这是我正在使用的类验证:
export class MediaAlphaResponseItem {
@ValidateNested({ each: true })
@Type(() => MediaAlphaCallTransferItem)
call_transfers: MediaAlphaCallTransferItem[];
@IsString()
ping_id:string;
@IsNumber()
time:number;
}
class MediaAlphaCallTransferItem {
@IsNumber()
bid:number;
@IsString()
bid_id:string;
@IsString()
carrier:string;
@IsNumber()
qualification_duration:number;
}
这是postman x1c 0d1x中的输出
1条答案
按热度按时间ee7vknir1#