admin管理员组文章数量:1312806
I am trying to enforce a validation on each item of an array.
As per my understanding (please correct me if I am wrong), class-validator does not support direct validation of arrays. It requires us to create a wrapper class.
So, the following are the classes:
export class SequenceQuery {
@MinLength(10, {
message: 'collection name is too short',
})
collection: string;
identifier: string;
count: number;
}
export class SequenceQueries{
@ValidateNested({ each: true })
queries:SequenceQuery[];
}
And the following is my controller:
@Get("getSequence")
async getSequence(@Body() query:SequenceQueries) {
return await this.sequenceService.getNextSequenceNew(query)
}
The following is the JSON that I am passing to the controller:
{"queries": [
{
"collection": "A",
"identifier": "abc",
"count": 1
},
{
"collection": "B",
"identifier": "mno",
"count": 5
},
{
"collection": "C",
"identifier": "xyz",
"count": 25
}
]}
But it doesn't seem to be working. It's not throwing any validation messages.
I am trying to enforce a validation on each item of an array.
As per my understanding (please correct me if I am wrong), class-validator does not support direct validation of arrays. It requires us to create a wrapper class.
So, the following are the classes:
export class SequenceQuery {
@MinLength(10, {
message: 'collection name is too short',
})
collection: string;
identifier: string;
count: number;
}
export class SequenceQueries{
@ValidateNested({ each: true })
queries:SequenceQuery[];
}
And the following is my controller:
@Get("getSequence")
async getSequence(@Body() query:SequenceQueries) {
return await this.sequenceService.getNextSequenceNew(query)
}
The following is the JSON that I am passing to the controller:
{"queries": [
{
"collection": "A",
"identifier": "abc",
"count": 1
},
{
"collection": "B",
"identifier": "mno",
"count": 5
},
{
"collection": "C",
"identifier": "xyz",
"count": 25
}
]}
But it doesn't seem to be working. It's not throwing any validation messages.
Share Improve this question asked Nov 26, 2020 at 16:01 SamuraiJackSamuraiJack 5,56917 gold badges103 silver badges212 bronze badges3 Answers
Reset to default 4I got the solution to the problem.
I was supposed to change my wrapper class to :
export class SequenceQueries{
@ValidateNested({ each: true })
@Type(() => SequenceQuery) // added @Type
queries:SequenceQuery[];
}
But I will leave the question open, just in case someone has an alternate solution as in not having to make a wrapper class.
There is my plete solution/implementation in Nestjs
- First create my DTO Class
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[];
}
- Place my DTO Class in my Controller definition
@MessagePattern('set_webhooks')
async setWebhooks(
@Payload('body') webhookDtoArray: WebhookDtoArray,
@Payload() data,
): Promise<Store> {
return this.storeManagementService.setWebhooks(
data.userPayload.id,
webhookDtoArray,
);
}
- An example in Postman of what the Body I should send
{
"webhooks": [{
"type": "InvoiceCreated",
"url": "https://test.free.beeceptor.",
"active": true
},
{
"type": "InvoiceSettled",
"url": "https://test.free.beeceptor.",
"active": true
},
{
"type": "InvoiceExpired",
"url": "https://test.free.beeceptor.",
"active": true
}
]
}
class-validator does support array validation you have just to add what you already did in @ValidateNested({ each: true }), you have only to add each to the collection element:
export class SequenceQuery {
@MinLength(10, {
each: true,
message: 'collection name is too short',
})
collection: string;
identifier: string;
count: number;
}
source : Validation Array
本文标签: javascriptNestJs Validating array of objects using classvalidatorStack Overflow
版权声明:本文标题:javascript - NestJs: Validating array of objects using class-validator - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741868932a2402076.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论