Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validation Pipe is not executed #13260

Closed
4 of 15 tasks
matiasgarcia opened this issue Feb 26, 2024 · 3 comments
Closed
4 of 15 tasks

Validation Pipe is not executed #13260

matiasgarcia opened this issue Feb 26, 2024 · 3 comments
Labels
needs triage This issue has not been looked into

Comments

@matiasgarcia
Copy link

matiasgarcia commented Feb 26, 2024

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

While doing the Advanced Architectures course, using nest 9.4.2 it seems that ValidationPipe is not executed.

async function bootstrap() {
  const app = await NestFactory.create(AppModule.register({ driver: 'orm' }));
  app.useGlobalPipes(
    new ValidationPipe({
      whitelist: true,
    }),
  );
  await app.listen(3000);
}
import { IsNotEmpty, IsString } from 'class-validator';

export class CreateAlarmDto {
  @IsString()
  @IsNotEmpty()
  name: string;

  @IsString()
  @IsNotEmpty()
  severity: string;
}
import { Body, Controller, Get, Post } from '@nestjs/common';
import { AlarmsService } from '../../application/alarms.service';
import { CreateAlarmCommand } from '../../application/commands/create-alarm.command';
import { CreateAlarmDto } from './dto/create-alarm.dto';

@Controller('alarms')
export class AlarmsController {
  constructor(private readonly alarmsService: AlarmsService) {}

  @Post()
  async create(@Body() createAlarmDto: CreateAlarmDto) {
    try {
      const alarm = await this.alarmsService.create(
        new CreateAlarmCommand(createAlarmDto.name, createAlarmDto.severity),
      );
      return alarm;
    } catch (error) {
      console.error(error);
      throw error;
    }
  }

  @Get()
  findAll() {
    return this.alarmsService.findAll();
  }
}

Minimum reproduction code

https://github.com/matiasgarcia/nestjs-advanced-architecture

Steps to reproduce

  1. npm run start:dev
  2. curl -X POST http://localhost:3000/alarms -H "Content-Type: application/json" -d '{"name": "Test Alarm", "severity": "high"}'
  3. You should get `{"statusCode":500,"message":"Internal server error"}``

In logs, you should see

[Nest] 99748  - 25/02/2024, 21:58:19   ERROR [ExceptionsHandler] Cannot read properties of undefined (reading 'name')
TypeError: Cannot read properties of undefined (reading 'name')
    at AlarmsController.create (/Users/matias/projects/advanced-architecture/src/alarms/presenters/http/alarms.controller.ts:14:47)
    at /Users/matias/projects/advanced-architecture/node_modules/@nestjs/core/router/router-execution-context.js:38:29
    at InterceptorsConsumer.intercept (/Users/matias/projects/advanced-architecture/node_modules/@nestjs/core/interceptors/interceptors-consumer.js:11:20)
    at /Users/matias/projects/advanced-architecture/node_modules/@nestjs/core/router/router-execution-context.js:46:60
    at /Users/matias/projects/advanced-architecture/node_modules/@nestjs/core/router/router-proxy.js:9:23
    at Layer.handle [as handle_request] (/Users/matias/projects/advanced-architecture/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/matias/projects/advanced-architecture/node_modules/express/lib/router/route.js:144:13)
    at Route.dispatch (/Users/matias/projects/advanced-architecture/node_modules/express/lib/router/route.js:114:3)
    at Layer.handle [as handle_request] (/Users/matias/projects/advanced-architecture/node_modules/express/lib/router/layer.js:95:5)
    at /Users/matias/projects/advanced-architecture/node_modules/express/lib/router/index.js:284:15

Expected behavior

Request should be processed successfully

Package

  • I don't know. Or some 3rd-party package
  • @nestjs/common
  • @nestjs/core
  • @nestjs/microservices
  • @nestjs/platform-express
  • @nestjs/platform-fastify
  • @nestjs/platform-socket.io
  • @nestjs/platform-ws
  • @nestjs/testing
  • @nestjs/websockets
  • Other (see below)

Other package

No response

NestJS version

9.0.0

Packages versions

{
  "name": "advanced-architecture",
  "version": "0.0.1",
  "description": "",
  "author": "",
  "private": true,
  "license": "UNLICENSED",
  "scripts": {
    "build": "nest build",
    "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
    "start": "nest start",
    "start:dev": "nest start --watch",
    "start:debug": "nest start --debug --watch",
    "start:prod": "node dist/main",
    "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
    "test": "jest",
    "test:watch": "jest --watch",
    "test:cov": "jest --coverage",
    "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
    "test:e2e": "jest --config ./test/jest-e2e.json"
  },
  "dependencies": {
    "@nestjs/common": "^9.0.0",
    "@nestjs/core": "^9.0.0",
    "@nestjs/mapped-types": "*",
    "@nestjs/platform-express": "^9.0.0",
    "@nestjs/typeorm": "^10.0.2",
    "class-transformer": "^0.5.1",
    "class-validator": "^0.14.1",
    "pg": "^8.11.3",
    "reflect-metadata": "^0.1.13",
    "rxjs": "^7.2.0",
    "typeorm": "^0.3.20"
  },
  "devDependencies": {
    "@nestjs/cli": "^9.0.0",
    "@nestjs/schematics": "^9.0.0",
    "@nestjs/testing": "^9.0.0",
    "@types/express": "^4.17.13",
    "@types/jest": "29.5.0",
    "@types/node": "18.15.11",
    "@types/supertest": "^2.0.11",
    "@typescript-eslint/eslint-plugin": "^5.0.0",
    "@typescript-eslint/parser": "^5.0.0",
    "eslint": "^8.0.1",
    "eslint-config-prettier": "^8.3.0",
    "eslint-plugin-prettier": "^4.0.0",
    "jest": "29.5.0",
    "prettier": "^2.3.2",
    "source-map-support": "^0.5.20",
    "supertest": "^6.1.3",
    "ts-jest": "29.0.5",
    "ts-loader": "^9.2.3",
    "ts-node": "^10.0.0",
    "tsconfig-paths": "4.2.0",
    "typescript": "^4.7.4"
  },
  "jest": {
    "moduleFileExtensions": [
      "js",
      "json",
      "ts"
    ],
    "rootDir": "src",
    "testRegex": ".*\\.spec\\.ts$",
    "transform": {
      "^.+\\.(t|j)s$": "ts-jest"
    },
    "collectCoverageFrom": [
      "**/*.(t|j)s"
    ],
    "coverageDirectory": "../coverage",
    "testEnvironment": "node"
  }
}

Node.js version

20

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

@matiasgarcia matiasgarcia added the needs triage This issue has not been looked into label Feb 26, 2024
@danbaghilovici
Copy link

danbaghilovici commented Feb 26, 2024

Hi @matiasgarcia , can you try upgrading the 'reflect-metadata' package to '^0.2' and see if the issue persists?
There was a bug reported that affected validation pipes regarding this package.

Related bug #13107

@kamilmysliwiec
Copy link
Member

That's because you have 2 different reflect-metadata packages installed (v0.1 and v0.2)
image

#13107

@Mnigos
Copy link

Mnigos commented Jul 2, 2024

I also have the same issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs triage This issue has not been looked into
Projects
None yet
Development

No branches or pull requests

4 participants