I'm using NestJS with class-validator and class-transformer, I run Jest tests with the following configuration
app.useGlobalPipes(
new ValidationPipe({
skipMissingProperties: false,
skipUndefinedProperties: false,
forbidNonWhitelisted: false,
whitelist: true,
transform: true,
transformOptions: {
enableImplicitConversion: true
}
})
);
I use the transform: true and enableImplicitConversion: true because otherwise numbers don't get transformed correctly, and for reusability I don't use pipes like ParseIntPipe and such because I have a lot of schemas and adding a pipe for each one will take forever,
When I'm testing using Jest + Fastify, whenever I send an object it gets transformed into string ([object Object]) and therefore passes IsString() validation, is that an expected behavior? because I don't think it should be
This is the test (which passed but shouldn't)
const commentContent = { a: 6 };
const body = { content: commentContent };
// Act
const result = await testEnvironment.app.inject({
method: "POST",
url: "api/v1/comment",
payload: JSON.stringify(body),
headers: { "content-type": "application/json" }
});
and this is the schema for the route:
export class CommentCreateSchema {
@IsString()
@MinLength(1)
@MaxLength(1023)
content!: string;
}
Thanks upfront!