Context: I faced this issue: https://github.com/nestjs/nest/issues/8562#issue-1050869638
Due to the security issues that class-validator presents, I need to replace the class-validator package with the @nestjs/class-validator package. At first, I experienced an error. I tried to solve it using what was suggested here https://github.com/nestjs/nest/issues/8562#issuecomment-970107298. Although I no longer have execution errors running the unit tests, I noticed that the validations included in the DTO (with @nestjs/class-validator) are not being respected.
Code:
...
app.useGlobalPipes(
new ValidationPipe({
validatorPackage: require('@nestjs/class-validator'),
transformerPackage: require('@nestjs/class-transformer'),
transform: true,
whitelist: true,
}),
...
);```
```dto.ts
export class dto {
@IsNumber({}, { each: true, message: 'items should be numbers' })
field?: number[];
}```
```controller.ts
@UsePipes( // I also added this block at the beggining of the controller
new ValidationPipe({
validatorPackage: require('@nestjs/class-validator'),
transformerPackage: require('@nestjs/class-transformer'),
transform: true,
}),
)
@Patch('')
async update(
@Body() payload: dto,
): Promise<string> {
return method(payload ),
);
}```
```test.ts
it(test', async () => {
const body = { field: [0, 'a'] };
const response = await request(app.getHttpServer())
.patch('url')
.set('Accept', 'application/json')
.set('Content-Type', 'application/json')
.send(JSON.stringify(body));
expect(response.status).toBe(HttpStatus.BAD_REQUEST);
});```
Result:
``` expect(received).toBe(expected) // Object.is equality
Expected: 400
Received: 200```