[英]Validation for array objects using class-validator in node js
如果为空,我想验证酒精 ID 和酒精名称。这是我的格式:
{
"barId": "string",
"barName": "string",
"drinksItems": [
{
"alcoholId": "string",
"alcoholName": "string",
"mixerList": [
{
"mixerId": "string"
}
],
"modifierList": [
{
"modifierId": "string"
}
]
}]
}
为此,您需要有一个实体并使用@ValidateNested
和@Type
来正确转换它(仅在您使用class-transformer
情况下)。
class Alcohol {
@IsNotEmpty()
alcoholId: string;
@IsNotEmpty()
alcoholName: string;
}
class Bar {
@IsNotEmpty() // <- requires the array to be present
@ValidateNested()
// @Type(() => Alcohol) // <- add only if you use class-transformer
alcohols: Array<Alcohol>;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.