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.