【问题标题】:No metadata found for class-validator找不到类验证器的元数据
【发布时间】:2019-08-24 17:53:27
【问题描述】:

我正在尝试使用ValidationPipe,但无论我如何编写代码,在发送请求时都会收到以下警告:No metadata found. There is more than once class-validator version installed probably. You need to flatten your dependencies

我的路线看起来像这样:

@Get()
@UsePipes(new ValidationPipe({ transform: true }))
async findAll(@Query() queryDto: QueryDto) {
    return await this.myService.findAll(queryDto);
}

我的 DTO 看起来像这样:

export class queryDto
{
    @ApiModelProperty({
        description: 'Maximum number of results',
        type: Number,
        example: 50,
        default: 50,
        required: false
    })
    readonly limit: number = 50;
}

the doc 之后,我尝试了几种使用ValidationPipe 的方法,但对我来说没有任何效果。我知道它不起作用,因为尽管请求得到了响应,但当查询为空时,不使用我在 DTO 中为属性limit 编写的默认值50。因此,当查询中没有提供limit 时,limit 的值是未定义的,而它应该是50(即不使用ValidationPipe)。

我的package.json 似乎是正确的:

npm ls class-validator
api-sport@0.0.1 /home/pierre_t/Bureau/dev/ApiSport
└── class-validator@0.9.1

完整的package.json:

{
  "name": "api-sport",
  "version": "0.0.1",
  "description": "",
  "author": "",
  "license": "MIT",
  "scripts": {
    "build": "tsc -p tsconfig.build.json",
    "format": "prettier --write \"src/**/*.ts\"",
    "start": "ts-node -r tsconfig-paths/register src/main.ts",
    "start:dev": "nodemon",
    "start:debug": "nodemon --config nodemon-debug.json",
    "start:prod": "pm2 start ./src/main.js --no-daemon",
    "lint": "tslint -p tsconfig.json -c tslint.json",
    "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": "^6.0.5",
    "@nestjs/core": "^6.0.5",
    "@nestjs/platform-express": "^6.0.5",
    "@nestjs/swagger": "^3.0.1",
    "@nestjs/typeorm": "^6.0.0",
    "@types/lodash": "^4.14.123",
    "class-transformer": "^0.2.0",
    "class-validator": "^0.9.1",
    "dotenv": "^7.0.0",
    "hbs": "^4.0.3",
    "mysql": "^2.16.0",
    "pm2": "^3.4.1",
    "reflect-metadata": "^0.1.12",
    "rimraf": "^2.6.2",
    "rxjs": "^6.3.3",
    "swagger-ui-express": "^4.0.2",
    "typeorm": "^0.2.16"
  },
  "devDependencies": {
    "@nestjs/testing": "^6.0.5",
    "@types/express": "^4.16.0",
    "@types/jest": "^23.3.13",
    "@types/node": "^10.14.4",
    "@types/supertest": "^2.0.7",
    "jest": "^23.6.0",
    "nodemon": "^1.18.9",
    "prettier": "^1.15.3",
    "supertest": "^3.4.1",
    "ts-jest": "^23.10.5",
    "ts-node": "^7.0.1",
    "tsconfig-paths": "^3.7.0",
    "tslint": "5.12.1",
    "typescript": "^3.4.1"
  },
  "jest": {
    "moduleFileExtensions": [
      "js",
      "json",
      "ts"
    ],
    "rootDir": "src",
    "testRegex": ".spec.ts$",
    "transform": {
      "^.+\\.(t|j)s$": "ts-jest"
    },
    "coverageDirectory": "../coverage",
    "testEnvironment": "node"
  }
}

为什么我会收到此消息以及如何使用ValidationPipe

【问题讨论】:

  • 您可以添加您的package.json 吗?您的node_modules 中似乎有多个版本的class-validator
  • @KimKern 我用关于我的package.json的相关信息更新了我的答案
  • 请发布完整的package.json。它可以是您使用的另一个库的子依赖项。
  • 你也可以试试npm ls class-validator
  • @KimKern 我添加了整个文件 + npm ls

标签: javascript node.js typescript nestjs class-validator


【解决方案1】:

这是我的 bootstrap,它适用于 class-validator

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.useGlobalPipes(new ValidationPipe());
  await app.listen(3000);
}
bootstrap();

【讨论】:

    【解决方案2】:

    问题已经回答了,但供有相同问题的人以后参考...

    class-validator 允许您绕过某些属性的验证 (whitelisting) 特殊标志来验证任何属性。

    作为docs

    这将删除所有没有任何装饰器的属性。如果不 其他适合你属性的装饰器,你可以使用 @Allow 装饰师

    例如:

    import {validate, Allow, Min} from "class-validator";
    
    export class Post {
    
        @Allow()
        title: string;
    
        @Min(0)
        views: number;
    
        nonWhitelistedProperty: number;
    }
    

    【讨论】:

      【解决方案3】:

      这是因为您使用的是class-validator,但没有任何验证,请参阅此issue

      基本上,它会警告您在存储中没有任何元数据, 这意味着您没有使用类验证器中的任何装饰器。那 意味着您不执行任何验证,因此您应该通过 validate: false 选项 buildSchema 禁用自动验证。

      我不确定您是否可以对 Nest 的 ValidationPipe 进行验证,但您也可以在 dto 中添加一个断言(如果有意义的话),例如:

      import { Min } from 'class-validator';
      export class QueryDto {
          @Min(1)
          readonly limit: number = 50;
      }
      

      顺便说一句:由于您的@Query 将只有字符串属性,您可能希望将您的limitstring 转换为number。看看这个answer

      【讨论】:

      • 什么是buildSchema?我在哪里可以找到/声明它?
      • (不知何故,在尝试了您的建议之后,我无法设法再次获得undefined。我将代码恢复为默认获得undefined的版本值,但我不再得到undefined,我得到默认值,即使没有类验证器,也没有ValidationPipe。Wtf?那是黑魔法。但这只是一个细节,我的问题似乎已经解决了)
      • 很高兴它现在为您工作。 :-) 我刚刚在没有ValidationPipe 的情况下尝试过它,它没有为我设置默认值。设置默认值的重要一点是您的 dto 类实际上是实例化的。否则它将是一个普通的 javascript 对象。
      猜你喜欢
      • 1970-01-01
      • 2016-12-21
      • 2013-05-09
      • 2021-02-03
      • 2011-08-24
      • 2014-02-06
      • 2015-01-02
      • 2016-04-16
      • 1970-01-01
      相关资源
      最近更新 更多