javascripttypescriptnestjs

ValidationPipe complaining about empty fields when using a DTO in body


I am trying to implement validation for API requests to my project like documented here: https://docs.nestjs.com/techniques/validation

It seems to work as when I leave out fields I get appropriate errors messages. Only it also complains when I don't leave them out. Below is my code for the endpoint:

@Post('heatMap')
async getOrCreateHeatMap(@Body() heatMap: HeatMapGetOrCreate) {
    const getHeatMap = await this.graphService.getHeatMapByFields(heatMap);
    if (getHeatMap) {
        return getHeatMap;
    } else {
        // return this.graphService.createHeatMap(name, complexity, repositoryId);
    }
}

This is the DTO class I am using:

export class HeatMapGetOrCreate {
    @IsNotEmpty()
    name!: string;
    @IsNotEmpty()
    complexity!: number;
    @IsNotEmpty()
    repositoryId!: string;
}

I think it's not a problem with the validation maybe, but instead with how I do the @Body part. Is what I'm trying to do possible or do I need to define each field separately?


Solution

  • Silly mistake. I was using postman and posting the request as form data instead of json.